Multidimensional Arrays Notes
0% completed
Search in a row wise and column wise sorted matrix
Matrix Is Symmetric
Matrix Flip Bit
Spiral Matrix
Find nth elements of spiral matrix
Rotation Based Problems
0% completed
Rotate matrix to the right
Inplace rotate matrix 90 degree
0% completed
Matrix Median
Empty Cells in a Matrix
Return in Row wave form
Maximum 1s
Minimum Sum in matrix
Summed Matrix
Print All K x K
Reset Matrix
Sub query sum
Array sum
Find rank 8

# Rotate matrix to the right

Difficulty: MEDIUM
Avg. time to solve
20 min
Success Rate
80%

Problem Statement
Suggest Edit

#### You have been given a matrix ‘MAT’ of size 'N' * 'M' (where 'N' and 'M' denote the number of rows and columns respectively) and a positive integer ‘K’. Your task is to rotate the matrix to the right 'K' times.

##### Note:
``````Right rotation on a matrix is shifting each column to the right side (the last column moves to the first column) and 'K' times means performing this rotation 'K' times.
``````
##### Example:
``````For 'K' = 1 and the given 'MAT':

1 2 3
4 5 6
7 8 9

Output after rotating 'MAT' one time is:

3 1 2
6 4 5
9 7 8
``````
##### Input Format:
``````The first line of input contains an integer 'T' representing the number of test cases.

The first line of each test case contains three single space-separated integers ‘N’, ‘M’, and ‘K’, respectively. ‘N’ and ‘M’ represent the rows and columns of the matrix and ‘K’ denotes the number of right rotations to be performed.

Then each of the next 'N' lines of each test case contains 'M' single space-separated integers representing the elements in a row of the matrix.
``````
##### Output Format:
``````For each test case, return the elements of the matrix row-wise after rotation in a single line.
``````
##### Note:
``````You don't need to print the output, It has already been taken care of. Just implement the given function.
``````
##### Constraints:
``````1 <= T <= 10
1 <= N <= 200
1 <= M <= 200
0 <= K <= 10^9
1 <= MAT[i][j] <= 10^5

Where 'MAT[i][j]' denotes the element in the 'i'th row and 'j'th column of the matrix.

It is guaranteed that sum of 'N' * 'M' over all the test cases does not exceed 10 ^ 5.

Time limit: 1 sec
``````
##### Sample Input 1 :
``````2
3 3 2
10 20 30
40 50 60
70 80 90
2 2 2
1 2
3 4
``````
##### Sample Output 1 :
``````20 30 10 50 60 40 80 90 70
1 2 3 4
``````
##### Explanation for Sample Output 1:
``````In test case 1, Performing right rotation for the first time ('K' = 1) we get:

30 10 20
60 40 50
90 70 80

Performing right rotation for the second time ('K' = 2) we get:

20 30 10
50 60 40
80 90 70

The matrix after rotations will be printed in a single line row-wise. Therefore, the output is:

20 30 10 50 60 40 80 90 70

In test case 2, Performing right rotation for the first time ('K' = 1) we get:

2 1
4 3

Performing right rotation for the second time ('K' = 2) we get:

1 2
3 4

The matrix after rotations will be printed in a single line row-wise. Therefore, the output is:

1 2 3 4
``````
##### Sample Input 2 :
``````2
2 3 2
1 2 3
4 5 6
4 4 1
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
``````
##### Sample Output 2 :
``````2 3 1 5 6 4
4 1 2 3 8 5 6 7 12 9 10 11 16 13 14 15
``````
##### Explanation for Sample Output 2:
``````In test case 1, Performing right rotation for the first time ('K' = 1) we get:

3 1 2
6 4 5

Performing right rotation for the second time ('K' = 2) we get:

2 3 1
5 6 4

The matrix after rotations will be printed in a single line row-wise. Therefore, the output is:

2 3 1 5 6 4

In test case 2, Performing right rotation for the first time ('K' = 1) we get:

4 1 2 3
8 5 6 7
12 9 10 11
16 13 14 15

The matrix after rotations will be printed in a single line row-wise. Therefore, the output is:

4 1 2 3 8 5 6 7 12 9 10 11 16 13 14 15
``````   Console