Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

48. Rotate Image

Medium76.4% Acceptance
ArrayMathMatrix
Asked by:
C
Cisco
ProblemSolutions (12)VideosCompanies (10)Notes

Problem Statement

You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

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

Example 2:

Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

Constraints:

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

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
F
Facebook
M
Microsoft
A
Amazon
A
Apple
+5

Approach

The Rotate Image problem asks you to rotate an n x n matrix by 90 degrees clockwise in-place. Since modifying the matrix without using extra space is required, the key idea is to transform the matrix step by step rather than creating a new one.

A common and efficient strategy is a two-step transformation. First, transpose the matrix (swap matrix[i][j] with matrix[j][i]), which converts rows into columns. Then, reverse each row to achieve the final 90° clockwise rotation. This approach works because the transpose reflects the matrix across its diagonal, and the row reversal aligns elements into their rotated positions.

Another conceptual approach is the layer-by-layer rotation, where elements are rotated in groups of four while traversing each square layer of the matrix from the outside inward.

Both approaches visit each element a constant number of times, leading to O(n²) time complexity while maintaining O(1) extra space due to in-place operations.

Complexity

ApproachTime ComplexitySpace Complexity
Transpose + Reverse RowsO(n²)O(1)
Layer-by-Layer RotationO(n²)O(1)

Video Solution Available

take U forward

View all video solutions

Solutions (12)

Transpose and Reverse

This approach utilizes two main operations. First, we transpose the matrix, which means flipping it over its diagonal. In other words, swap matrix[i][j] with matrix[j][i]. After transposing the matrix, we reverse each row. This combination results in a 90-degree clockwise rotation.

Time Complexity: O(n^2) - Because we loop through each element once.
Space Complexity: O(1) - No extra space is used, operations are in-place.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2
3void rotate(int matrix[][3], int n) {
4    for (int i = 0; i < n; i++) {
5        for (int j = i; j < n; j++) {
6            int temp = matrix[i][j];
7            matrix[i][j] = matrix[j][i];
8            matrix[j][i] = temp;
9        }
10    }
11    for (int i = 0; i < n; i++) {
12        for (int j = 0; j < n/2; j++) {
13            int temp = matrix[i][j];
14            matrix[i][j] = matrix[i][n-j-1];
15            matrix[i][n-j-1] = temp;
16        }
17    }
18}
19
20int main() {
21    int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
22    rotate(matrix, 3);
23    for (int i = 0; i < 3; i++) {
24        for (int j = 0; j < 3; j++) {
25            printf("%d ", matrix[i][j]);
26        }
27        printf("\n");
28    }
29    return 0;
30}

Explanation

The code first transposes the given matrix in place using two nested loops. Then, for each row, it reverses the elements to achieve a 90-degree clockwise rotation.

Layer by Layer Rotation

This approach rotates the matrix layer by layer or ring by ring. Start from the outer layer and move to the inner layer, rotating elements by moving them in groups of four. This involves swapping the elements in four-step rotations.

Time Complexity: O(n^2) - Each individual element is moved once.
Space Complexity: O(1) - Done entirely in place, without additional memory.

CC++JavaPythonC#JavaScript
1#include

Video Solutions

Watch expert explanations and walkthroughs

Rotate Matrix/Image by 90 Degrees | Brute - Optimal

take U forward
17:47324,741 views

Asked By Companies

10 companies
C
Cisco
F
Facebook
M
Microsoft
A
Amazon
A
Apple
A
Adobe
G
Google
B
Bloomberg
R
Rubrik
U
Uber

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Plus OneEasy
Max Points on a LineHard
Evaluate Reverse Polish NotationMedium
Rotate ArrayMedium
More similar problems

Related Topics

ArrayMathMatrix

Problem Stats

Acceptance Rate76.4%
DifficultyMedium
Companies10

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Rotate Image asked in FAANG interviews?

Yes, Rotate Image is a popular medium-level matrix problem frequently asked in coding interviews, including FAANG-style interviews. It tests understanding of matrix transformations and in-place operations.

What is the optimal approach for Rotate Image?

The optimal approach is to transpose the matrix and then reverse each row. This method rotates the matrix 90 degrees clockwise in-place while keeping the time complexity O(n^2) and space complexity O(1).

Why do we transpose the matrix in Rotate Image?

Transposing swaps rows and columns across the main diagonal, which reorganizes the matrix structure. After transposition, reversing each row aligns elements into their correct rotated positions.

What data structure concept is used in Rotate Image?

The problem primarily uses matrix manipulation and array indexing. Understanding 2D arrays and in-place element swapping is essential to efficiently rotate the matrix.

Previous Problem

Permutations II

Next Problem

Group Anagrams

<stdio.h>
2
3
void
rotate
(
int
matrix
[
]
[
3
]
,
int
n
)
{
4
for
(
int
layer
=
0
;
layer
<
n
/
2
;
layer
++
)
{
5
int
first
=
layer
;
6
int
last
=
n
-
layer
-
1
;
7
for
(
int
i
=
first
;
i
<
last
;
i
++
)
{
8
int
offset
=
i
-
first
;
9
int
top
=
matrix
[
first
]
[
i
]
;
10
matrix
[
first
]
[
i
]
=
matrix
[
last
-
offset
]
[
first
]
;
11
matrix
[
last
-
offset
]
[
first
]
=
matrix
[
last
]
[
last
-
offset
]
;
12
matrix
[
last
]
[
last
-
offset
]
=
matrix
[
i
]
[
last
]
;
13
matrix
[
i
]
[
last
]
=
top
;
14
}
15
}
16
}
17
18
int
main
(
)
{
19
int
matrix
[
3
]
[
3
]
=
{
{
1
,
2
,
3
}
,
{
4
,
5
,
6
}
,
{
7
,
8
,
9
}
}
;
20
rotate
(
matrix
,
3
)
;
21
for
(
int
i
=
0
;
i
<
3
;
i
++
)
{
22
for
(
int
j
=
0
;
j
<
3
;
j
++
)
{
23
printf
(
"%d "
,
matrix
[
i
]
[
j
]
)
;
24
}
25
printf
(
"\n"
)
;
26
}
27
return
0
;
28
}

Explanation

The C solution processes the matrix in concentric layers, shifting each element appropriately to its new position during the 90-degree rotation.