Dsa
Two Pointers
Medium
Rotating Squares
You have a 2D grid of n x n representing a grid of squares. Each square can have a value of 0 (empty) or 1 (filled). You need to determine the maximum number of 1's that can be rotated 90 degrees in any direction in a selected round of k consecutive squares. Return the maximum number of 1's in a valid square formation after the rotation.
Input:
The first line contains an integer n (1 <= n <= 100)
The second line contains an integer k (1 <= k <= n)
The next n lines contain n integers (0s and 1s) representing the grid.
Output:
An integer representing the maximum number of 1's after rotating.
Example:
Input:
4
2
1 0 0 1
0 1 1 0
1 1 0 0
0 0 1 1
Output:
4
Explanation: By selecting the 2x2 grid in the bottom right corner and rotating the squares, we can maximize the number of filled squares.
Key concepts
two_pointersgeometryarray
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now