Shift matrix elements row-wise by k
Last Updated :
20 Feb, 2023
Given a square matrix mat[][] and a number k. The task is to shift the first k elements of each row to the right of the matrix.
Examples :
Input : mat[N][N] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
k = 2
Output :mat[N][N] = {{3, 1, 2}
{6, 4, 5}
{9, 7, 8}}
Input : mat[N][N] = {{1, 2, 3, 4}
{5, 6, 7, 8}
{9, 10, 11, 12}
{13, 14, 15, 16}}
k = 2
Output :mat[N][N] = {{3, 4, 1, 2}
{7, 8, 5, 6}
{11, 12, 9, 10}
{15, 16, 13, 14}}
Note: Matrix should be a square matrix
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 4
void shiftMatrixByK(int mat[N][N], int k)
{
if (k > N) {
cout << "shifting is not possible" << endl;
return;
}
int j = 0;
while (j < N) {
for (int i = k; i < N; i++)
cout << mat[j][i] << " ";
for (int i = 0; i < k; i++)
cout << mat[j][i] << " ";
cout << endl;
j++;
}
}
int main()
{
int mat[N][N] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
int k = 2;
shiftMatrixByK(mat, k);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static int N = 4;
static void shiftMatrixByK(int [][]mat,
int k)
{
if (k > N) {
System.out.print("Shifting is"
+ " not possible");
return;
}
int j = 0;
while (j < N) {
for (int i = k; i < N; i++)
System.out.print(mat[j][i] + " ");
for (int i = 0; i < k; i++)
System.out.print(mat[j][i] + " ");
System.out.println();
j++;
}
}
public static void main(String args[])
{
int [][]mat = new int [][]
{ {1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16} };
int k = 2;
shiftMatrixByK(mat, k);
}
}
|
Python3
N = 4
def shiftMatrixByK(mat, k):
if (k > N) :
print ("shifting is"
" not possible")
return
j = 0
while (j < N) :
for i in range(k, N):
print ("{} " .
format(mat[j][i]), end="")
for i in range(0, k):
print ("{} " .
format(mat[j][i]), end="")
print ("")
j = j + 1
mat = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
k = 2
shiftMatrixByK(mat, k)
|
C#
using System;
class GFG {
static int N = 4;
static void shiftMatrixByK(int [,]mat,
int k)
{
if (k > N) {
Console.WriteLine("shifting is"
+ " not possible");
return;
}
int j = 0;
while (j < N) {
for (int i = k; i < N; i++)
Console.Write(mat[j,i] + " ");
for (int i = 0; i < k; i++)
Console.Write(mat[j,i] + " ");
Console.WriteLine();
j++;
}
}
public static void Main()
{
int [,]mat = new int [,]
{ {1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16} };
int k = 2;
shiftMatrixByK(mat, k);
}
}
|
PHP
<?php
function shiftMatrixByK($mat, $k)
{
$N = 4;
if ($k > $N)
{
echo ("shifting is not possible\n");
return;
}
$j = 0;
while ($j < $N)
{
for ($i = $k; $i < $N; $i++)
echo ($mat[$j][$i]." ");
for ($i = 0; $i < $k; $i++)
echo ($mat[$j][$i]." ");
echo ("\n");
$j++;
}
}
$mat = array(array(1, 2, 3, 4),
array(5, 6, 7, 8),
array(9, 10, 11, 12),
array(13, 14, 15, 16));
$k = 2;
shiftMatrixByK($mat, $k);
?>
|
Javascript
<script>
let N = 4;
function shiftMatrixByK(mat,k)
{
if (k > N) {
document.write("Shifting is not possible");
return;
}
let j = 0;
while (j < N) {
for (let i = k; i < N; i++)
document.write(mat[j][i] + " ");
for (let i = 0; i < k; i++)
document.write(mat[j][i] + " ");
document.write("<br>");
j++;
}
}
let mat =
[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]];
let k = 2;
shiftMatrixByK(mat, k);
</script>
|
Output
3 4 1 2
7 8 5 6
11 12 9 10
15 16 13 14
Complexity Analysis:
- Time Complexity: O(n2),
- Auxiliary Space: O(1)
Please Login to comment...