Longest Span with same Sum in two Binary arrays | Returns length of the longest common subarray with same sum ; Initialize result ; One by one pick all possible starting points of subarrays ; Initialize sums of current subarrays ; Conider all points for starting with arr [ i ] ; Update sums ; If sums are same and current length is more than maxLen , update maxLen ; Driver Code
< ? php function longestCommonSum ( $ arr1 , $ arr2 , $ n ) { $ maxLen = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum1 = 0 ; $ sum2 = 0 ; for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { $ sum1 += $ arr1 [ $ j ] ; $ sum2 += $ arr2 [ $ j ] ; if ( $ sum1 == $ sum2 ) { $ len = $ j - $ i + 1 ; if ( $ len > $ maxLen ) $ maxLen = $ len ; } } } return $ maxLen ; } $ arr1 = array ( 0 , 1 , 0 , 1 , 1 , 1 , 1 ) ; $ arr2 = array ( 1 , 1 , 1 , 1 , 1 , 0 , 1 ) ; $ n = sizeof ( $ arr1 ) ; echo " Length ▁ of ▁ the ▁ longest ▁ common ▁ span ▁ " . " with ▁ same ▁ " , " sum ▁ is ▁ " , longestCommonSum ( $ arr1 , $ arr2 , $ n ) ; ? >