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 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 program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; bool sumEqualToK ( int arr [ ] , int & start , int & end , int n , int k ) { unordered_map < int , int > um ; 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 . find ( sum ) == um . end ( ) ) um [ sum ] = i ; if ( um . find ( sum - k ) != um . end ( ) ) { if ( maxLen < ( i - um [ sum - k ] ) ) { maxLen = i - um [ sum - k ] ; start = um [ sum - k ] + 1 ; end = i ; } } } return ( maxLen != 0 ) ; } void sumZeroMatrix ( int mat [ ] [ MAX ] , int row , int col , int k ) { int temp [ row ] , area ; bool sum ; int up , down ; int fup = 0 , fdown = 0 , fleft = 0 , fright = 0 ; int maxArea = INT_MIN ; for ( int left = 0 ; left < col ; left ++ ) { memset ( temp , 0 , sizeof ( temp ) ) ; for ( int right = left ; right < col ; right ++ ) { for ( int i = 0 ; i < row ; i ++ ) temp [ i ] += mat [ i ] [ right ] ; sum = sumEqualToK ( temp , up , down , row , k ) ; area = ( down - up + 1 ) * ( right - left + 1 ) ; if ( sum && maxArea < area ) { fup = up ; fdown = down ; fleft = left ; fright = right ; maxArea = area ; } } } if ( fup == 0 && fdown == 0 && fleft == 0 && fright == 0 && mat [ 0 ] [ 0 ] != k ) { cout << " No ▁ sub - matrix ▁ with ▁ sum ▁ " << k << " ▁ exists " ; return ; } cout << " ( Top , ▁ Left ) : ▁ " << " ( " << fup << " , ▁ " << fleft << " ) " << endl ; cout << " ( Bottom , ▁ Right ) : ▁ " << " ( " << fdown << " , ▁ " << fright << " ) " << endl ; for ( int j = fup ; j <= fdown ; j ++ ) { for ( int i = fleft ; i <= fright ; i ++ ) cout << mat [ j ] [ i ] << " ▁ " ; cout << endl ; } } int main ( ) { int mat [ ] [ MAX ] = { { 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 ) ; return 0 ; }