GOAT EngineerPreparing your learning space…

arrays · matrix · in-place

Rotate a matrix 90° with no second grid

A rotation looks like it needs somewhere to put the answer. It does not: two reflections, 45° apart, do the whole job in place.

Step by step

  1. Rotate it 90°, with no second grid

    Turning this clockwise sends the top row down the right edge. The obvious way costs another n² of memory. Watch what happens if you refuse to allocate it.

  2. Fold across the diagonal: swap 2 and 4

    First, reflect the grid across its main diagonal. Every cell above the line trades places with its mirror below. One temporary variable is all you need.

  3. Swap 3 and 7

    The corners trade next. The diagonal never moves, because a cell at [i][i] is its own mirror. So this pass touches each pair once and never undoes itself.

  4. Swap 6 and 8, and the fold is done

    Three swaps for a 3×3. Only the cells strictly above the diagonal move. The grid is now the transpose, and that is not the rotation yet.

  5. Compare it to the answer you wanted

    The transpose is the rotation seen in a mirror. Right columns, right places, but each row runs backwards. Fixing that is a second reflection, across the vertical centre.

  6. Reverse row 0

    Reversing a row is the same swap along a different line. First with last, second with second-last, meeting in the middle. An odd row leaves its centre alone.

  7. Reverse row 1

    Each row is reversed on its own. A reversal only ever swaps a pair, and a pair needs one number held aside. That is why no second grid is needed.

  8. Reverse row 2

    The last row falls into place. Six swaps in total, one temporary variable, and the grid you started with is the grid you are finishing with.

  9. Two mirrors, 45° apart, make one 90° turn

    That is the geometry underneath. Two reflections always make a rotation, by twice the angle between the mirrors. Remember the folds and the index arithmetic follows.

Why the obvious approach costs another n²

Almost everyone reaches for a second grid first. It is the honest reading. A rotation moves every cell, so you need somewhere to put the answer while the original is still being read.

Stop thinking about where each cell goes. Think about which cells trade places. A swap needs no destination grid, only one temporary variable, and a rotation turns out to be nothing but a sequence of swaps.

The two folds, in order

  1. Transpose: swap every cell above the main diagonal with its mirror below. Rows become columns.
  2. Reverse each row: swap first with last, working inwards. That is the second mirror.

python

def rotate(m):
    n = len(m)
    # strictly above the diagonal
    for r in range(n):
        for c in range(r + 1, n):
            m[r][c], m[c][r] = m[c][r], m[r][c]
    for row in m:
        row.reverse()

Where it goes wrong

Counter-clockwise is the same idea with the second fold moved: transpose, then reverse each column instead of each row.

All snippets