Java Array Program · A47
Rotate a Matrix by 90 Degrees in Java
A clockwise quarter turn is the same as a transpose (rows become columns) followed by reversing every row. Both steps work in place.
Problem Statement
Rotate an n × n matrix 90 degrees clockwise, in place, without creating a second matrix.
After rotation, the first column, read from bottom to top, becomes the first row.
Input/Output Examples
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]Output[[7, 4, 1], [8, 5, 2], [9, 6, 3]]Explanation: the left column 7, 4, 1 becomes the top row.[[1, 2], [3, 4]]Output[[3, 1], [4, 2]]Explanation: a quarter turn of four cells.[[7]]Output[[7]]Explanation: nothing moves.Visual Explanation
| rows | c0 | c1 | c2 |
|---|---|---|---|
| original | 1 2 3 | 4 5 6 | 7 8 9 |
| transposed | 1 4 7 | 2 5 8 | 3 6 9 |
| rows reversed | 7 4 1 | 8 5 2 | 9 6 3 |
Transposing mirrors the matrix across its main diagonal, and reversing the rows mirrors it left to right. Two mirror images combine into a rotation.
Approaches
Both approaches rotate in place. One combines two simple passes; the other moves four cells at a time ring by ring.
Approach 1: Transpose, Then Reverse Rows (Recommended)
Idea: swap m[i][j] with m[j][i] above the diagonal, then reverse each row.
- For
iandj > i, swap across the diagonal. - Reverse every row with two pointers.
- For counter-clockwise, reverse the columns instead of the rows.
Time: O(n²) · Space: O(1).
Approach 2: Ring-by-Ring Four-Way Swap
Idea: for each ring, move left → top, bottom → left, right → bottom and top → right in one step.
- Process rings from the outside in (
n / 2of them). - For each position on a ring, save the top cell.
- Rotate the four corresponding cells, using the saved value last.
Time: O(n²) · Space: O(1).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Transpose, Then Reverse Rows (Recommended)
Two simple, easy-to-check loops, which is why this is the usual interview answer.
import java.util.Arrays;
public class RotateMatrixTranspose {
// Clockwise rotation = transpose (swap across the diagonal) + reverse every row
static void rotate(int[][] m) {
int n = m.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int t = m[i][j];
m[i][j] = m[j][i];
m[j][i] = t;
}
}
for (int[] row : m) {
for (int l = 0, r = n - 1; l < r; l++, r--) {
int t = row[l];
row[l] = row[r];
row[r] = t;
}
}
}
public static void main(String[] args) {
int[][][] inputs = {{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, {{1, 2}, {3, 4}}, {{7}}};
for (int[][] m : inputs) {
String before = Arrays.deepToString(m);
rotate(m);
System.out.println(before + " -> " + Arrays.deepToString(m));
}
}
}
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] -> [[7, 4, 1], [8, 5, 2], [9, 6, 3]] [[1, 2], [3, 4]] -> [[3, 1], [4, 2]] [[7]] -> [[7]]
Time complexity: O(n²) · Space complexity: O(1).
Approach 2: Ring-by-Ring Four-Way Swap
Each cell is written exactly once, but the index arithmetic is easier to get wrong.
import java.util.Arrays;
public class RotateMatrixLayers {
// Rotates ring by ring; each step moves four cells at once: left -> top -> right -> bottom -> left
static void rotate(int[][] m) {
int n = m.length;
for (int layer = 0; layer < n / 2; layer++) {
int first = layer;
int last = n - 1 - layer;
for (int i = first; i < last; i++) {
int offset = i - first;
int top = m[first][i];
m[first][i] = m[last - offset][first]; // left -> top
m[last - offset][first] = m[last][last - offset]; // bottom -> left
m[last][last - offset] = m[i][last]; // right -> bottom
m[i][last] = top; // top -> right
}
}
}
public static void main(String[] args) {
int[][][] inputs = {{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, {{1, 2}, {3, 4}}, {{7}}};
for (int[][] m : inputs) {
String before = Arrays.deepToString(m);
rotate(m);
System.out.println(before + " -> " + Arrays.deepToString(m));
}
}
}
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] -> [[7, 4, 1], [8, 5, 2], [9, 6, 3]] [[1, 2], [3, 4]] -> [[3, 1], [4, 2]] [[7]] -> [[7]]
Time complexity: O(n²) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Transpose, Then Reverse Rows | O(n²) | O(1) |
| Ring-by-Ring Four-Way Swap | O(n²) | O(1) |
Here n is the side length. Both approaches touch each of the n² cells a constant number of times and use one temporary variable.
Interview Tips and Recommended Approach
- Know both directions. Clockwise: transpose + reverse rows. Counter-clockwise: transpose + reverse columns.
- Transpose only above the diagonal. Looping over every cell swaps each pair twice.
- The matrix must be square for an in-place rotation.
- Explain the ring method if asked for a single-pass solution.
Recommended Approach: Transpose, Then Reverse Rows
Why: it splits the rotation into two well-known operations, each easy to write and verify, with no extra memory.