Largest sub | C # implementation of the approach ; Function to find the maximum rectangular area under given histogram with n bars ; Create an empty stack . The stack holds indexes of hist [ ] array . The bars stored in stack are always in increasing order of their heights . ; Initialize max area ; To store top of the stack ; To store area with top bar ; Run through all bars of given histogram ; If this bar is higher than the bar on top stack , push it to stack ; If this bar is lower than top of stack , then calculate area of rectangle with stack top as the smallest ( or minimum height ) bar . ' i ' is ' right ▁ index ' for the top and element before top in stack is ' left ▁ index ' ; Store the top index ; Pop the top ; Calculate the area with hist [ tp ] stack as smallest bar ; Update max area , if needed ; Now pop the remaining bars from stack and calculate area with every popped bar as the smallest bar ; Function to find largest sub matrix with all equal elements ; To find largest sub matrix with all elements 1 ; Fill dp [ , ] by traversing each column from bottom to up ; Maintain the histogram array ; Find maximum area rectangle in Histogram ; To fill dp [ , ] for finding largest sub matrix with all elements 0 ; Maintain the histogram array ; Find maximum area rectangle in Histogram ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int row = 6 ; static int col = 8 ; static int cal ( int [ ] hist , int n ) { Stack < int > s = new Stack < int > ( ) ; int max_area = 0 ; int tp ; int area_with_top ; int i = 0 ; while ( i < n ) { if ( s . Count == 0 || hist [ s . Peek ( ) ] <= hist [ i ] ) s . Push ( i ++ ) ; else { tp = s . Peek ( ) ; s . Pop ( ) ; area_with_top = hist [ tp ] * ( s . Count == 0 ? i : i - s . Peek ( ) - 1 ) ; if ( max_area < area_with_top ) max_area = area_with_top ; } } while ( s . Count == 0 == false ) { tp = s . Peek ( ) ; s . Pop ( ) ; area_with_top = hist [ tp ] * ( s . Count == 0 ? i : i - s . Peek ( ) - 1 ) ; if ( max_area < area_with_top ) max_area = area_with_top ; } return max_area ; } static int largestMatrix ( int [ , ] a ) { int [ , ] dp = new int [ row , col ] ; for ( int i = 0 ; i < col ; i ++ ) { int cnt = 0 ; for ( int j = row - 1 ; j >= 0 ; j -- ) { dp [ j , i ] = 0 ; if ( a [ j , i ] == 1 ) { cnt ++ ; dp [ j , i ] = cnt ; } else { cnt = 0 ; } } } int ans = - 1 ; for ( int i = 0 ; i < row ; i ++ ) { int [ ] hist = new int [ col ] ; for ( int j = 0 ; j < col ; j ++ ) { hist [ j ] = dp [ i , j ] ; } ans = Math . Max ( ans , cal ( hist , col ) ) ; } for ( int i = 0 ; i < col ; i ++ ) { int cnt = 0 ; for ( int j = row - 1 ; j >= 0 ; j -- ) { dp [ j , i ] = 0 ; if ( a [ j , i ] == 0 ) { cnt ++ ; dp [ j , i ] = cnt ; } else { cnt = 0 ; } } } for ( int i = 0 ; i < row ; i ++ ) { int [ ] hist = new int [ col ] ; for ( int j = 0 ; j < col ; j ++ ) { hist [ j ] = dp [ i , j ] ; } ans = Math . Max ( ans , cal ( hist , col ) ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ , ] a = { { 1 , 1 , 0 , 1 , 0 , 0 , 0 , 0 } , { 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 } , { 1 , 0 , 0 , 1 , 1 , 1 , 0 , 0 } , { 0 , 1 , 1 , 0 , 1 , 1 , 0 , 0 } , { 1 , 0 , 1 , 1 , 1 , 1 , 1 , 0 } , { 0 , 0 , 1 , 1 , 1 , 1 , 1 , 1 } } ; Console . WriteLine ( largestMatrix ( a ) ) ; } }