text: Largest area possible after removal of a series of horizontal & vertical bars | C # program for the above approach ; Function to find the largest area when a series of horizontal & vertical bars are removed ; Stores all bars ; Insert horizontal bars ; Insert vertictal bars ; Remove horizontal separators from s1 ; Remove vertical separators from s2 ; Stores left out horizontal and vertical separators ; Sort both list in ascending order ; Find maximum difference of neighbors of list1 ; Find max difference of neighbors of list2 ; Print largest volume ; Driver code ; Given value of N & M ; Given arrays ; Function call to find the largest area when a series of horizontal & vertical bars are removed
code: using System ; using System . Collections . Generic ; class GFG { static void largestArea ( int N , int M , int [ ] H , int [ ] V ) { HashSet < int > s1 = new HashSet < int > ( ) ; HashSet < int > s2 = new HashSet < int > ( ) ; for ( int i = 1 ; i <= N + 1 ; i ++ ) s1 . Add ( i ) ; for ( int i = 1 ; i <= M + 1 ; i ++ ) s2 . Add ( i ) ; for ( int i = 0 ; i < H . Length ; i ++ ) { s1 . Remove ( H [ i ] ) ; } for ( int i = 0 ; i < V . Length ; i ++ ) { s2 . Remove ( V [ i ] ) ; } int [ ] list1 = new int [ s1 . Count ] ; int [ ] list2 = new int [ s2 . Count ] ; int I = 0 ; foreach ( int it1 in s1 ) { list1 [ I ++ ] = it1 ; } I = 0 ; foreach ( int it2 in s2 ) { list2 [ I ++ ] = it2 ; } Array . Sort ( list1 ) ; Array . Sort ( list2 ) ; int maxH = 0 , p1 = 0 , maxV = 0 , p2 = 0 ; for ( int j = 0 ; j < list1 . Length ; j ++ ) { maxH = Math . Max ( maxH , list1 [ j ] - p1 ) ; p1 = list1 [ j ] ; } for ( int j = 0 ; j < list2 . Length ; j ++ ) { maxV = Math . Max ( maxV , list2 [ j ] - p2 ) ; p2 = list2 [ j ] ; } Console . WriteLine ( maxV * maxH ) ; } static void Main ( ) { int N = 3 , M = 3 ; int [ ] H = { 2 } ; int [ ] V = { 2 } ; largestArea ( N , M , H , V ) ; } }