Cyclically Rotating a Grid - Problem

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

example_1.py β€” Basic 4Γ—4 Matrix
$ 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]]
πŸ’‘ Note: Layer 1: [1,2,3,4,8,12,16,15,14,13,9,5] rotates 2 positions to [3,4,8,12,16,15,14,13,9,5,1,2]. Layer 2: [6,7,11,10] rotates 2 positions to [11,10,6,7]
example_2.py β€” Rectangular Matrix
$ Input: grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
β€Ί Output: [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]]
πŸ’‘ Note: Layer 1 has 12 elements, k=4 means 4 positions rotation. Layer 2 has 4 elements, k=4 means full rotation (back to original). Both layers return to original positions
example_3.py β€” Large Rotation Value
$ Input: grid = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], k = 100
β€Ί Output: [[9,5,1,2],[13,7,6,3],[14,11,10,4],[15,16,12,8]]
πŸ’‘ Note: Layer 1 has 12 elements: 100 % 12 = 4 effective rotations. Layer 2 has 4 elements: 100 % 4 = 0 effective rotations (stays same). Optimization prevents 100 actual rotations

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

Visualization

Tap to expand
Original Matrix12348121615141395671110After k=2 Rotations34812161514139512111067Counter-clockwise rotationOuter layerInner layer
Understanding the Visualization
1
Layer Identification
Identify concentric rectangular layers in the matrix
2
Element Extraction
Extract elements from each layer in counter-clockwise order
3
Modular Rotation
Use k % layer_size to find effective rotations needed
4
Element Placement
Place rotated elements back into their new positions
Key Takeaway
🎯 Key Insight: Each layer rotates independently as a circular array. Using modular arithmetic (k % layer_size) eliminates unnecessary full rotations, making the solution efficient even for very large k values.
Asked in
Google 25 Amazon 20 Meta 15 Microsoft 12
22.0K Views
Medium Frequency
~25 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen