Matrix Similarity After Cyclic Shifts

You are given an m x n integer matrix mat and an integer k. The matrix rows are 0-indexed.

The following process happens k times:

  • Even-indexed rows (0, 2, 4, ...) are cyclically shifted to the left.
  • Odd-indexed rows (1, 3, 5, ...) are cyclically shifted to the right.

Return true if the final modified matrix after k steps is identical to the original matrix, and false otherwise.

Example 1
1 2 3
4 5 6
7 8 9
Inputmat = [[1,2,3],[4,5,6],[7,8,9]], k = 4
Outputfalse
In each step, rows 0 and 2 are shifted left and row 1 is shifted right, so after 4 steps the matrix is not identical to the original.
Example 2
1 2 1 2
5 5 5 5
6 3 6 3
Inputmat = [[1,2,1,2],[5,5,5,5],[6,3,6,3]], k = 2
Outputtrue
After 2 cyclic shifts, every row returns to an arrangement identical to its original row.

Constraints

  • 1 <= mat.length <= 25
  • 1 <= mat[i].length <= 25
  • 1 <= mat[i][j] <= 25
  • 1 <= k <= 50

Asked at 5 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