Check if there exist two elements in an array whose sum is equal to the sum of rest of the array | Function to check whether two elements exist whose sum is equal to sum of rest of the elements . ; Find sum of whole array ; If sum of array is not even than we can not divide it into two part ; For each element arr [ i ] , see if there is another element with vaalue sum - arr [ i ] ; If element exist than return the pair ; Driver Code
< ? php function checkPair ( & $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ; if ( $ sum % 2 != 0 ) return false ; $ sum = $ sum / 2 ; $ s = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ val = $ sum - $ arr [ $ i ] ; if ( array_search ( $ val , $ s ) ) { echo " Pair ▁ elements ▁ are ▁ " . $ arr [ $ i ] . " ▁ and ▁ " . $ val . " STRNEWLINE " ; return true ; } array_push ( $ s , $ arr [ $ i ] ) ; } return false ; } $ arr = array ( 2 , 11 , 5 , 1 , 4 , 7 ) ; $ n = sizeof ( $ arr ) ; if ( checkPair ( $ arr , $ n ) == false ) echo " No ▁ pair ▁ found " ; ? >