Find pairs in array whose sums already exist in array
Given an array of n distinct and positive elements, the task is to find pair whose sum already exists in the given array.
Examples :
Input : arr[] = {2, 8, 7, 1, 5};
Output : 2 5
7 1
Input : arr[] = {7, 8, 5, 9, 11};
Output : Not Exist
A Naive Approach is to run three loops to find pair whose sum exists in an array.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void findPair( int arr[], int n)
{
bool found = false ;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
for ( int k = 0; k < n; k++) {
if (arr[i] + arr[j] == arr[k]) {
cout << arr[i] << " " << arr[j] << endl;
found = true ;
}
}
}
}
if (found == false )
cout << "Not exist" << endl;
}
int main()
{
int arr[] = { 10, 4, 8, 13, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
findPair(arr, n);
return 0;
}
|
Java
import java.io.*;
public class GFG {
static void findPair( int [] arr, int n)
{
boolean found = false ;
for ( int i = 0 ; i < n; i++) {
for ( int j = i + 1 ; j < n; j++) {
for ( int k = 0 ; k < n; k++) {
if (arr[i] + arr[j] == arr[k]) {
System.out.println(arr[i] +
" " + arr[j]);
found = true ;
}
}
}
}
if (found == false )
System.out.println( "Not exist" );
}
static public void main(String[] args)
{
int [] arr = { 10 , 4 , 8 , 13 , 5 };
int n = arr.length;
findPair(arr, n);
}
}
|
Python3
def findPair(arr, n):
found = False
for i in range ( 0 , n):
for j in range (i + 1 , n):
for k in range ( 0 , n):
if (arr[i] + arr[j] = = arr[k]):
print (arr[i], arr[j])
found = True
if (found = = False ):
print ( "Not exist" )
if __name__ = = '__main__' :
arr = [ 10 , 4 , 8 , 13 , 5 ]
n = len (arr)
findPair(arr, n)
|
C#
using System;
public class GFG {
static void findPair( int [] arr, int n)
{
bool found = false ;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
for ( int k = 0; k < n; k++) {
if (arr[i] + arr[j] == arr[k]) {
Console.WriteLine(arr[i] +
" " + arr[j]);
found = true ;
}
}
}
}
if (found == false )
Console.WriteLine( "Not exist" );
}
static public void Main(String []args)
{
int [] arr = {10, 4, 8, 13, 5};
int n = arr.Length;
findPair(arr, n);
}
}
|
PHP
<?php
function findPair( $arr , $n )
{
$found = false;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = $i + 1; $j < $n ; $j ++)
{
for ( $k = 0; $k < $n ; $k ++)
{
if ( $arr [ $i ] + $arr [ $j ] == $arr [ $k ])
{
echo $arr [ $i ] , " " , $arr [ $j ] ;
$found = true;
}
}
}
}
if ( $found == false)
echo "Not exist" ;
}
$arr = array ( 10, 4, 8, 13, 5 );
$n = sizeof( $arr ) / sizeof( $arr [0]);
findPair( $arr , $n );
?>
|
Javascript
<script>
function findPair(arr,n)
{
let found = false ;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
for (let k = 0; k < n; k++) {
if (arr[i] + arr[j] == arr[k]) {
document.write(arr[i] +
" " + arr[j]+ "<br>" );
found = true ;
}
}
}
}
if (found == false )
document.write( "Not exist" );
}
let arr=[10, 4, 8, 13, 5];
let n = arr.length;
findPair(arr, n);
</script>
|
Time complexity: O(n3)
Auxiliary space: O(1)
An Efficient solution is to store all elements in a hash table (unordered_set in C++) and check one by one all pairs and check its sum exists in set or not. If it exists in the set then print pair. If no pair found in the array then print not exists.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void findPair( int arr[], int n)
{
unordered_set< int > s;
for ( int i = 0; i < n; i++)
s.insert(arr[i]);
bool found = false ;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if (s.find(arr[i] + arr[j]) != s.end()) {
cout << arr[i] << " " << arr[j] << endl;
found = true ;
}
}
}
if (found == false )
cout << "Not exist" << endl;
}
int main()
{
int arr[] = { 10, 4, 8, 13, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
findPair(arr, n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class Getpairs {
public static void findPair( int [] arr, int n)
{
HashSet<Integer> s = new HashSet<Integer>();
for (Integer i : arr) {
s.add(i);
}
boolean found = false ;
for ( int i = 0 ; i < n - 1 ; i++) {
for ( int j = i + 1 ; j < n; j++) {
int sum = arr[i] + arr[j];
if (s.contains(sum)) {
found = true ;
System.out.println(arr[i] + " "
+ arr[j]);
}
}
}
if (found == false )
System.out.println( "Not Exist " );
}
public static void main(String args[])
{
int [] arr = { 10 , 4 , 8 , 13 , 5 };
int n = arr.length;
findPair(arr, n);
}
}
|
Python3
def findPair(arr, n):
s = {i : 1 for i in arr}
found = False
for i in range (n):
for j in range (i + 1 , n):
if arr[i] + arr[j] in s.keys():
print (arr[i], arr[j])
found = True
if found = = False :
print ( "Not exist" )
arr = [ 10 , 4 , 8 , 13 , 5 ]
n = len (arr)
findPair(arr, n)
|
C#
using System;
using System.Collections.Generic;
class Getpairs
{
public static void findPair( int [] arr, int n)
{
HashSet< int > s = new HashSet< int >();
foreach ( int i in arr)
{
s.Add(i);
}
bool found = false ;
for ( int i = 0; i < n - 1; i++)
{
for ( int j = i + 1; j < n; j++)
{
int sum = arr[i] + arr[j];
if (s.Contains(sum))
{
found = true ;
Console.WriteLine(arr[i] + " "
+ arr[j]);
}
}
}
if (found == false )
Console.WriteLine( "Not Exist " );
}
public static void Main()
{
int [] arr = { 10, 4, 8, 13, 5 };
int n = arr.Length;
findPair(arr, n);
}
}
|
Javascript
<script>
function findPair(arr,n)
{
let s = new Set();
for (let i=0;i<arr.length;i++) {
s.add(arr[i]);
}
let found = false ;
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
let sum = arr[i] + arr[j];
if (s.has(sum)) {
found = true ;
document.write(arr[i] + " "
+ arr[j]+ "<br>" );
}
}
}
if (found == false )
document.write( "Not Exist " );
}
let arr=[10, 4, 8, 13, 5 ];
let n = arr.length;
findPair(arr, n);
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(n2)
Last Updated :
07 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...