How rotating works
Rotating a matrix by 90° turns the whole grid a quarter turn, the way you would turn a photo. Clockwise, the top row swings round to become the rightmost column, the second row becomes the column just inside it, and the bottom row becomes the leftmost column. Every cell keeps its value and its neighbours; only the orientation changes.
Because rows become columns, a matrix with rows rows and cols columns turns into one with cols rows and rows columns, just like a transpose. The difference is the direction each row is read into its column. Use the Clockwise and Counter-clockwise toggle above to watch the same grid turn both ways.
The index formula
Clockwise, row i of A becomes column rows − 1 − i of the result, so the top row lands in the last column:
B = new matrix[cols][rows]
for i in 0 .. rows-1:
for j in 0 .. cols-1:
B[j][rows-1-i] = A[i][j]
return BCounter-clockwise, column j of A becomes row cols − 1 − j of the result, so the write is B[cols-1-j][i] = A[i][j]. Both versions do one read and one write per cell, rows × cols steps in all. The − 1 is there because indices start at 0: in a matrix with 3 rows the last row is row 2.
A worked example: a 3 × 4 grid, clockwise
The top row, 1 2 3 4, becomes the last column of the result, read top to bottom. The bottom row, 9 10 11 12, becomes the first column. The result has 4 rows and 3 columns.
Follow one cell: 7 is at A[1][2]. Clockwise it goes to row 2, column 3 − 1 − 1 = 1, so B[2][1] = 7. Counter-clockwise it would go to row 4 − 1 − 2 = 1, column 1 instead.
Rotating in place
A square matrix can be rotated without a second copy, and this is the version interviewers ask for. Clockwise is a transpose followed by reversing every row:
transpose(A) # A[i][j] <-> A[j][i] for j > i for each row of A: reverse(row) # two pointers swapping inwards
Counter-clockwise is a transpose followed by reversing every column, or, if you prefer, reverse every row first and then transpose. The order matters: reverse the rows before transposing and a clockwise recipe turns into a counter-clockwise one. The other classic in-place method moves four cells at a time around each ring of the matrix, which is the same idea as a spiral traversal with a four-way swap.
Where you'll meet it
- Images. An image is a matrix of pixels, and rotating it a quarter turn is exactly this loop.
- Games. Tetris pieces, jigsaw tiles and dungeon rooms are small matrices that get rotated on the fly.
- Puzzles. Checking a board against its four rotations finds positions that are really the same.
- Interviews. Rotate Image is one of the most asked matrix questions, and the in-place version is the one to know.
Mistakes beginners make
- Rotating the wrong way. Clockwise uses
rows − 1 − ion the column index; counter-clockwise usescols − 1 − jon the row index. Mixing them gives a transpose or a mirror instead. - Keeping the old shape. The result of a 3 × 4 rotation is 4 × 3. Allocate it that way.
- Reversing before transposing. In the in-place recipe the order decides the direction.
Every rotation costs one step per cell. See how that growth compares with other algorithms: Big-O Playground →
Matrix rotation questions
How do you rotate a matrix 90 degrees clockwise?
Create a result with the dimensions swapped, then copy every A[i][j] to B[j][rows − 1 − i]. The first row of A becomes the last column of B, read top to bottom. For a square matrix you can do it in place: transpose, then reverse each row.
How do you rotate a matrix counter-clockwise?
Copy every A[i][j] to B[cols − 1 − j][i]. The first row of A becomes the first column of B, filled from the bottom up. In place on a square matrix: transpose, then reverse each column, or equivalently reverse each row first and then transpose.
How do you rotate a matrix 180 degrees?
Rotate 90° twice, or reverse the order of the rows and then reverse each row. That is the same as a vertical flip followed by a horizontal flip, and the shape stays the same.
What is the time complexity of rotating a matrix?
O(rows × cols): each cell is read once and written once. The copying version needs a second matrix; the in-place version for square matrices needs only a temporary variable for each swap.