Back to March Back to Year 2026
EASY 21 Mar 2026 View on LeetCode

3643. Flip Square Submatrix Vertically

</> Solution

/**
 * Problem: LeetCode 3643 - Flip Square Submatrix Vertically
 * Difficulty: Easy
 * Language: Java
 * Author: CodeByTushu
 */
class Solution {
    public int[][] flipSubmatrix(int[][] grid, int x, int y, int k) {
        int top = x;
        int bottom = x + k - 1;

        while (top < bottom) {
            for (int j = y; j < y + k; j++) {
                int temp = grid[top][j];
                grid[top][j] = grid[bottom][j];
                grid[bottom][j] = temp;
            }
            top++;
            bottom--;
        }

        return grid;
    }
}

TIME COMPLEXITY

O(k^2)

SPACE COMPLEXITY

O(1)

TOPICS

Array Matrix Simulation Two Pointers