Rotate Image

HardAcc. 72.1%
+50 XP 25

The Layer Rotation (90°)

Rotating a digital image is one of the most common operations in graphics programming. To rotate a square grid 90 degrees clockwise without using extra memory, we perform a two-step "Spatial Flip": first, we flip the grid over its diagonal, then we reverse each individual row.

The Assignment

Your mission is to rotate the image in-place. Your function receives an $N imes N$ matrix.

  1. Step 1 (Transpose): Flip the matrix across its main diagonal (swap matrix[i][j] with matrix[j][i]).
  2. Step 2 (Reverse): Reverse each row of the matrix from left to right.
  3. Print the final rotated matrix row-by-row, with elements in each row separated by spaces.

01EXAMPLE 1

Input[[1,2],[3,4]]
Output3 1 4 2

Explanation: [1,2] goes to col 1. [3,4] goes to col 0.

Constraints

  • Assume a square matrix.
ArraysMatrix
JavaScript
Loading...
1 Hidden

Input Arguments

matrix[[1,2],[3,4]]

Expected Output

3 1
4 2

Click RUN to test your solution