← All coding problems
Rotate Image
mediumArrays & Hashing+20 XPTime limit 2s
Rotate a square n × n image (matrix) by 90 degrees clockwise.
Input:
- line 1:
n n - next
nlines:nintegers each
Output: the rotated matrix, one row per line.
Constraints: 1 ≤ n ≤ 300, −1000 ≤ value ≤ 1000.
Challenge: do it in place — transpose the matrix (swap a[i][j] with a[j][i]), then reverse every row.
Sample input 1
3 3 1 2 3 4 5 6 7 8 9
Sample output 1
7 4 1 8 5 2 9 6 3
Sample input 2
4 4 5 1 9 11 2 4 8 10 13 3 6 7 15 14 12 16
Sample output 2
15 13 2 5 14 3 4 1 12 6 8 9 16 7 10 11
📖 Stuck? Learn the pattern
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).