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 layer by layer.

#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}
    };
    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