1260. Shift 2D Grid LeetCode Solution
In this guide, you will get 1260. Shift 2D Grid LeetCode Solution with the best time and space complexity. The solution to Shift D Grid problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Shift D Grid solution in C++
- Shift D Grid solution in Java
- Shift D Grid solution in Python
- Additional Resources

Problem Statement of Shift D Grid
Given a 2D grid of size m x n and an integer k. You need to shift the grid k times.
In one shift operation:
Element at grid[i][j] moves to grid[i][j + 1].
Element at grid[i][n – 1] moves to grid[i + 1][0].
Element at grid[m – 1][n – 1] moves to grid[0][0].
Return the 2D grid after applying shift operation k times.
Example 1:
Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output: [[9,1,2],[3,4,5],[6,7,8]]
Example 2:
Input: grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
Output: [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
Example 3:
Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9
Output: [[1,2,3],[4,5,6],[7,8,9]]
Constraints:
m == grid.length
n == grid[i].length
1 <= m <= 50
1 <= n <= 50
-1000 <= grid[i][j] <= 1000
0 <= k <= 100
Complexity Analysis
- Time Complexity: O(mn)
- Space Complexity: O(mn)
1260. Shift 2D Grid LeetCode Solution in C++
class Solution {
public:
vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) {
const int m = grid.size();
const int n = grid[0].size();
vector<vector<int>> ans(m, vector<int>(n));
k %= m * n;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) {
const int index = (i * n + j + k) % (m * n);
const int x = index / n;
const int y = index % n;
ans[x][y] = grid[i][j];
}
return ans;
}
};
/* code provided by PROGIEZ */
1260. Shift 2D Grid LeetCode Solution in Java
class Solution {
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
final int m = grid.length;
final int n = grid[0].length;
List<List<Integer>> ans = new ArrayList<>();
int[][] arr = new int[m][n];
k %= m * n;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) {
final int index = (i * n + j + k) % (m * n);
final int x = index / n;
final int y = index % n;
arr[x][y] = grid[i][j];
}
for (int[] row : arr)
ans.add(Arrays.stream(row).boxed().collect(Collectors.toList()));
return ans;
}
}
// code provided by PROGIEZ
1260. Shift 2D Grid LeetCode Solution in Python
class Solution:
def shiftGrid(self, grid: list[list[int]], k: int) -> list[list[int]]:
m = len(grid)
n = len(grid[0])
ans = [[0] * n for _ in range(m)]
k %= m * n
for i in range(m):
for j in range(n):
index = (i * n + j + k) % (m * n)
x = index // n
y = index % n
ans[x][y] = grid[i][j]
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.