Clockwise Rotation with Frozen Diagonals
Arrays & HashingAuto-graded · 18 test casesLast updated
Simulate clockwise 90° rotation on a square grid while keeping all main-diagonal and anti-diagonal elements fixed in place.
You are given an n × n square matrix and an integer k representing the number of clockwise 90-degree rotations to perform. However, certain cells must remain frozen: any cell that lies on the main diagonal or the anti-diagonal stays in place throughout all rotations.
A cell at position (row, col) (zero-indexed) is considered frozen if:
row == col(main diagonal), orrow + col == n - 1(anti-diagonal)
For each rotation, every non-frozen cell at position (row, col) moves to position (col, n - 1 - row), exactly as it would in a standard clockwise 90-degree matrix rotation. Frozen cells never move and retain their original values.
Perform exactly k rotations and return the resulting matrix.
Constraints
1 ≤ n ≤ 1001 ≤ k ≤ 4- All matrix elements are integers in the range
[-1000000000, 1000000000]
Examples
Example 1
Input: rotate_with_frozen_diagonals([[1, 2, 3, 4, 5], [2, 1, 9, 6, 3], [7, 0, 4, 8, 1], [5, 2, 4, 1, 9], [6, 4, 3, 2, 1]], 1)
Output: [[1, 5, 7, 2, 5], [4, 1, 0, 6, 2], [3, 4, 4, 9, 3], [2, 2, 8, 1, 4], [6, 9, 1, 3, 1]]
Example 2
Input: rotate_with_frozen_diagonals([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 2)
Output: [[1, 8, 3], [6, 5, 4], [7, 2, 9]]
Example 3
Input: rotate_with_frozen_diagonals([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], 1)
Output: [[1, 9, 5, 4], [14, 6, 7, 2], [15, 10, 11, 3], [13, 12, 8, 16]]
Hints
Hint 1
A standard clockwise rotation maps cell (r, c) to (c, n-1-r). Apply this transformation only to cells that are not on either diagonal.
Hint 2
Use a temporary matrix to avoid overwriting values you still need to read during the same rotation step.
Hint 3
Notice that after 4 rotations, non-diagonal cells return to their original positions, forming complete cycles.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is that a standard clockwise 90-degree rotation maps any cell at position (row, col) to position (col, n - 1 - row). We apply this transformation selectively: only to cells that are not on either diagonal.
An important mathematical property makes this work: when you apply the rotation mapping to a diagonal cell, it lands on a diagonal cell. Specifically:
- Main diagonal cells (where
row == col) map to anti-diagonal positions - Anti-diagonal cells (where
row + col == n - 1) map to main diagonal positions
This means the rotation naturally partitions the grid: diagonal cells map among themselves, and non-diagonal cells map among themselves. We can therefore safely rotate only the non-diagonal cells without any collision or gap.
The implementation uses a buffer matrix for each rotation to avoid reading values that have already been overwritten in the same step:
- Create a copy of the current state
- For each non-frozen cell
(r, c), write its value to the rotated position(c, n - 1 - r)in the buffer - Frozen cells keep their values from the copy
- Repeat for
kiterations
Correctness
Each rotation preserves diagonal values by construction—we never write to diagonal positions, and the buffer starts as a complete copy. Every non-diagonal destination receives exactly one value because the rotation is a bijection (one-to-one mapping). After k iterations, we have correctly simulated k selective rotations.
Complexity
Time: O(k × n²). Since k ≤ 4, this simplifies to O(n²). With n ≤ 100, we perform at most 40,000 cell operations.
Space: O(n²) for the result matrix and temporary buffer used during each rotation.
Edge Cases
- When
n = 1orn = 2, all cells are on diagonals, so the output equals the input - When
nis odd, the center cell lies on both diagonals and remains fixed - After 4 rotations, non-diagonal cells complete a full cycle and return to their starting positions
from typing import List
def rotate_with_frozen_diagonals(matrix: List[List[int]], k: int) -> List[List[int]]:
n = len(matrix)
result = [row[:] for row in matrix]
for _ in range(k):
rotated = [row[:] for row in result]
for r in range(n):
for c in range(n):
if r != c and r + c != n - 1:
rotated[c][n - 1 - r] = result[r][c]
result = rotated
return result