Imagine you have a layered matrix like an onion - each layer can be peeled away to reveal the next inner layer. You're given an m × n integer matrix where both dimensions are even, and your task is to cyclically rotate each layer counter-clockwise by k positions.
What does this mean? Think of each layer as a circular track where elements move in a counter-clockwise direction. After k rotations, each element takes the position that was k steps ahead of it in the counter-clockwise direction.
Goal: Return the matrix after applying k cyclic rotations to each layer.
Visual Example: In a 4×4 matrix with layers colored differently:
🔴 Outer layer (border elements)
🔵 Inner layer (center 2×2)
Each layer rotates independently!
Input & Output
Constraints
-
m == grid.length -
n == grid[i].length -
2 ≤ m, n ≤ 300 -
4 ≤ m * n ≤ 3 * 104 - Both m and n are even integers
-
1 ≤ grid[i][j] ≤ 109 -
1 ≤ k ≤ 109