Largest sub | Javascript 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
let row = 6 ; let col = 8 ; function cal ( hist , n ) { let s = [ ] ; let max_area = 0 ; let tp ; let area_with_top ; let i = 0 ; while ( i < n ) { if ( s . length == 0 hist [ s [ s . length - 1 ] ] <= hist [ i ] ) s . push ( i ++ ) ; else { tp = s [ s . length - 1 ] ; s . pop ( ) ; area_with_top = hist [ tp ] * ( s . length == 0 ? i : i - s [ s . length - 1 ] - 1 ) ; if ( max_area < area_with_top ) max_area = area_with_top ; } } while ( s . length != 0 ) { tp = s [ s . length - 1 ] ; s . pop ( ) ; area_with_top = hist [ tp ] * ( s . length == 0 ? i : i - s [ s . length - 1 ] - 1 ) ; if ( max_area < area_with_top ) max_area = area_with_top ; } return max_area ; } function largestMatrix ( a ) { let dp = new Array ( row ) ; for ( let i = 0 ; i < row ; i ++ ) { dp [ i ] = new Array ( col ) ; } for ( let i = 0 ; i < col ; i ++ ) { let cnt = 0 ; for ( let j = row - 1 ; j >= 0 ; j -- ) { dp [ j ] [ i ] = 0 ; if ( a [ j ] [ i ] == 1 ) { cnt ++ ; dp [ j ] [ i ] = cnt ; } else { cnt = 0 ; } } } let ans = - 1 ; for ( let i = 0 ; i < row ; i ++ ) { let hist = new Array ( col ) ; for ( let j = 0 ; j < col ; j ++ ) { hist [ j ] = dp [ i ] [ j ] ; } ans = Math . max ( ans , cal ( hist , col ) ) ; } for ( let i = 0 ; i < col ; i ++ ) { let cnt = 0 ; for ( let j = row - 1 ; j >= 0 ; j -- ) { dp [ j ] [ i ] = 0 ; if ( a [ j ] [ i ] == 0 ) { cnt ++ ; dp [ j ] [ i ] = cnt ; } else { cnt = 0 ; } } } for ( let i = 0 ; i < row ; i ++ ) { let hist = new Array ( col ) ; for ( let j = 0 ; j < col ; j ++ ) { hist [ j ] = dp [ i ] [ j ] ; } ans = Math . max ( ans , cal ( hist , col ) ) ; } return ans ; } let 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 ] ] ; document . write ( largestMatrix ( a ) ) ;