Largest rectangular sub-matrix whose sum is 0
Pattern: 2D grid DP
Fix the top and bottom rows, add up each column between them, and find the widest run of columns whose sum is 0 with prefix sums in a hash map. The area is the number of rows times that width.
The problem
Given a matrix of integers, return the area of the largest rectangle whose sum is 0, or 0 if none exists. GFG prints the rectangle; this guide returns its area.
Think first
- If two rows are fixed, what is left?
- When does a run of columns sum to 0?
- How many pairs of rows are there?
Hints
Brute force, and why it fails
Try every rectangle with a 2D prefix sum: O(R² × C²).
The key insight
The row pair reduces the rest to the longest zero-sum subarray, which is linear with a hash map.
Dry run
[[1, 2, 3], [-3, -2, -1], [1, 7, 5]]. Rows 0 and 1 give column sums -2, 0, 2.
Step 1 of 2
The plan
for top: col = zeros; for bottom: col += row bottom
widest zero-sum run of col; best = max(best, rows × width)
Complexity
- Time
O(R² × C)- Space
O(C)
Every pair of rows times a linear scan.
Edge cases
- A single 0: 1.
- No zero-sum rectangle: 0.
- All zeros: the whole matrix.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
public int maxZeroArea(int[][] mat) {
int R = mat.length, C = mat[0].length, best = 0;
for (int top = 0; top < R; top++) {
int[] col = new int[C]; // column sums between rows top and bottom
for (int bottom = top; bottom < R; bottom++) {
for (int c = 0; c < C; c++) col[c] += mat[bottom][c];
Map<Integer, Integer> first = new HashMap<>(); // prefix sum -> earliest column where it was seen
first.put(0, -1);
int sum = 0;
for (int c = 0; c < C; c++) { // a repeated prefix sum means the columns between sum to 0
sum += col[c];
Integer start = first.get(sum);
if (start != null) best = Math.max(best, (bottom - top + 1) * (c - start));
else first.put(sum, c);
}
}
}
return best;
}
}C++
class Solution {
public:
int maxZeroArea(vector<vector<int>>& mat) {
int R = static_cast<int>(mat.size()), C = static_cast<int>(mat[0].size()), best = 0;
for (int top = 0; top < R; top++) {
vector<int> col(C, 0); // column sums between rows top and bottom
for (int bottom = top; bottom < R; bottom++) {
for (int c = 0; c < C; c++) col[c] += mat[bottom][c];
unordered_map<int, int> first; // prefix sum -> earliest column where it was seen
first[0] = -1;
int sum = 0;
for (int c = 0; c < C; c++) { // a repeated prefix sum means the columns between sum to 0
sum += col[c];
auto it = first.find(sum);
if (it != first.end()) best = max(best, (bottom - top + 1) * (c - it->second));
else first[sum] = c;
}
}
}
return best;
}
};Python
class Solution:
def maxZeroArea(self, mat: list[list[int]]) -> int:
R, C, best = len(mat), len(mat[0]), 0
for top in range(R):
col = [0] * C # column sums between rows top and bottom
for bottom in range(top, R):
for c in range(C):
col[c] += mat[bottom][c]
first, s = {0: -1}, 0 # prefix sum -> earliest column where it was seen
for c in range(C): # a repeated prefix sum means the columns between sum to 0
s += col[c]
if s in first:
best = max(best, (bottom - top + 1) * (c - first[s]))
else:
first[s] = c
return bestCommon mistakes
- Overwriting the first position of a prefix sum.
- Forgetting the empty prefix (sum 0 at -1).