Problem Statement
Given a binary matrix mat[][] containing only 0s and 1s, and an integer k, we are also given multiple queries.
Each query contains a cell (i, j) that represents the center of a square.
For every query, we need to find the largest odd-sized square centered at (i, j) such that the square contains at most k ones.
If even the smallest 1 × 1 square contains more than k ones, the answer is -1.
Understanding the Square
Because the square must be centered at (i, j) and expand equally in all four directions, its side length is always odd.
For example:
Radius = 0
1 × 1
Radius = 1
3 × 3
Radius = 2
5 × 5
In general:
Side Length = 2 × radius + 1
So instead of directly searching for the side length, we can search for the maximum possible radius.
Example
Consider:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Suppose the query is:
(1, 2)
and:
k = 9
The center is:
row = 1
column = 2
The possible squares are:
Radius 0
1 × 1
Only the center cell is included.
Radius 1
3 × 3
Rows 0 to 2 and columns 1 to 3 are included.
This square contains 6 ones.
Since:
6 <= 9
the square is valid.
Radius 2
A 5 × 5 square would be required, but the center is too close to the matrix boundary.
Therefore, the largest valid square is:
3 × 3
So the answer is:
3
Main Idea
There are two important techniques used to solve this problem efficiently:
2D Prefix Sum
Binary Search
The constraints allow up to:
n, m <= 500
queries <= 10^4
Checking every cell for every possible square would be too expensive.
Instead, we first preprocess the matrix using a 2D prefix sum.
This allows us to find the number of ones inside any rectangular region in O(1) time.
Then, for each query, we use binary search to find the largest valid square.
1. Building the 2D Prefix Sum
We create:
int[][] prefix = new int[n + 1][m + 1];
The extra row and column make the calculations easier.
The formula is:
prefix[i][j] =
mat[i - 1][j - 1]
+ prefix[i - 1][j]
+ prefix[i][j - 1]
- prefix[i - 1][j - 1];
Why do we subtract:
prefix[i - 1][j - 1]Because that area was counted twice.
Visually:
prefix[i-1][j]
|
↓
+-------+-------+
| | |
| A | B |
| | |
+-------+-------+
| | |
| C | D |
| | |
+-------+-------+
↑
prefix[i][j-1]
When we add the top and left portions, the top-left portion is included twice, so we subtract it once.
2. Getting the Number of Ones in a Square
Suppose the square boundaries are:
top
bottom
left
right
We can calculate the number of ones using:
private int getSum(
int[][] prefix,
int top,
int left,
int bottom,
int right) {
return prefix[bottom + 1][right + 1]
- prefix[top][right + 1]
- prefix[bottom + 1][left]
+ prefix[top][left];
}
This gives the number of ones inside:
[top ... bottom]
[left ... right]
in constant time:
O(1)
3. Finding the Maximum Possible Radius
For a query:
int r = query[0];
int c = query[1];
the square must remain inside the matrix.
Therefore, the radius is limited by the distance from the center to each boundary.
The four distances are:
distance to top = r
distance to bottom = n - 1 - r
distance to left = c
distance to right = m - 1 - c
The smallest of these determines the maximum radius.
So:
int maxRadius = Math.min(
Math.min(r, n - 1 - r),
Math.min(c, m - 1 - c)
);
For example, if:
r = 2
c = 3
and the matrix is 5 × 7, then:
top = 2
bottom = 2
left = 3
right = 3
The smallest distance is 2, so the maximum radius is 2.
4. Important Edge Case
Before performing binary search, we check the 1 × 1 square.
if (mat[r][c] > k) {
ans.add(-1);
continue;
}
Why?
The smallest possible square contains only the center cell.
If the center contains 1 and:
k = 0
then:
1 > 0
Therefore, even the smallest square is invalid.
There is no answer, so we return:
-1
This is an important case that can otherwise cause a wrong answer.
5. Binary Search
Now we know:
Radius
0is valid.maxRadiusis the largest radius allowed by the matrix boundaries.
We binary search between:
int low = 0;
int high = maxRadius;

Join the conversation! Your thoughts help the community grow.