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