Java Program to Rotate a Square Matrix by 90 Degrees

Rotating a matrix by 90 degrees is a common problem encountered in programming. It involves moving elements of a matrix in a way that the rows of the original matrix become columns of the rotated matrix.

In this article, we will learn how to rotate a matrix by 90 degrees clockwise in Java. We'll first discuss the basic logic behind the rotation and then implement the solution step by step.

Given a square matrix of size n x n, the task is to rotate the matrix by 90 degrees in a clockwise direction. The process can be described as follows.

  1. Transpose the matrix: Swap rows with columns.
  2. Reverse each row: After transposing the matrix, reverse each row to get the rotated matrix.

Steps to Rotate the Matrix

  1. Transpose the matrix: Swap matrix[i][j] with matrix[j][i].
  2. Reverse each row: For each row, reverse the elements.

Java Program Implementation

Here is a simple Java program to rotate a matrix by 90 degrees in a clockwise direction.

public class RotateMatrix {

    // Function to rotate the matrix by 90 degrees clockwise
    public static void rotateMatrix(int[][] matrix) {
        int n = matrix.length;

        // Step 1: Transpose the matrix (swap matrix[i][j] with matrix[j][i])
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                // Swap elements at (i, j) and (j, i)
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }

        // Step 2: Reverse each row
        for (int i = 0; i < n; i++) {
            int start = 0;
            int end = n - 1;
            while (start < end) {
                // Swap elements at (i, start) and (i, end)
                int temp = matrix[i][start];
                matrix[i][start] = matrix[i][end];
                matrix[i][end] = temp;

                // Move towards the center
                start++;
                end--;
            }
        }
    }

    // Function to print the matrix
    public static void printMatrix(int[][] matrix) {
        int n = matrix.length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }

    // Main function to test the rotation
    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        System.out.println("Original Matrix:");
        printMatrix(matrix);

        // Rotate the matrix by 90 degrees
        rotateMatrix(matrix);

        System.out.println("\nRotated Matrix by 90 degrees:");
        printMatrix(matrix);
    }
}

Explanation of the Code

  1. rotateMatrix Function
    • First, the function transposes the matrix by swapping the elements at position (i, j) with (j, i).
    • Then, the function reverses each row of the transposed matrix by swapping elements at the start and end of each row.
  2. printMatrix Function
    • This function is used to print the matrix in a readable format. It loops through each element of the matrix and prints it.
  3. main Function
    • In the main function, we define a sample matrix, print the original matrix, rotate it by 90 degrees, and then print the rotated matrix.

Output

When you run the program, the output will be

Conclusion

In this article, we've implemented a simple Java program to rotate a matrix by 90 degrees clockwise. The process involves transposing the matrix and then reversing each row. This is a commonly asked problem in coding interviews, and mastering it helps improve your understanding of matrix manipulation.


Similar Articles