Sign in

Rotate a Matrix 90 Degrees

core

Implement rotate90(a), rotating an n x n matrix 90 degrees clockwise in place: after the call, a[i][j] must hold the value that was at a[n-1-j][i] before the call.

The standard technique is transpose, then reverse each row; do both steps, not just one.

Example: a = {{1,2},{3,4}} transposes to {{1,3},{2,4}}, then reversing each row gives {{3,1},{4,2}}, the final, rotated matrix.

Input format: each test in tests/*.in is laid out as:

  • Line 1: n, the side length of the square matrix.
  • Next n lines: one matrix row of n values each.

Test 01.in reads:

1
5

This is a 1 x 1 matrix; rotating it changes nothing.

Where you'll use it:

✦ Solution & editorial unlock with the pass.

Loading...