Description
You are given an m x n integer matrix grid, where m and n are both even integers, and an integer k.
The matrix is composed of several layers, which is shown in the below image, where each color is its own layer:

A cyclic rotation of the matrix is done by cyclically rotating each layer in the matrix. To cyclically rotate a layer once, each element in the layer will take the place of the adjacent element in the counter-clockwise direction. An example rotation is shown below:
Return the matrix after applying k cyclic rotations to it.
Example 1:
Input: grid = [[40,10],[30,20]], k = 1 Output: [[10,20],[40,30]] Explanation: The figures above represent the grid at every state.
Example 2:
Input: grid = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], k = 2 Output: [[3,4,8,12],[2,11,10,16],[1,7,6,15],[5,9,13,14]] Explanation: The figures above represent the grid at every state.
Constraints:
m == grid.lengthn == grid[i].length2 <= m, n <= 50- Both
mandnare even integers. 1 <= grid[i][j] <= 50001 <= k <= 109
Solutions
This code rotates the elements within each concentric layer of a matrix by k positions. It works in three phases: first, it extracts each layer (the outermost ring, then the next ring inward, etc.) into a flat array by traversing the perimeter clockwise (top row → right column → bottom row → left column); second, it rotates each layer's elements by shifting them k positions to the left using slice and concat; and third, it places the rotated elements back into the result grid by traversing the same clockwise path. The key insight is that k % layers[i].length handles the case where k is larger than the layer size by using the modulo operator, effectively repeating the rotation pattern.
/**
* @param {number[][]} grid
* @param {number} k
* @return {number[][]}
*/
var rotateGrid = function(grid, k) {
const m = grid.length;
const n = grid[0].length;
const halfLen = Math.min(m / 2, n / 2);
const layers = Array.from({ length: halfLen }, () => []);
const res = Array.from({ length: m }, () => Array(n));
for (let layer = 0; layer < halfLen; layer++) {
let row = layer;
let col = layer;
for (; col < n - layer; col++) {
layers[layer].push(grid[row][col]);
}
col--;
row++;
for (; row < m - layer; row++) {
layers[layer].push(grid[row][col]);
}
row--;
col--;
for (; col >= layer; col--) {
layers[layer].push(grid[row][col]);
}
col++;
row--;
for (; row > layer; row--) {
layers[layer].push(grid[row][col]);
}
}
for (let i = 0; i < layers.length; i++) {
const split = k % layers[i].length;
layers[i] = layers[i].slice(split).concat(layers[i].slice(0, split));
}
for (let layer = 0; layer < halfLen; layer++) {
let row = layer;
let col = layer;
let pos = 0;
for (; col < n - layer; col++) {
res[row][col] = layers[layer][pos];
pos++;
}
col--;
row++;
for (; row < m - layer; row++) {
res[row][col] = layers[layer][pos];
pos++;
}
row--;
col--;
for (; col >= layer; col--) {
res[row][col] = layers[layer][pos];
pos++;
}
col++;
row--;
for (; row > layer; row--) {
res[row][col] = layers[layer][pos];
pos++;
}
}
return res;
};