Find pairs in array whose sum does not exist in Array
Given an array arr[] consisting of N positive integers, the task is to print all pairs of array elements whose sum does not exist in the given array. If no such pair exists, print “-1”.
Examples:
Input: arr[] = {2, 4, 2, 6}
Output:
(2, 6)
(4, 6)
(2, 6)
Explanation:
All possible pairs in the array are (2, 4), (2, 2), (2, 6), (4, 2), (4, 6) and (2, 6).
Among these, the pairs (2, 6), (4, 6) and (2, 6) have sums {8, 10, 8} respectively which are not present in the array.Input: arr[] = {1, 1, 2, 3}
Output:
(2, 3)
Explanation:
All possible pairs in the array are (1, 1), (1, 2), (1, 3), (1, 2), (1, 3) and (2, 3).
Among these, the only pair whose sum is not present in the array is (2, 3).
Naive Approach:
The simplest approach to solve the problem is to generate all possible pairs one by one, and for every pair check if its sum exists in the array by traversing the array. If any pair is found with its sum existing in the array, print the pair. Otherwise, move to the next pair.
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to print all pairs // with sum not present in the array void findPair( int arr[], int n) { int count = 0; // Generating all pair. for ( int i = 0; i < n; i++) { for ( int j = i + 1; j < n; j++) { int sum = arr[i] + arr[j]; bool flag = true ; // Checking if sum of pair exist // in the given array or not for ( int k = 0; k < n; k++) { if (arr[k] == sum) { flag = false ; break ; } } // If sum doesn't exist, then increment the // count if (flag) count++; } } // Print the count cout << count; } // Driver code int main() { int arr[] = { 2, 4, 2, 6 }; int n = sizeof (arr) / sizeof (arr[0]); findPair(arr, n); return 0; } |
Java
/*package whatever //do not write package name here */ import java.io.*; class GFG { // Function to print all pairs // with sum not present in the array static void findPair( int arr[], int n) { int count = 0 ; // Generating all pair. for ( int i = 0 ; i < n; i++) { for ( int j = i + 1 ; j < n; j++) { int sum = arr[i] + arr[j]; boolean flag = true ; // Checking if sum of pair exist // in the given array or not for ( int k = 0 ; k < n; k++) { if (arr[k] == sum) { flag = false ; break ; } } // If sum doesn't exist, then increment the // count if (flag) count++; } } // Print the count System.out.println(count); } public static void main (String[] args) { int arr[] = { 2 , 4 , 2 , 6 }; int n = arr.length; findPair(arr, n); } } // This code is contributed by aadityaburujwale. |
Python3
# Python program to implement # the above approach # Function to print all pairs # with sum not present in the array def findPair(arr, n): count = 0 ; # Generating all pair. for i in range ( 0 ,n): for j in range (i + 1 , n): sum = arr[i] + arr[j]; flag = True ; # Checking if sum of pair exist # in the given array or not for k in range ( 0 ,n): if (arr[k] = = sum ): flag = False ; break ; # If sum doesn't exist, then increment the # count if (flag): count + = 1 ; # Print the count print (count); # Driver code arr = [ 2 , 4 , 2 , 6 ]; n = len (arr); findPair(arr, n); # This code is contributed by poojaagarwal2. |
C#
using System; public class Gfg { static void Main( string [] args) { int [] arr = { 2, 4, 2, 6 }; int n = arr.Length; FindPair(arr, n); } static void FindPair( int [] arr, int n) { int count = 0; // Generating all pair. for ( int i = 0; i < n; i++) { for ( int j = i + 1; j < n; j++) { int sum = arr[i] + arr[j]; bool flag = true ; // Checking if sum of pair exist // in the given array or not for ( int k = 0; k < n; k++) { if (arr[k] == sum) { flag = false ; break ; } } // If sum doesn't exist, then increment the // count if (flag) count++; } } // Print the count Console.WriteLine(count); } } |
Javascript
// Javascript program to implement // the above approach // Function to print all pairs // with sum not present in the array function findPair(arr, n) { let count = 0; // Generating all pair. for (let i = 0; i < n; i++) { for (let j = i + 1; j < n; j++) { let sum = arr[i] + arr[j]; let flag = true ; // Checking if sum of pair exist // in the given array or not for (let k = 0; k < n; k++) { if (arr[k] == sum) { flag = false ; break ; } } // If sum doesn't exist, then increment the // count if (flag) count++; } } // Print the count document.write(count); } // Driver code let arr = [2, 4, 2, 6 ]; let n = arr.length; findPair(arr, n); |
3
Time Complexity: O(N3)
Auxiliary Space: O(N)
Efficient Approach:
The problem can be solved using a HashSet. Follow the steps below to solve the problem:
- Store the elements in the of the array in a HashSet.
- Now, traverse over all the array elements and generate all possible pairs.
- For each pair, check if the sum of that pair is present in the HashSet or not. If so, print the pair. Otherwise, move to the next pair.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to print all pairs // with sum not present in the array void findPair( int arr[], int n) { int i, j; // Corner Case if (n < 2) { cout << "-1" << endl; } // Stores the distinct array // elements set < int > hashMap; for ( int k = 0; k < n; k++) { hashMap.insert(arr[k]); } // Generate all possible pairs for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { // Calculate sum of current pair int sum = arr[i] + arr[j]; // Check if the sum exists in // the HashSet or not if (hashMap.find(sum) == hashMap.end()) { cout << "(" << arr[i] << ", " << arr[j] << ")" << endl; } } } } // Driver code int main() { int arr[] = { 2, 4, 2, 6 }; int n = sizeof (arr) / sizeof (arr[0]); findPair(arr, n); return 0; } // This code is contributed by divyeshrabadiya07 |
Java
// Java program to implement // the above approach import java.util.*; class GFG { // Function to print all pairs // with sum not present in the array public static void findPair( int [] arr, int n) { int i, j; // Corner Case if (n < 2 ) { System.out.println( "-1" ); } // Stores the distinct array // elements HashSet<Integer> hashMap = new HashSet<Integer>(); for (Integer k : arr) { hashMap.add(k); } // Generate all possible pairs for (i = 0 ; i < n - 1 ; i++) { for (j = i + 1 ; j < n; j++) { // Calculate sum of current pair int sum = arr[i] + arr[j]; // Check if the sum exists in // the HashSet or not if (!hashMap.contains(sum)) { System.out.println( "(" + arr[i] + ", " + arr[j] + ")" ); } } } } // Driver Code public static void main(String[] args) { int [] arr = { 2 , 4 , 2 , 6 }; int n = arr.length; findPair(arr, n); } } |
Python3
# Python3 program to implement # the above approach # Function to print all pairs # with sum not present in the array def findPair(arr, n): # Corner Case if (n < 2 ): print ( "-1" ) # Stores the distinct array # elements hashMap = [] for k in arr: hashMap.append(k) # Generate all possible pairs for i in range (n - 1 ): for j in range (i + 1 , n): # Calculate sum of current pair sum = arr[i] + arr[j] # Check if the sum exists in # the HashSet or not if sum not in hashMap: print ( "(" , arr[i] , ", " , arr[j] , ")" ) # Driver Code if __name__ = = "__main__" : arr = [ 2 , 4 , 2 , 6 ] n = len (arr) findPair(arr, n) # This code is contributed by ChitraNayal |
C#
// C# program to implement // the above approach using System; using System.Collections.Generic; class GFG{ // Function to print all pairs // with sum not present in the array public static void findPair( int [] arr, int n) { int i, j; // Corner Case if (n < 2) { Console.Write( "-1" ); } // Stores the distinct array // elements HashSet< int > hashMap = new HashSet< int >(); foreach ( int k in arr) { hashMap.Add(k); } // Generate all possible pairs for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { // Calculate sum of current pair int sum = arr[i] + arr[j]; // Check if the sum exists in // the HashSet or not if (!hashMap.Contains(sum)) { Console.Write( "(" + arr[i] + ", " + arr[j] + ")\n" ); } } } } // Driver Code public static void Main( string [] args) { int [] arr = { 2, 4, 2, 6 }; int n = arr.Length; findPair(arr, n); } } // This code is contributed by rutvik_56 |
Javascript
<script> // Javascript program to implement // the above approach // Function to print all pairs // with sum not present in the array function findPair(arr, n) { var i, j; // Corner Case if (n < 2) { document.write( "-1" ); } // Stores the distinct array // elements var hashMap = new Set(); for ( var k = 0; k < n; k++) { hashMap.add(arr[k]); } // Generate all possible pairs for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { // Calculate sum of current pair var sum = arr[i] + arr[j]; // Check if the sum exists in // the HashSet or not if (!hashMap.has(sum)) { document.write( "(" + arr[i] + ", " + arr[j] + ")<br>" ); } } } } // Driver code var arr = [2, 4, 2, 6]; var n = arr.length; findPair(arr, n); // This code is contributed by famously. </script> |
(2, 6) (4, 6) (2, 6)
Time Complexity: O(N2)
Auxiliary Space: O(N)
Please Login to comment...