ExamAdda Logo

Rotate Image

Easy

You are given an n × n 2D matrix representing an image. Rotate the image 90 degrees clockwise.

You must rotate the image in-place, meaning you must modify the input matrix directly without using another 2D matrix.

Example 1

Input

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

Output

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

Explanation

After rotating the matrix 90 degrees clockwise, the first column becomes the first row:

7 4 1

8 5 2

9 6 3

Example 2

Input

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

Output

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

Explanation

The elements move clockwise:

1 2       3 1

3 4   →   4 2

 

So the rotated matrix is [[3,1],[4,2]].

Constraints

n == matrix.length == matrix[i].length
1 <= n <= 20
-1000 <= matrix[i][j] <= 1000

Hints:

Hint 1

Transpose the matrix first, then reverse each row.
 

Hint 2

Both operations can be performed in-place without creating another matrix.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 18, 2026

Last updated

Aug 18, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output