Given a 2D matrix, find the largest rectangular sub-matrix whose sum is 0. for example consider the following N x M input matrix
Input : 1, 2, 3 -3, -2, -1 1, 7, 5 Output : 1, 2, 3 -3, -2, -1 Input : 9, 7, 16, 5 1, -6, -7, 3 1, 8, 7, 9 7, -2, 0, 10 Output :-6, -7 8, 7 -2, 0
The naive solution for this problem is to check every possible rectangle in given 2D array. This solution requires 4 nested loops and time complexity of this solution would be O(n^4).
The solution is based on Maximum sum rectangle in a 2D matrix. The idea is to reduce the problem to 1 D array. We can use Hashing to find maximum length of sub-array in 1-D array in O(n) time. We fix the left and right columns one by one and find the largest sub-array with 0 sum contiguous rows for every left and right column pair. We basically find top and bottom row numbers (which have sum is zero) for every fixed left and right column pair. To find the top and bottom row numbers, calculate sum of elements in every row from left to right and store these sums in an array say temp. So temp[i] indicates sum of elements from left to right in row i. If we find largest subarray with 0 sum on temp, and no. of elements is greater than previous no. of elements then update the values of final row_up, final row_down, final col_left, final col_right.
-6, -7 8, 7 -2, 0
This article is contributed by Harshit Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Largest possible square submatrix with maximum AND value
- Largest rectangular sub-matrix having sum divisible by k
- Largest area rectangular sub-matrix with equal number of 1's and 0's
- Find the largest area rectangular sub-matrix whose sum is equal to k
- Submatrix Sum Queries
- XOR of a submatrix queries
- Submatrix of given size with maximum 1's
- Count of submatrix with sum X in a given Matrix
- Minimum sum submatrix in a given 2D array
- Construct a Matrix whose sum of diagonals for each square submatrix is even
- Summed Area Table - Submatrix Summation
- Maximum value in a matrix which contain intersecting concentric submatrix
- Maximize the binary matrix by filpping submatrix once
- Check if a matrix contains a square submatrix with 0 as boundary element
- Find Maximum Length Of A Square Submatrix Having Sum Of Elements At-Most K
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Check if matrix A can be converted to B by changing parity of corner elements of any submatrix
- Print concentric rectangular pattern in a 2d matrix
- Print cells with same rectangular sums in a matrix
- Minimum sprinklers required to water a rectangular park