Rotate Matrix by 90 Degrees

Understanding the Problem

The goal is to rotate a given square matrix by 90 degrees clockwise.

Method 1: Using a Temporary Matrix

This method uses a temporary matrix to store the rotated values.

#include <iostream>
using namespace std;

void rotateMatrix(int matrix[][100], int n) {
    int temp[100][100];

    // Store the rotated values in a temporary matrix
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            temp[j][n - 1 - i] = matrix[i][j];
        }
    }

    // Copy the temporary matrix back to the original matrix
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            matrix[i][j] = temp[i][j];
        }
    }
}

void printMatrix(int matrix[][100], int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
}

int main() {
    int matrix[100][100] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    int n = 3;
    rotateMatrix(matrix, n);
    cout << "Rotated Matrix:\n";
    printMatrix(matrix, n);
    return 0;
}
            

Output:

Rotated Matrix:
7 4 1 
8 5 2 
9 6 3 

Method 2: In-Place Rotation

This method rotates the matrix in place without using extra space.

#include <iostream>
using namespace std;

void rotateMatrix(int matrix[][100], int n) {
    // Transpose the matrix
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            swap(matrix[i][j], matrix[j][i]);
        }
    }

    // Reverse each row
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n / 2; j++) {
            swap(matrix[i][j], matrix[i][n - 1 - j]);
        }
    }
}

void printMatrix(int matrix[][100], int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
}

int main() {
    int matrix[100][100] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    int n = 3;
    rotateMatrix(matrix, n);
    cout << "Rotated Matrix:\n";
    printMatrix(matrix, n);
    return 0;
}
            

Output:

Rotated Matrix:
7 4 1 
8 5 2 
9 6 3 

Method 3: Using Layer by Layer Rotation

This method rotates the matrix by working on concentric layers from outer to inner, swapping elements in groups of four.

#include <iostream>
using namespace std;

void rotateMatrix(int matrix[][100], int n) {
    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;
            // Save the top element
            int top = matrix[first][i];

            // Move left element to top
            matrix[first][i] = matrix[last - offset][first];

            // Move bottom element to left
            matrix[last - offset][first] = matrix[last][last - offset];

            // Move right element to bottom
            matrix[last][last - offset] = matrix[i][last];

            // Assign top element to right
            matrix[i][last] = top;
        }
    }
}

void printMatrix(int matrix[][100], int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
}

int main() {
    int matrix[100][100] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12},
        {13, 14, 15, 16}
    };
    int n = 4;
    cout << "Original Matrix:\n";
    printMatrix(matrix, n);
    
    rotateMatrix(matrix, n);
    cout << "\nRotated Matrix:\n";
    printMatrix(matrix, n);
    return 0;
}
            

Output:

Original Matrix:
1 2 3 4 
5 6 7 8 
9 10 11 12 
13 14 15 16 

Rotated Matrix:
13 9 5 1 
14 10 6 2 
15 11 7 3 
16 12 8 4 

Explanation:

This method works by:

  • Dividing the matrix into concentric layers (like an onion)
  • For each layer, rotating elements in groups of four
  • Moving elements from top → right → bottom → left → top
  • Working from the outermost layer inward
The algorithm has O(n²) time complexity (optimal for this problem) and O(1) space complexity as it performs the rotation in-place.

Key steps in the rotation:

  1. Save the top element
  2. Move left element to top position
  3. Move bottom element to left position
  4. Move right element to bottom position
  5. Move saved top element to right position
This process is repeated for all elements in the current layer before moving to the next inner layer.