Find pairs in array whose sums already exist in array in C++

C++Server Side ProgrammingProgramming

In this problem, we are given an array arr[] consisting of N integer. Our task is to find pairs in an array whose sums already exist in the array. We need to find pairs with sum value = a value in the array

Let’s take an example to understand the problem,

Input

arr[] = {1, 2, 4, 6, 7}

Output

(1, 6), (2, 4)

Explanation

For pairs (1, 6), the sum of values is 7 which is present in the array.

For pairs (2, 4), the sum of values is 6 which is present in the array.

Solution Approach

A simple solution to the problem is by finding all the pairs possible using elements of the array. Then calculate the sum of values of the parir. Search for this sum value in the array, print if present.

Also, we will have a counter for the number of pairs count. And if it is 0, we will print no pairs found.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;
void findSumPairsArr(int arr[], int n){
   int pairCount = 0;
   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]<<" ), sum = "<<(arr[i] + arr[j])<<"\n";
               pairCount++;
            }
         }
      }
   }
   if (!pairCount)
      cout<<"No Such Pairs found !";
}
int main() {
   int arr[] = { 1, 2, 4, 6, 7 };
   int n = sizeof(arr) / sizeof(arr[0]);
   cout<<"Pairs in array whose sum already exists in array : \n";
   findSumPairsArr(arr, n);
   return 0;
}

Output

Pairs in array whose sum already exists in array −

( 1, 6 ), sum = 7
( 2, 4 ), sum = 6

Another approach which happens to be more effective is to solve the problem using a hash table. We will check all the paris and then calculate their sum and check if it exists in the array and keep track of it. If the pairCount is 0, print "No Such Pairs found !".

Here, the implementation of a hash table in c is done using unordered_set.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void findSumPairsArr(int arr[], int n) {
   unordered_set<int> HT;
   for (int i = 0; i < n; i++)
      HT.insert(arr[i]);
   int pairCount = 0;
   for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
         if (HT.find(arr[i] + arr[j]) != HT.end()) {
            cout<<"( "<<arr[i]<<", "<<arr[j]<<" ), sum =
            "<<(arr[i] + arr[j])<<"\n";
            pairCount ++;
         }
      }
   }
   if (!pairCount)
   cout<<"No Such Pairs found !";
}
int main() {
   int arr[] = {1, 2, 4, 6, 7 };
   int n = sizeof(arr) / sizeof(arr[0]);
   cout<<"Pairs in array whose sum already exists in array : \n";
   findSumPairsArr(arr, n);
   return 0;
}

Output

Pairs in array whose sum already exists in array −

( 1, 6 ), sum = 7
( 2, 4 ), sum = 6
raja
Published on 16-Mar-2021 04:56:05
Advertisements