Find pairs in array whose sum does not exist in Array | C ++ program to implement the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPair ( int arr [ ] , int n ) { int i , j ; if ( n < 2 ) { cout << " - 1" << endl ; } set < int > hashMap ; for ( int k = 0 ; k < n ; k ++ ) { hashMap . insert ( arr [ k ] ) ; } for ( i = 0 ; i < n - 1 ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( hashMap . find ( sum ) == hashMap . end ( ) ) { cout << " ( " << arr [ i ] << " , ▁ " << arr [ j ] << " ) " << endl ; } } } } int main ( ) { int arr [ ] = { 2 , 4 , 2 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findPair ( arr , n ) ; return 0 ; }