Find pairs in array whose sum does not exist in Array | Java 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
import java . util . * ; class GFG { public static void findPair ( int [ ] arr , int n ) { int i , j ; if ( n < 2 ) { System . out . println ( " - 1" ) ; } HashSet < Integer > hashMap = new HashSet < Integer > ( ) ; for ( Integer k : arr ) { hashMap . add ( k ) ; } for ( i = 0 ; i < n - 1 ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( ! hashMap . contains ( sum ) ) { System . out . println ( " ( " + arr [ i ] + " , ▁ " + arr [ j ] + " ) " ) ; } } } } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 2 , 6 } ; int n = arr . length ; findPair ( arr , n ) ; } }