Find the largest area rectangular sub | C # implementation to find the largest area rectangular sub - matrix whose sum is equal to k ; This function basically finds largest ' k ' sum subarray in arr [ 0. . n - 1 ] . If ' k ' sum doesn 't exist, then it returns false. Else it returns true and sets starting and ending indexes as start and end. ; unordered_map ' um ' implemented as hash table ; traverse the given array ; accumulate sum ; when subarray starts from index '0' update maxLength and start and end points ; make an entry for ' sum ' if it is not present in ' um ' ; check if ' sum - k ' is present in ' um ' or not ; update maxLength and start and end points ; Return true if maximum length is non - zero ; function to find the largest area rectangular sub - matrix whose sum is equal to k ; Variables to store the temporary values ; Variables to store the readonly output ; Set the left column ; Initialize all elements of temp as 0 ; Set the right column for the left column set by outer loop ; Calculate sum between current left and right column for every row ' i ' ; Find largest subarray with ' k ' sum in [ ] temp . The sumEqualToK ( ) function also sets values of ' up ' and ' down ; ' . So if ' sum ' is true then rectangle exists between ( up , left ) and ( down , right ) which are the boundary values . ; Compare no . of elements with previous no . of elements in sub - Matrix . If new sub - matrix has more elements then update maxArea and readonly boundaries like fup , fdown , fleft , fright ; If there is no change in boundaries than check if mat [ 0 , 0 ] equals ' k ' If it is not equal to ' k ' then print that no such k - sum sub - matrix exists ; Print readonly values ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int MAX = 100 ; static int start , end ; static bool sumEqualToK ( int [ ] arr , int n , int k ) { Dictionary < int , int > um = new Dictionary < int , int > ( ) ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == k ) { maxLen = i + 1 ; start = 0 ; end = i ; } if ( ! um . ContainsKey ( sum ) ) um . Add ( sum , i ) ; if ( um . ContainsKey ( sum - k ) ) { if ( maxLen < ( i - um [ sum - k ] ) ) { maxLen = i - um [ sum - k ] ; start = um [ sum - k ] + 1 ; end = i ; } } } return ( maxLen != 0 ) ; } static void sumZeroMatrix ( int [ , ] mat , int row , int col , int k ) { int [ ] temp = new int [ row ] ; int area ; bool sum = false ; int fup = 0 , fdown = 0 , fleft = 0 , fright = 0 ; int maxArea = int . MinValue ; for ( int left = 0 ; left < col ; left ++ ) { temp = memset ( temp , 0 ) ; for ( int right = left ; right < col ; right ++ ) { for ( int i = 0 ; i < row ; i ++ ) temp [ i ] += mat [ i , right ] ; sum = sumEqualToK ( temp , row , k ) ; area = ( end - start + 1 ) * ( right - left + 1 ) ; if ( sum && maxArea < area ) { fup = start ; fdown = end ; fleft = left ; fright = right ; maxArea = area ; } } } if ( fup == 0 && fdown == 0 && fleft == 0 && fright == 0 && mat [ 0 , 0 ] != k ) { Console . Write ( " No ▁ sub - matrix ▁ with ▁ sum ▁ " + k + " ▁ exists " ) ; return ; } Console . Write ( " ( Top , ▁ Left ) : ▁ " + " ( " + fup + " , ▁ " + fleft + " ) " + " STRNEWLINE " ) ; Console . Write ( " ( Bottom , ▁ Right ) : ▁ " + " ( " + fdown + " , ▁ " + fright + " ) " + " STRNEWLINE " ) ; for ( int j = fup ; j <= fdown ; j ++ ) { for ( int i = fleft ; i <= fright ; i ++ ) Console . Write ( mat [ j , i ] + " ▁ " ) ; Console . WriteLine ( ) ; } } static int [ ] memset ( int [ ] arr , int val ) { for ( int i = 0 ; i < arr . Length ; i ++ ) arr [ i ] = val ; return arr ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 7 , - 6 , 5 } , { - 8 , 6 , 7 , - 2 } , { 10 , - 15 , 3 , 2 } , { - 5 , 2 , 0 , 9 } } ; int row = 4 , col = 4 ; int k = 7 ; sumZeroMatrix ( mat , row , col , k ) ; } }