# Count number of distinct pairs whose sum exists in the given array in C++

C++Server Side ProgrammingProgramming

We are given an array of, let's say, arr[] of integer values of any respective size and the task is to calculate the count of the number of distinct pairs available in a given array whose sum also exists in the same array.

Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

## Points to remember

• A pair will be counted once with the same elements irrespective of their orders. For example, (3,2) and (2,3) will be counted as 1.

• If there is a number occurring multiple times in an array then it will be considered exactly twice to form one pair. For example, if an array has elements as {2, 2, 2, 2} then the pair will be (2,2) and it will be counted as 1.

## For Example

Input − int arr = {6, 4, 10, 14}
Output − count is 2

Explanation − pairs with the sum in an array are (6,4) and (10,4) so count is 2

Input − int arr = {6, 6, 6 ,6, 6, 13}
Output − count is 0

Explanation − there is no pair in an array with the sum in the same array. So, count is 0.

## Approach used in the below program is as follows

• Create an array let’s say, arr[]

• Calculate the length of an array using the length() function that will return an integer value as per the elements in an array.

• Take a temporary variable that will store the count of elements.

• Create a map type variable let’s say mp

• Start loop for i to 0 and i less than the size of an array

• Create another map of pairs type variable let’s say par

• Start loop for i to 0 and i less than the size of an array

• Inside the loop, start another loop with j to i+1 and j less than the size of an array

• Inside the loop, check if mp[arr[i]+arr[j]] > 0 AND pr[{arr[i], arr[j] }] =0 then increment the count by 1

• Increment par[{ arr[i], arr[j] }] by 1

• Increment par[{ arr[j], arr[i] }] by 1

• Return the count

• Print the result.

## Example

Live Demo

#include <iostream>
#include <map>
using namespace std;
// Returns number of pairs in ar[0..n-1] with
// sum equal to 'sum'
int countpairs(int ar[], int n){
// Store counts of all elements in map m
// to find pair (ar[i], sum-ar[i])
// because (ar[i]) + (sum - ar[i]) = sum
map<int, int> mymap;
for (int i = 0; i < n; i++){
mymap[ar[i]]++;
}
// To remove duplicate items we use result map
map<pair<int, int>, int> p;
int result = 0;
// Considering all pairs
for (int i = 0; i < n; i++){
for (int j = i + 1; j < n; j++){
// If sum of current pair exists
if (mymap[ar[i] + ar[j]] > 0 && p[{ ar[i], ar[j] }] ==0){
result++;
}
// Inserting the current pair both ways to avoid
// duplicates.
p[{ ar[i], ar[j] }]++;
p[{ ar[j], ar[i] }]++;
}
}
return result;
}
// main function
int main(){
int ar[] = { 6, 4, 10, 14 };
int n = sizeof(ar) / sizeof(ar[0]);
cout << "count is "<<countpairs(ar, n);
return 0;
}

## Output

If we run the above code we will get the following output −

count is 2
Published on 15-May-2020 11:53:09