Given a square matrix, turn it by 90 degrees in anti-clockwise direction without using any extra space.
Input: 1 2 3 4 5 6 7 8 9 Output: 3 6 9 2 5 8 1 4 7 Rotated the input matrix by 90 degrees in anti-clockwise direction. Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 4 8 12 16 3 7 11 15 2 6 10 14 1 5 9 13 Rotated the input matrix by 90 degrees in anti-clockwise direction.
An approach that requires extra space is already discussed in a different article:
Inplace rotate square matrix by 90 degrees | Set 1
This post discusses the same problem with a different approach which is space-optimized.
Approach: The idea is to find the transpose of the matrix and then reverse the columns of the transposed matrix.
Here is an example to show how this works.
- To solve the given problem there are two tasks. 1st is finding the transpose and second is reversing the columns without using extra space
- A transpose of a matrix is when the matrix is flipped over its diagonal, i.e the row index of an element becomes the column index and vice versa. So to find the transpose interchange the elements at position (i, j) with (j, i). Run two loops, the outer loop from 0 to row count and inner loop from 0 to index of the outer loop.
- To reverse the column of the transposed matrix, run two nested loops, the outer loop from 0 to column count and inner loop from 0 to row count/2, interchange elements at (i, j) with (i, row[count-1-j]), where i and j are indices of inner and outer loop respectively.
4 8 12 16 3 7 11 15 2 6 10 14 1 5 9 13
- Time complexity :O(R*C).
The matrix is traversed twice, so the complexity is O(R*C).
- Space complexity :O(1).
The space complexity is constant as no extra space is required.
Implementation: Let’s see a method of Python numpy that can be used to arrive at the particular solution.
numpy implementation [[ 4 8 12 16] [ 3 7 11 15] [ 2 6 10 14] [ 1 5 9 13]]
Note: The above steps/programs do left (or anticlockwise) rotation. Let’s see how to do the right rotation or clockwise rotation. The approach would be similar. Find the transpose of the matrix and then reverse the rows of the transposed matrix.
This is how it is done.
This article is contributed by DANISH_RAZA . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Rotate a matrix by 90 degree in clockwise direction without using any extra space
- Rotate a Matrix by 180 degree
- Print n x n spiral matrix using O(1) extra space
- Inplace rotate square matrix by 90 degrees | Set 1
- Rotate Matrix Elements
- Rotate each ring of matrix anticlockwise by K elements
- Rotate the matrix right by K times
- Rotate matrix by 45 degrees
- Maximum trace possible for any sub-matrix of the given matrix
- Turn an image by 90 degree
- Inplace (Fixed space) M x N size matrix transpose | Updated
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Minimum steps to reach any of the boundary edges of a matrix | Set-2
- Minimum steps to reach any of the boundary edges of a matrix | Set 1
- Rotate bits of a number
- Rotate a Linked List
- Program to cyclically rotate an array by one
- Rotate Linked List block wise
- String slicing in Python to rotate a string
- Rotate Doubly linked list by N nodes