Longest Span with same Sum in two Binary arrays | C ++ program to find largest subarray with equal number of 0 ' s ▁ and ▁ 1' s . ; Returns largest common subarray with equal number of 0 s and 1 s in both of t ; Find difference between the two ; Creates an empty hashMap hM ; Initialize sum of elements ; Initialize result ; Traverse through the given array ; Add current element to sum ; To handle sum = 0 at last index ; If this sum is seen before , then update max_len if required ; Else put this sum in hash table ; Driver progra + m to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestCommonSum ( bool arr1 [ ] , bool arr2 [ ] , int n ) { int arr [ n ] ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = arr1 [ i ] - arr2 [ i ] ; unordered_map < int , int > hM ; int sum = 0 ; int max_len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == 0 ) max_len = i + 1 ; if ( hM . find ( sum ) != hM . end ( ) ) max_len = max ( max_len , i - hM [ sum ] ) ; else hM [ sum ] = i ; } return max_len ; } int main ( ) { bool arr1 [ ] = { 0 , 1 , 0 , 1 , 1 , 1 , 1 } ; bool arr2 [ ] = { 1 , 1 , 1 , 1 , 1 , 0 , 1 } ; int n = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; cout << longestCommonSum ( arr1 , arr2 , n ) ; return 0 ; }