Java Program to Rotate Matrix Elements

Last Updated : 21 Aug, 2026

A matrix is a two-dimensional array arranged in rows and columns. Rotating matrix elements means shifting the elements of the matrix in a specified direction while preserving their relative order.

Example

Input:

7 8 9
10 11 12
2 3 4

Output:

4 1 2
7 5 3
8 9 6

Approach to Rotate Matrix Elements

The matrix can be divided into multiple layers or rings. We rotate each layer one at a time. For every layer:

  • Store an element temporarily to avoid losing it.
  • Move the elements of the top row.
  • Move the elements of the right column.
  • Move the elements of the bottom row.
  • Move the elements of the left column.
  • Move the boundaries inward and process the next layer.

Example: Program to Rotate Matrix Elements in Clockwise Direction

Java
class Geeks {

    // Function to rotate matrix elements
    static void rotateMatrix(int[][] mat) {

        int rows = mat.length;
        int cols = mat[0].length;

        int top = 0;
        int bottom = rows - 1;
        int left = 0;
        int right = cols - 1;

        while (top < bottom && left < right) {

            // Store the first element of the next row
            int prev = mat[top + 1][left];

            // Move top row elements
            for (int i = left; i <= right; i++) {
                int curr = mat[top][i];
                mat[top][i] = prev;
                prev = curr;
            }
            top++;

            // Move right column elements
            for (int i = top; i <= bottom; i++) {
                int curr = mat[i][right];
                mat[i][right] = prev;
                prev = curr;
            }
            right--;

            // Move bottom row elements
            for (int i = right; i >= left; i--) {
                int curr = mat[bottom][i];
                mat[bottom][i] = prev;
                prev = curr;
            }
            bottom--;

            // Move left column elements
            for (int i = bottom; i >= top; i--) {
                int curr = mat[i][left];
                mat[i][left] = prev;
                prev = curr;
            }
            left++;
        }
    }

    // Function to print the matrix
    static void printMatrix(int[][] mat) {

        for (int[] row : mat) {
            for (int value : row) {
                System.out.print(value + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {

        int[][] mat = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        rotateMatrix(mat);
        printMatrix(mat);
    }
}

Output
4 1 2 
7 5 3 
8 9 6 

Explanation

  • top, bottom, left, and right represent the boundaries of the current layer.
  • prev temporarily stores an element while shifting the elements.
  • The four loops rotate the top row, right column, bottom row, and left column.
  • After completing one layer, the boundaries are moved inward.
  • The process continues until all layers are rotated.

Approach

The idea is to rotate each row of the matrix to the right by K positions.

  • Treat each row as a separate one-dimensional array.
  • Calculate K % P to handle cases where K is greater than the number of columns.
  • Store the first P - K elements of the current row in a temporary array.
  • Move the last K elements to the beginning of the row.
  • Copy the stored elements to the remaining positions.
  • Repeat the process for every row.

Example: For a given matrix of size P×Q, we need to rotate its elements layer-wise in a clockwise direction by K times to the right side, where K is a given number.

Java
// Main Class
public class Geeks
{
    // Dimension of the matrix

    // Initializing to custom values
    static final int P = 3;
    static final int Q = 3;

    // Method 1
    // To rotate the stated matrix by K times
    static void rotate_Matrix(int mat[][], int K)
    {
        // Using temporary array of dimension P
        int tempo[] = new int[P];

        // Rotating matrix by k times 
        // across the size of matrix
        K = K % P;

        for (int j = 0; j < Q; j++) {

            // Copying first P-K elements
            // to the temporary array
            for (int l = 0; l < P - K; l++)
                tempo[l] = mat[j][l];

            // Copying the elements of the matrix
            // from K to the end to the starting
            for (int x = P - K; x < P; x++)
                mat[j][x - P + K] = mat[j][x];

            // Copying the elements of the matrix
            // from the temporary array to end
            for (int x = K; x < P; x++)
                mat[j][x] = tempo[x - K];
        }
    }

    // Method 2
    // To show the resultant matrix
    static void show_Matrix(int mat[][])
    {
        for (int j = 0; j < Q; j++) {
            for (int x = 0; x < P; x++)
                System.out.print(mat[j][x] + " ");
            System.out.println();
        }
    }

    // Method 3
    // Main driver method
    public static void main(String[] args)
    {
        // Custom input array
        int mat[][]
            = { { 1, 2, 5 }, { 3, 4, 6 }, { 8, 10, 9 } };

        // Custom value of K
        int K = 2;

        // Calling the above created method for
        // rotating matrix by k times
        rotate_Matrix(mat, K);

        // Calling the above method for
        // displaying rotated matrix
        show_Matrix(mat);
    }
}
Try It Yourself
redirect icon

Output
2 5 1 
4 6 3 
10 9 8 

Explanation

  • K = K % P ensures the number of rotations stays within the row length.
  • tempo[] temporarily stores the elements that will be moved to the end.
  • For each row, the last K elements are moved to the beginning.
  • The remaining elements are then placed after them.
  • Thus, every row is rotated independently to the right by K positions.
Comment