Cyclically Rotating a Grid

You are given an m x n integer matrix grid, where m and n are both even integers, and an integer k.

The matrix is composed of several layers, where each layer is rotated independently.

A cyclic rotation of the matrix is done by cyclically rotating each layer in the matrix. To cyclically rotate a layer once, each element in the layer will take the place of the adjacent element in the counter-clockwise direction.

Return the matrix after applying k cyclic rotations to it.

Example 1
40 10
30 20
Inputgrid = [[40,10],[30,20]], k = 1
Output[[10,20],[40,30]]
The figures above represent the grid at every state.
Example 2
 1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15 16
Inputgrid = [[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]]
The figures above represent the grid at every state.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 2 <= m, n <= 50
  • Both m and n are even integers.
  • 1 <= grid[i][j] <=^ 5000
  • 1 <= k <= 10^9

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate