
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find pairs in array whose sums already exist in array in C++
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
#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
#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
- Related Articles
- Count pairs whose products exist in array in C++
- Add object to array in JavaScript if name does not already exist?
- Count pairs in array whose sum is divisible by K in C++
- Count pairs in array whose sum is divisible by 4 in C++
- Find K Pairs with Smallest Sums in C++
- Count of pairs in an array whose sum is a perfect square in C++
- Count pairs in a sorted array whose sum is less than x in C++
- Count pairs in a sorted array whose product is less than k in C++
- Count number of distinct pairs whose sum exists in the given array in C++
- Array index to balance sums in JavaScript
- Array thirds with equal sums in JavaScript
- All combinations of sums for array in JavaScript
- Construct Target Array With Multiple Sums in C++
- Find all distinct subset (or subsequence) sums of an array in C++
- Find original array from encrypted array (An array of sums of other elements) using C++.
