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++
#include <bits/stdc++.h>
using namespace std;
void findPair( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
int sum = arr[i] + arr[j];
bool flag = true ;
for ( int k = 0; k < n; k++) {
if (arr[k] == sum) {
flag = false ;
break ;
}
}
if (flag)
count++;
}
}
cout << count;
}
int main()
{
int arr[] = { 2, 4, 2, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
findPair(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void findPair( int arr[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = i + 1 ; j < n; j++) {
int sum = arr[i] + arr[j];
boolean flag = true ;
for ( int k = 0 ; k < n; k++) {
if (arr[k] == sum) {
flag = false ;
break ;
}
}
if (flag)
count++;
}
}
System.out.println(count);
}
public static void main (String[] args) {
int arr[] = { 2 , 4 , 2 , 6 };
int n = arr.length;
findPair(arr, n);
}
}
|
Python3
def findPair(arr, n):
count = 0 ;
for i in range ( 0 ,n):
for j in range (i + 1 , n):
sum = arr[i] + arr[j];
flag = True ;
for k in range ( 0 ,n):
if (arr[k] = = sum ):
flag = False ;
break ;
if (flag):
count + = 1 ;
print (count);
arr = [ 2 , 4 , 2 , 6 ];
n = len (arr);
findPair(arr, n);
|
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;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
int sum = arr[i] + arr[j];
bool flag = true ;
for ( int k = 0; k < n; k++) {
if (arr[k] == sum) {
flag = false ;
break ;
}
}
if (flag)
count++;
}
}
Console.WriteLine(count);
}
}
|
Javascript
function findPair(arr, n)
{
let count = 0;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
let sum = arr[i] + arr[j];
let flag = true ;
for (let k = 0; k < n; k++) {
if (arr[k] == sum) {
flag = false ;
break ;
}
}
if (flag)
count++;
}
}
document.write(count);
}
let arr = [2, 4, 2, 6 ];
let n = arr.length;
findPair(arr, n);
|
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 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++
#include <bits/stdc++.h>
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;
}
|
Java
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);
}
}
|
Python3
def findPair(arr, n):
if (n < 2 ):
print ( "-1" )
hashMap = []
for k in arr:
hashMap.append(k)
for i in range (n - 1 ):
for j in range (i + 1 , n):
sum = arr[i] + arr[j]
if sum not in hashMap:
print ( "(" , arr[i] , ", " ,
arr[j] , ")" )
if __name__ = = "__main__" :
arr = [ 2 , 4 , 2 , 6 ]
n = len (arr)
findPair(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static void findPair( int [] arr, int n)
{
int i, j;
if (n < 2)
{
Console.Write( "-1" );
}
HashSet< int > hashMap = new HashSet< int >();
foreach ( int k in 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))
{
Console.Write( "(" + arr[i] +
", " + arr[j] +
")\n" );
}
}
}
}
public static void Main( string [] args)
{
int [] arr = { 2, 4, 2, 6 };
int n = arr.Length;
findPair(arr, n);
}
}
|
Javascript
<script>
function findPair(arr, n)
{
var i, j;
if (n < 2)
{
document.write( "-1" );
}
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] + ")<br>" );
}
}
}
}
var arr = [2, 4, 2, 6];
var n = arr.length;
findPair(arr, n);
</script>
|
Output:
(2, 6)
(4, 6)
(2, 6)
Time Complexity: O(N2)
Auxiliary Space: O(N)
Last Updated :
17 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...