Last Updated: 16 Dec, 2020

Rotate matrix by 90 degrees

Easy
Asked in companies
SamsungAdobeMicrosoft

Problem statement

You are given a square matrix of non-negative integers 'MATRIX'. Your task is to rotate that array by 90 degrees in an anti-clockwise direction using constant extra space.

For example:

For given 2D array :

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

After 90 degree rotation in anti clockwise direction, it will become:

    [   [ 3,  6,  9 ],
        [ 2,  5,  8 ],
        [ 1,  4,  7 ]   ]
Input Format :
The first line of input contains an integer 'T' representing the number of the test case. Then the test case follows.

The first line of each test case contains an integer 'N' representing the size of the square matrix 'ARR'.

Each of the next 'N' lines contains 'N' space-separated integers representing the elements of the matrix 'ARR'.

Output format :

For each test case, print N lines where N is the size of the matrix, containing N space-separated integer denoting the elements of the matrix after rotation.

The output of each test case will be printed in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 ≤ T ≤ 50
1 ≤ N ≤ 100
1 ≤ MATRIX[i][j] ≤ 10 ^ 5

Time Limit: 1 sec.

Approaches

01 Approach

The idea is to find the transpose of the given matrix and then reverse the columns of the transposed matrix. For example:

For the given 2D matrix:
	[ [ 1,  2,  3 ],
	  [ 4,  5,  6 ],
	  [ 7,  8,  9 ] ]
After taking transpose, it will become:
	[ [ 1,  4,  7 ],
	  [ 2,  5,  8 ],
	  [ 3,  6,  9 ] ]
After reversing the columns, it will become 90-degree rotation in the anti-clockwise direction of the given matrix, it will become:
	[ [ 3,  6,  9 ],
	  [ 2,  5,  8 ],
	  [ 1,  4,  7 ] ]
  1. To solve the problem, there have to perform two tasks.
    1. Finding the transpose
    2. Reversing the columns
  2. For finding the transpose of the matrix we swap ARR[i][j] with ARR[j][i] for each 0 <= i < N and i <= j < N i.e. we swap elements across the principal diagonal of the matrix.
  3. Then we reverse the columns by swapping ARR[j][i] to ARR[k][i] for each  0 <= i < N representing the columns and 0 <= j, k < N where,
    1. j is incremented from 0 to the point where j>=k.
    2. k is decremented from N-1 to the point where k<=j.

02 Approach

The idea is to rotate the matrix in form of square frames, dividing the matrix into squares or cycles. For example, In a matrix of size 5 x 5 will have 3 cycles. The first cycle is formed by its first row, last column, last row and first column. The second cycle is formed by the second row, second-last column, second-last row and second column. The third cycle is formed by third row, third-last column, third-last row and third column. For each frame, swap the elements involved with the corresponding cell in the matrix in anti-clockwise direction i.e. from top to left, left to bottom, bottom to right and from right to top one at a time using just a temporary variable.

 

  1. There are N/2  cycles in a matrix of size N.
  2. We traverse in the matrix from the outermost cycle, i.e. (0,0) to innermost cycle i.e. ((N/2)-1, (N/2)-1).
  3. For each cycle, we’ll swap the elements of the matrix in a group of four elements i.e. for each i <= j < N-i-1 for each 0 <= i <= (N/2)-1 we swap:
    1. ARR[i] [j] with ARR[j, N-1-i]
    2. ARR[j, N-1-i] with ARR[N-1-i, N-1-j]
    3. ARR[N-1-i, N-1-j] with ARR[N-1-j,i]
    4. ARR[N-1-j,i]with ARR[i][j]
  4. At the end of these loops, we’ll get a rotated matrix.
  5. Print the matrix.