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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Rotate Matrix Elements
- Rotate the matrix right by K times
- Rotate each ring of matrix anticlockwise by K elements
- Inplace rotate square matrix by 90 degrees | Set 1
- Inplace (Fixed space) M x N size matrix transpose | Updated
- Printing Matrix Chain Multiplication (A Space Optimized Solution)
- Turn an image by 90 degree
- Rotate bits of a number
- Rotate a Linked List
- Python | Ways to rotate a list
- Program to cyclically rotate an array by one
- Rotate Doubly linked list by N nodes
- Rotate Linked List block wise
- Minimum steps required to convert the matrix into lower hessenberg matrix
- Minimum number of steps to convert a given matrix into Upper Hessenberg matrix
- Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
- Check if matrix can be converted to another matrix by transposing square sub-matrices