Open In App

Maximum size rectangle binary sub-matrix with all 1s | Set 2

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a binary matrix mat[][] of size n * m, your task is find the maximum area of the rectangle binary-sub-matrix with all 1’s.

Examples:

Input: mat[][] = [ [0, 1, 1, 0], 
                               [1, 1, 1, 1], 
                               [1, 1, 1, 1], 
                              [1, 1, 0, 0] ]
Output: 8
Explanation: The rectangle spanning from cell [1, 0] to [2, 3] consists of 8 ones, making it the largest possible area of a sub-matrix containing only 1’s.

Input: mat[][] = [ [0, 1, 1], 
                              [1, 1, 1], 
                               [0, 1, 1] ]
Output: 6
Explanation: The rectangle spanning from cell [0, 1] to [2, 2] consists of 6 ones, making it the largest possible area of a sub-matrix containing only 1’s.

Approach:

The idea is to use the concept of Largest Rectangular Area in Histogram. Follow the below given approach:

  • Consider each row as the base of the histogram chart formed with all 1s.
  • For each row, increase height of a bar by the amount of the previous row, only if the value in current row is 1 and calculate the largest rectangular area of that histogram.
  • The largest rectangular area of a histogram among all possible base rows is the required are of the rectangle.

Please refer Maximum size rectangle binary sub-matrix with all 1s for detailed explanation and implementation.



Next Article
Article Tags :
Practice Tags :

Similar Reads