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 program .
function checkPair ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } if ( sum % 2 != 0 ) { return false ; } sum = Math . floor ( sum / 2 ) ; let s = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) { let val = sum - arr [ i ] ; if ( ! s . has ( arr [ i ] ) ) { s . add ( arr [ i ] ) } if ( s . has ( val ) ) { document . write ( " " + arr [ i ] + " " + val + " " ) ; return true ; } s . add ( arr [ i ] ) ; } return false ; } let arr = [ 2 , 11 , 5 , 1 , 4 , 7 ] ; let n = arr . length ; if ( checkPair ( arr , n ) == false ) { document . write ( " " ) ; }