Flip Square Submatrix Vertically
Problem
Given an m × n matrix grid and three integers x, y, k: the cell (x, y) is the top-left corner of a k × k square submatrix. Flip that square vertically — reverse the order of its rows — and return the updated matrix. The cells outside the square stay untouched.
grid = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], x = 1, y = 0, k = 3[[1,2,3,4],[13,14,15,8],[9,10,11,12],[5,6,7,16]]def reverseSubmatrix(grid, x, y, k):
top, bottom = x, x + k - 1 # two row pointers
while top < bottom: # walk inward until they meet
for c in range(y, y + k): # swap the k columns of the block
grid[top][c], grid[bottom][c] = grid[bottom][c], grid[top][c]
top += 1 # move top down
bottom -= 1 # move bottom up
return grid
function reverseSubmatrix(grid, x, y, k) {
let top = x, bottom = x + k - 1; // two row pointers
while (top < bottom) { // walk inward until they meet
for (let c = y; c < y + k; c++) { // swap the k columns of the block
const t = grid[top][c];
grid[top][c] = grid[bottom][c];
grid[bottom][c] = t;
}
top++; // move top down
bottom--; // move bottom up
}
return grid;
}
int[][] reverseSubmatrix(int[][] grid, int x, int y, int k) {
int top = x, bottom = x + k - 1; // two row pointers
while (top < bottom) { // walk inward until they meet
for (int c = y; c < y + k; c++) { // swap the k columns of the block
int t = grid[top][c];
grid[top][c] = grid[bottom][c];
grid[bottom][c] = t;
}
top++; // move top down
bottom--; // move bottom up
}
return grid;
}
vector<vector<int>> reverseSubmatrix(vector<vector<int>>& grid, int x, int y, int k) {
int top = x, bottom = x + k - 1; // two row pointers
while (top < bottom) { // walk inward until they meet
for (int c = y; c < y + k; c++) { // swap the k columns of the block
swap(grid[top][c], grid[bottom][c]);
}
top++; // move top down
bottom--; // move bottom up
}
return grid;
}
Explanation
Flipping a square vertically means reversing the order of its rows: the first row of the block becomes the last, the second becomes the second-to-last, and so on. Only the cells inside the k × k block change; everything else in the grid is left exactly as it was.
The clean way to reverse rows in place is the classic two-pointer trick. Place top at the block's first row (x) and bottom at its last row (x + k − 1). While top < bottom, swap those two rows column by column, then step top down and bottom up so they march toward the middle.
Each swap only touches the k columns that belong to the block — columns y through y + k − 1 — so the rest of each row is never disturbed. When top meets or crosses bottom the block is fully reversed (an odd k leaves the exact middle row fixed, which is correct).
Because we swap in place, no extra matrix is needed. We touch each of the k² block cells at most once, giving O(k²) time and O(1) extra space.
Example: with x = 1, y = 0, k = 3 the block is rows 1–3, columns 0–2. We swap row 1 [5,6,7] with row 3 [13,14,15]; top and bottom then both reach row 2, so the loop stops and the middle row [9,10,11] is untouched. Column 3 stays the same throughout.