Smallest Integer to be inserted to have equal sums | Function to find the minimum value to be added ; Variable to store entire array sum ; Variables to store sum of subarray1 and subarray 2 ; minimum value to be added ; Traverse through the array ; Sum of both halves ; Calculate minimum number to be added ; Driver code ; Length of array
< ? php function findMinEqualSums ( $ a , $ N ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ sum += $ a [ $ i ] ; } $ sum1 = 0 ; $ sum2 = 0 ; $ min = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ sum1 += $ a [ $ i ] ; $ sum2 = $ sum - $ sum1 ; if ( abs ( $ sum1 - $ sum2 ) < $ min ) { $ min = abs ( $ sum1 - $ sum2 ) ; } if ( $ min == 0 ) { break ; } } return $ min ; } $ a = array ( 3 , 2 , 1 , 5 , 7 , 8 ) ; $ N = count ( $ a ) ; echo ( findMinEqualSums ( $ a , $ N ) ) ; ? >