Rotate Every Rectangular Ring. Given the even-by-even integer matrix grid, rotate every concentric rectangular layer k steps counterclockwise. In one step each entry moves to the neighboring position along its own layer in the counterclockwise direction. Return the resulting matrix.
### Problem
Apply `k` counterclockwise rotations to every concentric rectangular layer of the even-by-even matrix `grid`, and return the resulting matrix. In one rotation, each value moves to the adjacent position in the counterclockwise direction along its own layer. Values never move between layers.
```cpp
class Solution {
public:
vector<vector<int>> rotateGrid(vector<vector<int>>& grid, int k);
};
```
### Examples
**Example 1:**
```text
grid = [[1,2],[3,4]], k = 1
Output: [[2,4],[1,3]]
Explanation: The top-left entry moves down along the left edge.
```
**Example 2:**
```text
grid = [[2,5],[7,9]], k = 2
Output: [[9,7],[5,2]]
Explanation: Two counterclockwise steps move every entry to the opposite ring position.
```
### Constraints
- `m == grid.length`, `n == grid[i].length`; `2 <= m,n <= 50`.
- Both dimensions are even.
- `1 <= grid[i][j] <= 5000`; `1 <= k <= 10^9`.
### Clarifying Questions
A layer is the boundary of a submatrix remaining after zero or more outer boundaries are removed. Each layer rotates independently.
```hint Inspect the structure
How can a ring be indexed once so that a large rotation becomes modular arithmetic?
```
### Reasoning checkpoints
- Match the stated counterclockwise movement on every layer, including corners and inner layers.
- Account for very large rotation counts and for layers with different perimeters.
### Optional extensions
What special handling would odd dimensions require for a remaining central row or column?
Overview: Rotate Every Rectangular Ring. Given the even-by-even integer matrix grid, rotate every concentric rectangular layer k steps counterclockwise. In one step each entry moves to the neighboring position along its own layer in the counterclockwise direction. Return the resulting matrix.
Apply k counterclockwise rotations to every concentric rectangular layer of the even-by-even matrix grid, and return the resulting matrix. In one rotation, each value moves to the adjacent position in the counterclockwise direction along its own layer. Values never move between layers.
class Solution {
public:
vector<vector<int>> rotateGrid(vector<vector<int>>& grid, int k);
};
Examples
Example 1:
grid = [[1,2],[3,4]], k = 1
Output: [[2,4],[1,3]]
Explanation: The top-left entry moves down along the left edge.
Example 2:
grid = [[2,5],[7,9]], k = 2
Output: [[9,7],[5,2]]
Explanation: Two counterclockwise steps move every entry to the opposite ring position.
Constraints
m == grid.length
,
n == grid[i].length
;
2 <= m,n <= 50
.
Both dimensions are even.
1 <= grid[i][j] <= 5000
;
1 <= k <= 10^9
.
Clarifying Questions Guidance
A layer is the boundary of a submatrix remaining after zero or more outer boundaries are removed. Each layer rotates independently.
Reasoning checkpoints
Match the stated counterclockwise movement on every layer, including corners and inner layers.
Account for very large rotation counts and for layers with different perimeters.
Optional extensions
What special handling would odd dimensions require for a remaining central row or column?