Rotate Matrix Clockwise by One Step

Last Updated : 15 Jul, 2026

Given an n × m matrix mat[][], rotate all its elements by one position in clockwise, layer by layer (each rectangular layer (ring)) of the matrix should be rotated independently, return the modified matrix.

Examples:

Input: mat[][] = [[1, 2, 3], [2, 3, 3]]

151

Output: [[2, 1, 2], [3, 3, 3]]
Explanation:

2056958478

Input: mat[][] = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

154

Output: [[5, 1, 2, 3], [9, 10, 6, 4], [13, 11, 7, 8], [14, 15, 16, 12]]
Explanation:

2056958477
Try It Yourself
redirect icon

[Expected Approach ] - Using Layer Wise Rotation - O( n × m) Time and O(1) Space

The idea is to process the matrix one layer (ring) at a time. For each layer, we move all its boundary elements one step clockwise. Since moving one element can overwrite another, we first save its value in a temporary variable and keep updating it as we traverse the boundary. We repeat this for every layer from the outside to the inside.

  • Initialize the matrix boundaries (top, bottom, left, and right).
  • Process one layer of the matrix at a time from the outermost layer.
  • Store the first element of the next row in a temporary variable.
  • Move the boundary elements in the order: top row → right column → bottom row → left column.
  • Shrink the boundaries to move to the next inner layer.
  • Repeat until all layers are rotated and return the modified matrix.
C++
#include <iostream>
#include <vector>
using namespace std;

vector<vector<int>> rotateMatrix(vector<vector<int>> &mat)
{
    int m = mat.size();
    int n = mat[0].size();

    int row = 0, col = 0;
    int prev, curr;

    // Rotate each layer independently
    while (row < m && col < n)
    {

        // If there is only one row or one column left
        if (row + 1 == m || col + 1 == n)
            break;

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

        // Move elements of the top row
        for (int i = col; i < n; i++)
        {
            curr = mat[row][i];
            mat[row][i] = prev;
            prev = curr;
        }
        row++;

        // Move elements of the right column
        for (int i = row; i < m; i++)
        {
            curr = mat[i][n - 1];
            mat[i][n - 1] = prev;
            prev = curr;
        }
        n--;

        // Move elements of the bottom row
        if (row < m)
        {
            for (int i = n - 1; i >= col; i--)
            {
                curr = mat[m - 1][i];
                mat[m - 1][i] = prev;
                prev = curr;
            }
        }
        m--;

        // Move elements of the left column
        if (col < n)
        {
            for (int i = m - 1; i >= row; i--)
            {
                curr = mat[i][col];
                mat[i][col] = prev;
                prev = curr;
            }
        }
        col++;
    }

    return mat;
}

int main()
{
    vector<vector<int>> mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};

    vector<vector<int>> ans = rotateMatrix(mat);

    // Print the rotated matrix
    for (auto &row : ans)
    {
        for (int val : row)
            cout << val << " ";
        cout << endl;
    }

    return 0;
}
Java
import java.util.*;

public class GFG {
    static int[][] rotateMatrix(int[][] mat)
    {
        int m = mat.length;
        int n = mat[0].length;

        int row = 0, col = 0;
        int prev, curr;

        // Rotate each layer independently
        while (row < m && col < n) {

            // If there is only one row or one column left
            if (row + 1 == m || col + 1 == n)
                break;

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

            // Move elements of the top row
            for (int i = col; i < n; i++) {
                curr = mat[row][i];
                mat[row][i] = prev;
                prev = curr;
            }
            row++;

            // Move elements of the right column
            for (int i = row; i < m; i++) {
                curr = mat[i][n - 1];
                mat[i][n - 1] = prev;
                prev = curr;
            }
            n--;

            // Move elements of the bottom row
            if (row < m) {
                for (int i = n - 1; i >= col; i--) {
                    curr = mat[m - 1][i];
                    mat[m - 1][i] = prev;
                    prev = curr;
                }
            }
            m--;

            // Move elements of the left column
            if (col < n) {
                for (int i = m - 1; i >= row; i--) {
                    curr = mat[i][col];
                    mat[i][col] = prev;
                    prev = curr;
                }
            }
            col++;
        }

        return mat;
    }

    public static void main(String[] args)
    {
        int[][] mat = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 },
                        { 13, 14, 15, 16 } };

        int[][] ans = rotateMatrix(mat);

        // Print the rotated matrix
        for (int[] row : ans) {
            for (int val : row)
                System.out.print(val + " ");
            System.out.println();
        }
    }
}
Python
def rotateMatrix(mat):

    m = len(mat)
    n = len(mat[0])

    row = 0
    col = 0

    # Rotate each layer independently
    while row < m and col < n:

        # If there is only one row or one column left
        if row + 1 == m or col + 1 == n:
            break

        # Store the first element of the next row
        prev = mat[row + 1][col]

        # Move elements of the top row
        for i in range(col, n):
            curr = mat[row][i]
            mat[row][i] = prev
            prev = curr
        row += 1

        # Move elements of the right column
        for i in range(row, m):
            curr = mat[i][n - 1]
            mat[i][n - 1] = prev
            prev = curr
        n -= 1

        # Move elements of the bottom row
        if row < m:
            for i in range(n - 1, col - 1, -1):
                curr = mat[m - 1][i]
                mat[m - 1][i] = prev
                prev = curr
        m -= 1

        # Move elements of the left column
        if col < n:
            for i in range(m - 1, row - 1, -1):
                curr = mat[i][col]
                mat[i][col] = prev
                prev = curr
        col += 1

    return mat

# Driver Code

if __name__ == "__main__":
    mat = [
        [1, 2, 3, 4],
        [5, 6, 7, 8],
        [9, 10, 11, 12],
        [13, 14, 15, 16]
    ]

    ans = rotateMatrix(mat)

    # Print the rotated matrix
    for row in ans:
        for val in row:
            print val,
        print
C#
using System;

class GFG {
    static int[][] rotateMatrix(int[][] mat)
    {
        int m = mat.Length;
        int n = mat[0].Length;

        int row = 0, col = 0;
        int prev, curr;

        // Rotate each layer independently
        while (row < m && col < n) {
            // If there is only one row or one column left
            if (row + 1 == m || col + 1 == n)
                break;

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

            // Move elements of the top row
            for (int i = col; i < n; i++) {
                curr = mat[row][i];
                mat[row][i] = prev;
                prev = curr;
            }
            row++;

            // Move elements of the right column
            for (int i = row; i < m; i++) {
                curr = mat[i][n - 1];
                mat[i][n - 1] = prev;
                prev = curr;
            }
            n--;

            // Move elements of the bottom row
            if (row < m) {
                for (int i = n - 1; i >= col; i--) {
                    curr = mat[m - 1][i];
                    mat[m - 1][i] = prev;
                    prev = curr;
                }
            }
            m--;

            // Move elements of the left column
            if (col < n) {
                for (int i = m - 1; i >= row; i--) {
                    curr = mat[i][col];
                    mat[i][col] = prev;
                    prev = curr;
                }
            }
            col++;
        }

        return mat;
    }

    static void Main()
    {
        int[][] mat = { new int[] { 1, 2, 3, 4 },
                        new int[] { 5, 6, 7, 8 },
                        new int[] { 9, 10, 11, 12 },
                        new int[] { 13, 14, 15, 16 } };

        int[][] ans = rotateMatrix(mat);

        // Print the rotated matrix
        foreach(var row in ans)
        {
            foreach(var val in row)
                Console.Write(val + " ");
            Console.WriteLine();
        }
    }
}
JavaScript
function rotateMatrix(mat)
{
    let m = mat.length;
    let n = mat[0].length;

    let row = 0, col = 0;
    let prev, curr;

    // Rotate each layer independently
    while (row < m && col < n) {

        // If there is only one row or one column left
        if (row + 1 === m || col + 1 === n)
            break;

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

        // Move elements of the top row
        for (let i = col; i < n; i++) {
            curr = mat[row][i];
            mat[row][i] = prev;
            prev = curr;
        }
        row++;

        // Move elements of the right column
        for (let i = row; i < m; i++) {
            curr = mat[i][n - 1];
            mat[i][n - 1] = prev;
            prev = curr;
        }
        n--;

        // Move elements of the bottom row
        if (row < m) {
            for (let i = n - 1; i >= col; i--) {
                curr = mat[m - 1][i];
                mat[m - 1][i] = prev;
                prev = curr;
            }
        }
        m--;

        // Move elements of the left column
        if (col < n) {
            for (let i = m - 1; i >= row; i--) {
                curr = mat[i][col];
                mat[i][col] = prev;
                prev = curr;
            }
        }
        col++;
    }

    return mat;
}

// Driver Code
let mat = [
    [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ],
    [ 13, 14, 15, 16 ]
];

let ans = rotateMatrix(mat);

// Print the rotated matrix
for (const row of ans) {
    console.log(row.join(" "));
}

Output
5 1 2 3 
9 10 6 4 
13 11 7 8 
14 15 16 12 
Comment