- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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.

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.

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.

#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; }

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

count is 2

- Related Questions & Answers
- Count pairs in array whose sum is divisible by K in C++
- Count pairs in array whose sum is divisible by 4 in C++
- Count of pairs in an array whose sum is a perfect square in C++
- Count pairs with given sum in C++
- Pair whose sum exists in the array in JavaScript
- Count pairs whose products exist in array in C++
- Program to count number of fraction pairs whose sum is 1 in python
- Count pairs in a sorted array whose sum is less than x in C++
- Number of pairs whose sum is a power of 2 in C++
- Program to count maximum number of distinct pairs whose differences are larger than target in Python
- Count distinct pairs from two arrays having same sum of digits in C++
- Count pairs from two linked lists whose sum is equal to a given value in C++
- Count pairs from two BSTs whose sum is equal to a given value x in C++
- How to find all pairs of elements in Java array whose sum is equal to a given number?
- Count pairs in a binary tree whose sum is equal to a given value x in C++

Advertisements