Rotating the layers of a matrix moves every number one step clockwise around its own ring. Starting from the grid 1 to 16, the outer ring shifts round by one place and so does the inner ring of four numbers.
Quick answer: Treat the matrix as rings. For each ring, keep the value that will move into the top-left corner in prev, then walk the ring clockwise, swapping prev with each cell. After one lap every value has moved one place clockwise.
This program is part of the Spiral Patterns collection.
Table of contents8 sections
Rotate Matrix Layers by One output
Output (n = 4):
5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12

How the rotate matrix layers by one works
Each ring is a loop of cells, top row, right column, bottom row and left column. Rotating it by one means every value takes the place of its clockwise neighbour. Carrying one value in prev and swapping it into each cell in turn does that without a second array: each swap drops the carried value in and picks up the old one to carry on. The ring starts with the value just below the top-left corner, because that is the one that moves into the corner.
- Fill the matrix with 1 to
n². - For each ring, set
prevto the cell just below its top-left corner. - Walk the ring clockwise (top row, right column, bottom row, left column), swapping
prevwith each cell. - Print the matrix row by row.
Key takeaways
- A matrix of size
nhasn / 2rings. - Carrying one value and swapping rotates a ring in place, with no extra array.
- The walk starts with the value that will land in the top-left corner.
- Repeat the rotation
ktimes to rotate bykplaces.
Rotate Matrix Layers by One in C
#include <stdio.h>
int main(void) {
int n = 4;
int a[4][4];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = i * n + j + 1;
for (int layer = 0; layer < n / 2; layer++) {
int top = layer, left = layer, bottom = n - 1 - layer, right = n - 1 - layer;
int prev = a[top + 1][left]; /* this value moves into the top-left corner */
for (int j = left; j <= right; j++) { /* top row, left to right */
int t = a[top][j]; a[top][j] = prev; prev = t;
}
for (int i = top + 1; i <= bottom; i++) { /* right column, downwards */
int t = a[i][right]; a[i][right] = prev; prev = t;
}
for (int j = right - 1; j >= left; j--) { /* bottom row, right to left */
int t = a[bottom][j]; a[bottom][j] = prev; prev = t;
}
for (int i = bottom - 1; i > top; i--) { /* left column, upwards */
int t = a[i][left]; a[i][left] = prev; prev = t;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
printf("%3d", a[i][j]);
printf("\n");
}
return 0;
}Rotate Matrix Layers by One in C++
#include <iostream>
#include <iomanip>
#include <vector>
#include <utility>
using namespace std;
int main() {
int n = 4;
vector<vector<int>> a(n, vector<int>(n));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = i * n + j + 1;
for (int layer = 0; layer < n / 2; layer++) {
int top = layer, left = layer, bottom = n - 1 - layer, right = n - 1 - layer;
int prev = a[top + 1][left]; // this value moves into the top-left corner
for (int j = left; j <= right; j++) // top row, left to right
swap(prev, a[top][j]);
for (int i = top + 1; i <= bottom; i++) // right column, downwards
swap(prev, a[i][right]);
for (int j = right - 1; j >= left; j--) // bottom row, right to left
swap(prev, a[bottom][j]);
for (int i = bottom - 1; i > top; i--) // left column, upwards
swap(prev, a[i][left]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << setw(3) << a[i][j];
cout << endl;
}
return 0;
}Rotate Matrix Layers by One in Java
public class MatrixLayerRotation {
public static void main(String[] args) {
int n = 4;
int[][] a = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = i * n + j + 1;
for (int layer = 0; layer < n / 2; layer++) {
int top = layer, left = layer, bottom = n - 1 - layer, right = n - 1 - layer;
int prev = a[top + 1][left]; // this value moves into the top-left corner
for (int j = left; j <= right; j++) { // top row, left to right
int t = a[top][j]; a[top][j] = prev; prev = t;
}
for (int i = top + 1; i <= bottom; i++) { // right column, downwards
int t = a[i][right]; a[i][right] = prev; prev = t;
}
for (int j = right - 1; j >= left; j--) { // bottom row, right to left
int t = a[bottom][j]; a[bottom][j] = prev; prev = t;
}
for (int i = bottom - 1; i > top; i--) { // left column, upwards
int t = a[i][left]; a[i][left] = prev; prev = t;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
System.out.printf("%3d", a[i][j]);
System.out.println();
}
}
}Rotate Matrix Layers by One in Python
n = 4
a = [[i * n + j + 1 for j in range(n)] for i in range(n)]
for layer in range(n // 2):
top, left, bottom, right = layer, layer, n - 1 - layer, n - 1 - layer
prev = a[top + 1][left] # this value moves into the top-left corner
for j in range(left, right + 1): # top row, left to right
a[top][j], prev = prev, a[top][j]
for i in range(top + 1, bottom + 1): # right column, downwards
a[i][right], prev = prev, a[i][right]
for j in range(right - 1, left - 1, -1): # bottom row, right to left
a[bottom][j], prev = prev, a[bottom][j]
for i in range(bottom - 1, top, -1): # left column, upwards
a[i][left], prev = prev, a[i][left]
for row in a:
print("".join(f"{x:3d}" for x in row))Time and space complexity
Every cell is swapped once, so a single rotation takes O(n²) time and O(1) extra memory.
Frequently asked questions
How do I rotate anticlockwise?
Start with prev set to the cell just right of the top-left corner and walk the ring anticlockwise: down the left column, along the bottom, up the right column and back along the top.
How is this different from rotating the whole matrix by 90 degrees?
A 90-degree rotation moves each value a quarter of the way round its ring. This rotation moves it by one cell only, which is the version asked in HackerRank’s “Matrix Layer Rotation” problem.
How do I rotate by more than one step quickly?
Copy each ring into a list, rotate the list by k positions with slicing or modulo arithmetic, and write it back. That avoids repeating the one-step rotation k times.






