Rotate Matrix

Moderate
0/80
Average time to solve is 15m
321 upvotes
Asked in companies
AmazonSchlumbergerZoho Corporation

Problem statement

Given a 2-dimensional matrix of size ‘N’ x ‘M’, rotate the elements of the matrix clockwise.

For example: 
Input Matrix: [ [ 1, 2, 3 ] 
                [ 4, 5, 6 ] 
                [ 7, 8, 9 ] ]

Output Matrix: [ [ 4, 1, 2 ] 
                 [ 7, 5, 3 ] 
                 [ 8, 9, 6 ] ]

The output matrix is generated by rotating the elements of the input matrix in a clockwise direction. Note that every element is rotated only once. 
Note :
You do not need to print anything; it has already been taken care of. Also, update the given matrix in-place.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line of input contains an integer 'T' representing the number of test cases. Then the test cases follow.

The first line of each test case contains two single-spaced integers N and M, representing the number of rows and columns of the matrix, respectively.

The next N line contains M single-spaced integers denoting the matrix elements. 
Output Format :
For each test case, the modified matrix is printed.

The output for each test case is in a separate line.
Constraints :
1 <= T <= 10
1 <= N, M <= 100
-10^5 <= data <= 10^5,

where ‘T’ is the number of test cases,  ‘N’ and ‘M’ are the numbers of rows and columns respectively and ‘data’ is the value of the elements of the matrix.
Sample Input 1 :
1
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Sample Output 1 :
5 1 2 3
9 10 6 4
13 11 7 8
14 15 16 12
Explanation of Sample Input 1 :

The resulting matrix after rotating the given matrix clockwise is shown above. 
Sample Input 2 :
2
2 2
1 3
4 5
3 3
3 4 5
5 6 7
8 10 20
Sample Output 2 :
4 1
5 3
5 3 4
8 6 5
10 20 7 
Hint

 Can you solve this recursively?

Approaches (2)
Recursion

The idea is to consider the matrix in the form of rings and then rotate each ring recursively. One ring will be rotated in one recursive call. 

An image showing all the rings in a matrix is given below: 

There are two rings in the above matrix. The outer ring is shown in the yellow colour, and the inner ring is shown in the blue colour.  It’s easy to rotate the elements in the form of rings. 

Matrix after rotating the outer ring: 

Matrix after rotating the inner ring: 

As there is no more ring, this is the modified output matrix. 
 

Algorithm: 

  1. Create a helper function which takes the indices of the current ring as parameters, i.e the starting row index, ending row index, starting column index, ending column index.
  2. Call the helper function for the outer ring.
  3. In the helper function,
    1. Check the base condition, i.e. whether the indices of the ring are valid or not.
    2. Rotate the current ring as:
      1. Move the elements of the top side.
      2. Move the elements of the right side.
      3. Move the elements of the bottom side.
      4. Move the elements of the left side.
    3. Recursively call the function for the inner ring by passing the indices of the inner ring as parameters.
Time Complexity

O(N * M), where N is the number of rows and M is the number of columns in the matrix. 

 

Since we are traversing each element of the matrix just once, the time complexity of the above algorithm is O(N * M). 

Space Complexity

O(max(N, M)), where N is the number of rows and M is the number of columns in the matrix. 

 

We are making max(N/2, M/2) recursive calls and thus, stack space will be used. Hence, the space complexity is O(max(N, M)). 

Code Solution
(100% EXP penalty)
Rotate Matrix
Full screen
Console