Ctrl K

Rows Rolling Back Home

Easy
You are given an integer matrix grid with m rows and n columns (rows are numbered from zero) and an integer steps. In one step every row is rotated cyclically by one position at the same time: - even-numbered rows (0, 2, 4, …) rotate left: the first element moves to the end; - odd-numbered rows (1, 3, 5, …) rotate right: the last element moves to the front. Perform steps such steps. Return True if the resulting matrix is identical to the original one, and False otherwise.
Example 1
grid=[[1, 2, 1, 2], [3, 4, 3, 4]], steps=2 → True

Each row repeats with period 2, so after two shifts in either direction it looks exactly as before.

Example 2
grid=[[1, 2, 3], [4, 5, 6]], steps=1 → False

After one step the first row becomes [2, 3, 1] and the second becomes [6, 4, 5], so the grid has changed.

Example 3
grid=[[5, 6, 7], [8, 9, 1], [2, 3, 4]], steps=6 → True
Constraints
  • 1 ≤ m ≤ 25
  • 1 ≤ n ≤ 25
  • 1 ≤ grid[i][j] ≤ 25
  • 1 ≤ steps ≤ 50
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRepack the Tray EasyMirror and Negate a Bitmap EasySwap Rows and Columns EasyShadows of Block Towers EasySurface of Cube Towers Easy
loading Python…
Input
grid=[[1, 2, 1, 2], [3, 4, 3, 4]], steps=2
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 89 tests on the server, hidden ones included.