![]() |
VOOZH | about |
Given a square matrix mat[][], turn it by 90 degrees in an clockwise direction without using any extra space
Examples:
Input: mat[][] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
Output: {{7, 4, 1},
{8, 5, 2},
{9, 6, 3}}Input: mat[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11,12}
{13, 14, 15, 16}}
Output: {{13, 9, 5, 1},
{14, 10, 6, 2},
{15, 11, 7, 3},
{16, 12, 8, 4}
We mainly need to move first row elements to last column, second row elements to second last column.
Let us first try to find out a pattern to solve the problem for n = 4 (second example matrix above)
mat[0][0] goes to mat[0][3]
mat[0][1] goes to mat[1][3]
.............................................
mat[1][0] goes to mat[0][2]
............................................
mat[3][3] goes to mat[3][0]
Do you see a pattern? Mainly we need to move mat[i][j] to mat[j][n-i-1]. We first move elements in a temporary matrix, then copy the temporary to the original. If we directly copy elements within the matrix, it would cause data loss.
13 9 5 1 14 10 6 2 15 11 7 3 16 12 8 4
The approach is similar to Inplace rotate square matrix by 90 degrees counterclockwise. The only thing that is different is to print the elements of the cycle in a clockwise direction i.e. An n x n matrix will have floor(n/2) square cycles. For example, a 3 X 3 matrix will have 1 cycle and a 4 x 4 matrix will have 2 cycles. The first cycle is formed by its 1st row, last column, last row, and 1st column.
For each square cycle, we swap the elements involved with the corresponding cell in the matrix in the clockwise direction. We just need a temporary variable for this.
Explanation:
Let us consider the below matrix for example
{{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
During first iteration of first and only cycle
mat[i][j] = Element at first index (leftmost corner top)= 1.
mat[j][n-1-i]= Rightmost corner top Element = 3.
mat[n-1-i][n-1-j] = Rightmost corner bottom element = 9.
mat[n-1-j][i] = Leftmost corner bottom element = 7.
Move these elements in the clockwise direction.
During second iteration of first and only cycle
mat[i][j] = 2.
mat[j][n-1-i] = 6.
mat[n-1-i][n-1-j] = 8.
mat[n-1-j][i] = 4.
Similarly, move these elements in the clockwise direction.
Follow the given steps to solve the problem:
Below is the implementation of the above approach:
13 9 5 1 14 10 6 2 15 11 7 3 16 12 8 4
When you think about rotating a square matrix 90 degrees clockwise, each element moves to a new position. The top row becomes the right column, the second row becomes the second-right column, and so forth. If we first transpose the matrix and then find reverse of every row, we get the desired result.
Follow the given steps to solve the problem:
1 2 3 1 4 7 7 4 1
4 5 6 —Transpose-> 2 5 8 —-Reverse rows—-> 8 5 2
7 8 9 3 6 9 9 6 3
Below is the implementation of the above approach:
13 9 5 1 14 10 6 2 15 11 7 3 16 12 8 4
Alternate Implementation
We can alternatively do the following as well. But the above implementation is better as row reversal is more cache friendly than column reversal.
13 9 5 1 14 10 6 2 15 11 7 3 16 12 8 4