C Program to Rotate Matrix 90 Degrees Clockwise
rotated[j][n-1-i] = original[i][j] for each element, where n is matrix size.Examples
How to Think About It
i and column j moves to row j and column n-1-i in the rotated matrix.Algorithm
Code
#include <stdio.h> int main() { int n = 3; int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; int rotated[3][3]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { rotated[j][n - 1 - i] = matrix[i][j]; } } printf("Rotated matrix:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%d ", rotated[i][j]); } printf("\n"); } return 0; }
Dry Run
Let's trace the 3x3 matrix [[1,2,3],[4,5,6],[7,8,9]] through the rotation code.
Start with original matrix
[[1,2,3],[4,5,6],[7,8,9]]
Assign rotated[j][n-1-i] = matrix[i][j]
For i=0, j=0: rotated[0][2] = 1 For i=0, j=1: rotated[1][2] = 2 For i=0, j=2: rotated[2][2] = 3 For i=1, j=0: rotated[0][1] = 4 For i=1, j=1: rotated[1][1] = 5 For i=1, j=2: rotated[2][1] = 6 For i=2, j=0: rotated[0][0] = 7 For i=2, j=1: rotated[1][0] = 8 For i=2, j=2: rotated[2][0] = 9
Resulting rotated matrix
[[7,4,1],[8,5,2],[9,6,3]]
| i,j | rotated[j][n-1-i] | value assigned |
|---|---|---|
| 0,0 | rotated[0][2] | 1 |
| 0,1 | rotated[1][2] | 2 |
| 0,2 | rotated[2][2] | 3 |
| 1,0 | rotated[0][1] | 4 |
| 1,1 | rotated[1][1] | 5 |
| 1,2 | rotated[2][1] | 6 |
| 2,0 | rotated[0][0] | 7 |
| 2,1 | rotated[1][0] | 8 |
| 2,2 | rotated[2][0] | 9 |
Why This Works
Step 1: Mapping positions
Each element at position (i, j) in the original matrix moves to (j, n-1-i) in the rotated matrix, effectively swapping rows and columns with a reversed row index.
Step 2: Using nested loops
Two loops go through every element so we can assign each value to its new rotated position.
Step 3: Creating a new matrix
We use a separate matrix to store rotated values to avoid overwriting original data during the process.
Alternative Approaches
#include <stdio.h> int main() { int n = 3; int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; // 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 each row for (int i = 0; i < n; i++) { for (int j = 0; j < n / 2; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[i][n - 1 - j]; matrix[i][n - 1 - j] = temp; } } printf("Rotated matrix:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } return 0; }
#include <stdio.h> int main() { int n = 3; int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; int rotated[3][3]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { rotated[n - 1 - j][i] = matrix[i][j]; } } printf("Rotated matrix (90 degrees CCW):\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%d ", rotated[i][j]); } printf("\n"); } return 0; }
Complexity: O(n^2) time, O(n^2) space
Time Complexity
The program uses two nested loops each running n times, so it processes n*n elements, resulting in O(n^2) time.
Space Complexity
The main approach uses an extra matrix of the same size to store rotated values, so space complexity is O(n^2). The in-place method reduces space to O(1).
Which Approach is Fastest?
Both methods run in O(n^2) time, but the in-place rotation saves memory at the cost of more complex code.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Using extra matrix | O(n^2) | O(n^2) | Simplicity and clarity |
| In-place transpose + reverse | O(n^2) | O(1) | Memory efficiency and large matrices |
| Counterclockwise rotation | O(n^2) | O(n^2) | Different rotation direction |