Shuffle the given Matrix K times by reversing row and columns alternatively in sequence
Given a matrix arr[][] of size M * N, where M is the number of rows and N is the number of columns, and an integer K. The task is to shuffle this matrix in K steps such that in each step, the rows and columns of the matrix are reversed alternatively i.e start with reversing the 1st row first step, then the 1st column in the second step, then the 2nd row in the third step and so on.
Note: If K exceeds the M or N then reversal starts from the first row or column again.
Examples:
Input: arr[][] = {{3, 4, 1, 8},
{11, 23, 43, 21},
{12, 17, 65, 91},
{71, 56, 34, 24}}
K = 4
Output: {{71, 56, 4, 3},
{21, 17, 23, 12},
{11, 43, 65, 91},
{8, 1, 34, 24}}
Explanation: Steps to be followed:
Reverse the first row
Reverse the first column
Reverse the second row
Reverse the second column
Input: arr[][] = {{11, 23, 43, 21},
{12, 17, 65, 91},
{71, 56, 34, 24}}
K = 8
Output: {{11, 43, 56, 21},
{91, 65, 17, 12},
{24, 34, 23, 71}}
Approach: The problem can be solved by simply running two loops for traversing rows and columns alternatively, till K becomes 0. In the end, print the resultant matrix. Follow the steps mentioned below:
- Run a loop for traversing the matrix.
- In each iteration reverse the proper row and column.
- Do this operation K times.
- Print the final loop.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
const int M = 3;
const int N = 4;
void showArray( int arr[][N])
{
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++)
cout << arr[i][j] << " " ;
cout << endl;
}
}
void reverseAlternate( int arr[][N], int K)
{
int turn = 0;
int row = 0, col = 0;
while (turn < K) {
if (turn % 2 == 0) {
int start = 0, end = N - 1, temp;
while (start < end) {
temp = arr[row % M][start];
arr[row % M][start] =
arr[row % M][end];
arr[row % M][end] = temp;
start += 1;
end -= 1;
}
row++;
turn++;
}
else {
int start = 0, end = M - 1, temp;
while (start < end) {
temp = arr[start][col % N];
arr[start][col % N] =
arr[end][col % N];
arr[end][col % N] = temp;
start += 1;
end -= 1;
}
col++;
turn++;
}
}
}
int main()
{
int matrix[M][N] = { { 11, 23, 43, 21 },
{ 12, 17, 65, 91 },
{ 71, 56, 34, 24 } };
int K = 8;
reverseAlternate(matrix, K);
showArray(matrix);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int M = 3 ;
public static int N = 4 ;
public static void showArray( int arr[][])
{
for ( int i = 0 ; i < M; i++) {
for ( int j = 0 ; j < N; j++)
System.out.print(arr[i][j] + " " );
System.out.println();
}
}
public static void reverseAlternate( int arr[][], int K)
{
int turn = 0 ;
int row = 0 , col = 0 ;
while (turn < K) {
if (turn % 2 == 0 ) {
int start = 0 , end = N - 1 , temp;
while (start < end) {
temp = arr[row % M][start];
arr[row % M][start] =
arr[row % M][end];
arr[row % M][end] = temp;
start += 1 ;
end -= 1 ;
}
row++;
turn++;
}
else {
int start = 0 , end = M - 1 , temp;
while (start < end) {
temp = arr[start][col % N];
arr[start][col % N] =
arr[end][col % N];
arr[end][col % N] = temp;
start += 1 ;
end -= 1 ;
}
col++;
turn++;
}
}
}
public static void main (String[] args)
{
int matrix[][] = { { 11 , 23 , 43 , 21 },
{ 12 , 17 , 65 , 91 },
{ 71 , 56 , 34 , 24 } };
int K = 8 ;
reverseAlternate(matrix, K);
showArray(matrix);
}
}
|
Python3
M = 3
N = 4
def showArray(arr):
for i in range (M):
for j in range (N):
print (arr[i][j], end = " " )
print ()
def reverseAlternate(arr, K):
turn = 0
row = 0
col = 0
while (turn < K):
if (turn % 2 = = 0 ):
start = 0
end = N - 1
temp = 0
while (start < end):
temp = arr[row % M][start]
arr[row % M][start] = arr[row % M][end]
arr[row % M][end] = temp
start + = 1
end - = 1
row + = 1
turn + = 1
else :
start = 0
end = M - 1
temp = 0
while (start < end):
temp = arr[start][col % N]
arr[start][col % N] = arr[end][col % N]
arr[end][col % N] = temp
start + = 1
end - = 1
col + = 1
turn + = 1
matrix = [[ 11 , 23 , 43 , 21 ] ,
[ 12 , 17 , 65 , 91 ] ,
[ 71 , 56 , 34 , 24 ]]
K = 8
reverseAlternate(matrix, K)
showArray(matrix)
|
C#
using System;
class GFG {
public static int M = 3;
public static int N = 4;
public static void showArray( int [, ] arr)
{
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++)
Console.Write(arr[i, j] + " " );
Console.WriteLine();
}
}
public static void reverseAlternate( int [, ] arr, int K)
{
int turn = 0;
int row = 0, col = 0;
while (turn < K) {
if (turn % 2 == 0) {
int start = 0, end = N - 1, temp;
while (start < end) {
temp = arr[row % M, start];
arr[row % M, start] = arr[row % M, end];
arr[row % M, end] = temp;
start += 1;
end -= 1;
}
row++;
turn++;
}
else {
int start = 0, end = M - 1, temp;
while (start < end) {
temp = arr[start, col % N];
arr[start, col % N] = arr[end, col % N];
arr[end, col % N] = temp;
start += 1;
end -= 1;
}
col++;
turn++;
}
}
}
public static void Main( string [] args)
{
int [, ] matrix = { { 11, 23, 43, 21 },
{ 12, 17, 65, 91 },
{ 71, 56, 34, 24 } };
int K = 8;
reverseAlternate(matrix, K);
showArray(matrix);
}
}
|
Javascript
<script>
let M = 3;
let N = 4;
function showArray(arr) {
for (let i = 0; i < M; i++) {
for (let j = 0; j < N; j++)
document.write(arr[i][j] + " " );
document.write( '<br>' );
}
}
function reverseAlternate(arr, K) {
let turn = 0;
let row = 0, col = 0;
while (turn < K) {
if (turn % 2 == 0) {
let start = 0, end = N - 1, temp;
while (start < end) {
temp = arr[row % M][start];
arr[row % M][start] =
arr[row % M][end];
arr[row % M][end] = temp;
start += 1;
end -= 1;
}
row++;
turn++;
}
else {
let start = 0, end = M - 1, temp;
while (start < end) {
temp = arr[start][col % N];
arr[start][col % N] =
arr[end][col % N];
arr[end][col % N] = temp;
start += 1;
end -= 1;
}
col++;
turn++;
}
}
}
let matrix = [[11, 23, 43, 21],
[12, 17, 65, 91],
[71, 56, 34, 24]];
let K = 8;
reverseAlternate(matrix, K);
showArray(matrix);
</script>
|
Output
11 43 56 21
91 65 17 12
24 34 23 71
Time Complexity: O(K * max(M, N))
Auxiliary Space: O(1)
Last Updated :
03 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...