Find the largest area rectangular sub | Java 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 final 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 final 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 final values ; Driver code
import java . util . * ; class GFG { static int MAX = 100 ; static int start , end ; static boolean sumEqualToK ( int arr [ ] , int n , int k ) { HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; 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 . put ( sum , i ) ; if ( um . containsKey ( sum - k ) ) { if ( maxLen < ( i - um . get ( sum - k ) ) ) { maxLen = i - um . get ( sum - k ) ; start = um . get ( 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 ; boolean sum = false ; int fup = 0 , fdown = 0 , fleft = 0 , fright = 0 ; int maxArea = Integer . MIN_VALUE ; 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 ) { System . out . print ( " No ▁ sub - matrix ▁ with ▁ sum ▁ " + k + " ▁ exists " ) ; return ; } System . out . print ( " ( Top , ▁ Left ) : ▁ " + " ( " + fup + " , ▁ " + fleft + " ) " + "NEW_LINE"); System . out . print ( " ( Bottom , ▁ Right ) : ▁ " + " ( " + fdown + " , ▁ " + fright + " ) " + "NEW_LINE"); for ( int j = fup ; j <= fdown ; j ++ ) { for ( int i = fleft ; i <= fright ; i ++ ) System . out . print ( mat [ j ] [ i ] + " ▁ " ) ; System . out . println ( ) ; } } 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 ) ; } }