How do you rotate a two dimensional array?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Rotating a two-dimensional array is a common operation in computer science and image processing. This operation involves changing the orientation of an array, which is often used in graphics applications and matrix manipulations. In this article, we will explore different methods to rotate a 2D array, primarily focusing on square matrices, and understand the underlying algorithmic concepts.
Understanding the Basics
A two-dimensional array (or matrix) is a sequence of elements arranged in rows and columns. If the number of rows equals the number of columns, it is known as a square matrix. Rotating a matrix usually involves rotating the elements by 90 degrees clockwise or counterclockwise.
Rotations:
- 90 Degrees Clockwise:
- The first row becomes the last column, the second row becomes the second-last column, and so on.
- 90 Degrees Counterclockwise:
- The first column becomes the last row, the second column becomes the second-last row, and so on.
- 180 Degrees:
- A combination of two 90-degree clockwise or counterclockwise rotations.
Algorithm for 90-Degree Clockwise Rotation
To rotate an N x N matrix by 90 degrees clockwise, we can follow these steps:
- Transpose the Matrix:
- Swap elements
matrix[i][j]withmatrix[j][i]for all i, j.
- Reverse Each Row:
- After the transposition, reverse each row to achieve the final rotated matrix.
Code Example - 90 Degrees Clockwise Rotation
Below is an example in Python to illustrate this process:
Algorithm for 90-Degree Counterclockwise Rotation
For a 90-degree counterclockwise rotation, the steps are as follows:
- Transpose the Matrix:
- Swap elements
matrix[i][j]withmatrix[j][i].
- Reverse Each Column:
- After transposition, reverse each column to get the final rotated matrix.
Code Example - 90 Degrees Counterclockwise Rotation
Here is the Python code to achieve a counterclockwise rotation:
Summarizing Key Operations
Below is a table summarizing the steps involved in matrix rotations:
| Rotation Type | Main Steps |
| 90 Degrees Clockwise | Transpose Reverse Each Row |
| 90 Degrees Counterclockwise | Transpose Reverse Each Column |
| 180 Degrees | Rotate 90 Degrees Twice |
Considerations and Complexity
- Time Complexity: The above methods run in time, where is the number of rows or columns in the matrix. This is because each element of the matrix is accessed a constant number of times.
- Space Complexity: These approaches use additional space, modifying the matrix in place.
Extensions
Apart from the discussed techniques, rotating non-square matrices or rotating by angles other than multiples of 90 degrees typically requires different approaches, often involving computationally heavier operations.
- Non-Square Matrices: Special handling is required, and such rotations are typically approached by translating the transformations into a linear algebra context.
- Arbitrary Angles: Rotations at arbitrary angles are beyond simple index manipulation and require interpolation techniques, especially in the context of graphical transformations.
This comprehensive understanding of rotating two-dimensional arrays equips you to tackle a variety of problems in competitive programming, game development, and real-world applications of data transformation.
Related reading
- How do you sort a dictionary by value?
- How do you sort an array on multiple columns?
- How do you tell if two wildcards overlap?
- How do you use a Bidirectional BFS to find the shortest path?
- How do you sort a dictionary by value?
- How do you update multiple field using Update.Set in MongoDB using official c driver?
- How do you validate a binary search tree?
- How do you write a program to find if certain words are similar?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.