Find pairs in array whose sum does not exist in Array | Function to print all pairs with sum not present in the array ; Corner Case ; Stores the distinct array elements ; Generate all possible pairs ; Calculate sum of current pair ; Check if the sum exists in the HashSet or not ; Driver code
function findPair ( arr , n ) { var i , j ; if ( n < 2 ) { document . write ( " " ) ; } var hashMap = new Set ( ) ; for ( var k = 0 ; k < n ; k ++ ) { hashMap . add ( arr [ k ] ) ; } for ( i = 0 ; i < n - 1 ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { var sum = arr [ i ] + arr [ j ] ; if ( ! hashMap . has ( sum ) ) { document . write ( " " + arr [ i ] + " " + arr [ j ] + " " ) ; } } } } var arr = [ 2 , 4 , 2 , 6 ] ; var n = arr . length ; findPair ( arr , n ) ;