MediumArrays · Matrix

Rotate Image

Given an n × n matrix, rotate it 90° clockwise in place: change matrix itself without allocating another 2D matrix.

[[1,2],[3,4]] → [[3,1],[4,2]][[1,2,3],[4,5,6],[7,8,9]] → [[7,4,1],[8,5,2],[9,6,3]]

Try:
CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Variables

The data structures the code is building

Matrix

Values slide from the cell they are read from to the cell they are written to

Time and space complexity

The matrix is n × n. The highlighted row is the solution being traced.

Keyboard: ← → step, Space play or pause. Enter a square matrix up to 6 × 6 like [[1,2],[3,4]]. Values fly between cells when you step forward one step at a time.