Given a binary matrix, find the maximum size rectangle binary-sub-matrix with all 1’s.
Input: 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 Output : 1 1 1 1 1 1 1 1 Explanation : The largest rectangle with only 1's is from (1, 0) to (2, 3) which is 1 1 1 1 1 1 1 1 Input: 0 1 1 1 1 1 0 1 1 Output: 1 1 1 1 1 1 Explanation : The largest rectangle with only 1's is from (0, 1) to (2, 2) which is 1 1 1 1 1 1
There is already an algorithm discussed a dynamic programming based solution for finding largest square with 1s.
Approach: In this post an interesting method is discussed that uses largest rectangle under histogram as a subroutine.
If the height of bars of the histogram is given then the largest area of the histogram can be found. This way in each row, the largest area of bars of the histogram can be found. To get the largest rectangle full of 1’s, update the next row with the previous row and find the largest area under the histogram, i.e. consider each 1’s as filled squares and 0’s with an empty square and consider each row as the base.
Input : 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 Step 1: 0 1 1 0 maximum area = 2 Step 2: row 1 1 2 2 1 area = 4, maximum area becomes 4 row 2 2 3 3 2 area = 8, maximum area becomes 8 row 3 3 4 0 0 area = 6, maximum area remains 8
- Run a loop to traverse through the rows.
- Now If the current row is not the first row then update the row as follows, if matrix[i][j] is not zero then matrix[i][j] = matrix[i-1][j] + matrix[i][j].
- Find the maximum rectangular area under the histogram, consider the ith row as heights of bars of a histogram. This can be calculated as given in this article Largest Rectangular Area in a Histogram
- Do the previous two steps for all rows and print the maximum area of all the rows.
Note: It is strongly recommended to refer this post first as most of the code taken from there.
Area of maximum rectangle is 8
- Time Complexity: O(R x C).
Only one traversal of the matrix is required, so the time complexity is O(R X C)
- Space Complexity: O(C).
Stack is required to store the columns, so so space complexity is O(C)
This article is contributed by Sanjiv Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article and 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.
- Submatrix of given size with maximum 1's
- Maximize the binary matrix by filpping submatrix once
- Maximum value in a matrix which contain intersecting concentric submatrix
- Find Maximum Length Of A Square Submatrix Having Sum Of Elements At-Most K
- Largest possible square submatrix with maximum AND value
- Maximum sum of any submatrix of a Matrix which is sorted row-wise and column-wise
- Smallest submatrix with Kth maximum XOR
- Maximum size of square such that all submatrices of that size have sum less than K
- Submatrix Sum Queries
- Summed Area Table - Submatrix Summation
- Minimum sum submatrix in a given 2D array
- XOR of a submatrix queries
- Check if matrix A can be converted to B by changing parity of corner elements of any submatrix
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Check if a matrix contains a square submatrix with 0 as boundary element
- Count of submatrix with sum X in a given Matrix
- Bitwise XOR of a submatrix of a matrix generated from a given array
- Check whether row or column swaps produce maximum size binary sub-matrix with all 1s
- Sliding Window Maximum (Maximum of all subarrays of size k)
- Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time