Rotate Image

java dev.to

Matrix problems often seem tricky because multiple rows and columns move at the same time. In this problem, we need to rotate an n × n matrix by 90 degrees clockwise without using any extra matrix.

The key insight is that we don't need to perform the rotation directly. Instead, we can break it down into two simple operations that are easy to perform in-place.


Problem Statement

Given an n x n matrix representing an image, rotate the image by 90 degrees clockwise.

Example

Input:

1 2 3
4 5 6
7 8 9
Enter fullscreen mode Exit fullscreen mode

Output:

7 4 1
8 5 2
9 6 3
Enter fullscreen mode Exit fullscreen mode

Brute Force Approach

The most intuitive solution is to create a new matrix and place every element at its rotated position.

For every element:

rotated[j][n - 1 - i] = matrix[i][j];
Enter fullscreen mode Exit fullscreen mode

Complexity

  • Time Complexity: O(N²)
  • Space Complexity: O(N²)

This solution works, but it uses an extra matrix. Interviewers usually expect an in-place solution.


The Key Observation

Instead of rotating directly, we can achieve the same result in two steps:

Step 1: Transpose the Matrix

Swap rows and columns.

Original Matrix

1 2 3
4 5 6
7 8 9
Enter fullscreen mode Exit fullscreen mode

⬇️ Transpose

1 4 7
2 5 8
3 6 9
Enter fullscreen mode Exit fullscreen mode

Step 2: Reverse Every Row

1 4 7
2 5 8
3 6 9
Enter fullscreen mode Exit fullscreen mode

⬇️ Reverse Each Row

7 4 1
8 5 2
9 6 3
Enter fullscreen mode Exit fullscreen mode

And that's exactly our rotated matrix.


Why Does This Work?

When we transpose a matrix, every element gets mirrored across the main diagonal.

After that, reversing each row shifts the elements into their correct positions for a 90-degree clockwise rotation.

A useful interview pattern to remember:

90° Clockwise = Transpose + Reverse Rows

90° Anti-Clockwise = Transpose + Reverse Columns

Once you remember this pattern, matrix rotation problems become much easier.


Optimal Java Solution

class Solution {
    public void rotate(int[][] matrix) {
        int n = matrix.length;

        // Transpose the matrix
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }

        // Reverse every row
        for (int i = 0; i < n; i++) {
            int left = 0;
            int right = n - 1;

            while (left < right) {
                int temp = matrix[i][left];
                matrix[i][left] = matrix[i][right];
                matrix[i][right] = temp;

                left++;
                right--;
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Dry Run

Input:

1 2 3
4 5 6
7 8 9
Enter fullscreen mode Exit fullscreen mode

After Transpose:

1 4 7
2 5 8
3 6 9
Enter fullscreen mode Exit fullscreen mode

After Reversing Rows:

7 4 1
8 5 2
9 6 3
Enter fullscreen mode Exit fullscreen mode

Final Answer


Complexity Analysis

  • Time Complexity: O(N²)
  • Space Complexity: O(1)

Since we modify the matrix directly without creating another matrix, this is the optimal solution.


Key Interview Takeaway

The biggest lesson from this problem is that complex matrix transformations can often be broken into simpler operations.

Instead of memorizing the rotation itself, remember this pattern:

Transpose → Reverse Rows → Rotate 90° Clockwise

This is the approach most interviewers expect and a pattern that appears in many matrix-related questions.


Connect With Me

I'm currently solving the Striver SDE Sheet Challenge and documenting my learnings, patterns, mistakes, and interview insights along the way.

🔗 LinkedIn: https://www.linkedin.com/in/jaspreetsodhi482/

🐦 X (Twitter): https://x.com/Jaspree54799902

💻 GitHub: https://github.com/codewithjaspreet

Source: dev.to

arrow_back Back to Tutorials