Watch 10 video solutions for Maximum Side Length of a Square with Sum Less than or Equal to Threshold, a medium level problem involving Array, Binary Search, Matrix. This walkthrough by codestorywithMIK has 10,272 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given a m x n matrix mat and an integer threshold, return the maximum side-length of a square with a sum less than or equal to threshold or return 0 if there is no such square.
Example 1:
Input: mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4 Output: 2 Explanation: The maximum side length of square with sum less than 4 is 2 as shown.
Example 2:
Input: mat = [[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2]], threshold = 1 Output: 0
Constraints:
m == mat.lengthn == mat[i].length1 <= m, n <= 3000 <= mat[i][j] <= 1040 <= threshold <= 105Problem Overview: You are given an m x n matrix and a threshold. The task is to find the largest possible square submatrix whose total sum is less than or equal to the threshold. The result is the side length of that square.
Approach 1: Prefix Sum + Iteration (Time: O(m * n * min(m,n)), Space: O(m * n))
This approach builds a 2D prefix sum matrix so you can query the sum of any square in constant time. After preprocessing, iterate through each cell and attempt to expand the square size from that position. Using the prefix matrix, compute the sum of a candidate square with the formula sum(x1,y1,x2,y2) in O(1). Continue increasing the side length while the square sum remains ≤ threshold. This method is straightforward and demonstrates correct use of prefix sums, but it checks many possible square sizes.
Approach 2: Binary Search on Side Length (Time: O(m * n * log(min(m,n))), Space: O(m * n))
This approach still relies on a 2D prefix sum but reduces unnecessary checks using binary search. Instead of testing every square size, search the possible side length range from 0 to min(m,n). For a candidate size k, scan the matrix and use the prefix sum to compute each k×k square sum in O(1). If any square satisfies the threshold constraint, the size is valid and you try a larger one. Otherwise, search smaller sizes. The monotonic property (if size k works, all smaller sizes work) makes binary search effective.
The prefix matrix itself is computed in O(m*n) using cumulative sums from the matrix. Each square sum query becomes a constant-time subtraction of four prefix values.
Recommended for interviews: Binary search with prefix sum is the approach most interviewers expect. A brute-force square scan shows you understand the problem, but combining prefix sums with binary search demonstrates algorithmic optimization and familiarity with matrix range queries.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Prefix Sum + Iteration | O(m * n * min(m,n)) | O(m * n) | When implementing a straightforward solution using prefix sums without optimization |
| Binary Search on Side Length + Prefix Sum | O(m * n * log(min(m,n))) | O(m * n) | Preferred approach when matrix dimensions are large and you want fewer square checks |