Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Shift 2D Grid

XPChallenge Points: 10
levelLevel: Easy

You are given a 2D matrix with m rows and n columns, along with an integer k.
Each shift operation moves elements forward in row-major order:

  • Every element at position (i, j) moves to (i, j+1).

  • If it is currently at the end of the row, it moves to the start of the next row.

  • The last element of the grid wraps around to position (0, 0).

You must perform exactly k such shifts and return the final configuration of the grid.

Example 1:

Input: grid = [[5,6],[7,8]], k = 2

Output: [[7,8],[5,6]]

Explanation:

After two shifts, elements rotate twice in order.

Example 2:

Input: grid = [[1,4,7],[2,5,8],[3,6,9]], k = 3

Output: [[7,1,4],[8,2,5],[9,3,6]]

Explanation:

Matrix shifts three positions forward in sequence.

Example 3:

Input: grid = [[10]], k = 5

Output: [[10]]

Explanation:

A single-cell grid always remains unchanged.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result