Count number of distinct pairs whose sum exists in the given array
Given an array of N positive integers. Count the number of pairs whose sum exists in the given array. While repeating pairs will not be counted again. And we can’t make a pair using same position element. Eg : (2, 1) and (1, 2) will be considered as only one pair.
Please read all examples carefully.
Examples:
Input : arr[] = {1, 2, 3, 5, 10}
Output : 2
Explanation : Here there are two such pairs:
(1 + 2) = 3, (2 + 3) = 5.
Note : Here we can't take pair (5, 5) as
we can see 5 is not coming twice
Input : arr[] = {1, 5, 6, 4, -1, 5}
Output : 4
Explanation : (1 + 5) = 6, (1 + 4) = 5,
(5 + -1) = 4, (6 + -1) = 5
Note : Here (1, 5) comes twice will be
considered as only one pair.
Input : arr[] = {5, 5, 5, 5, 10}
Output : 1
Explanation : (5 + 5) = 10
Note : Here (5, 5) comes twice will be
considered as only one pair.
The idea is to map of pairs to find unique elements. We first store elements and their counts in a map. Then we traverse array elements, for every pair of elements (arr[i], arr[j]), we check if (arr[i] + arr[j]) exists in array. If exists, then we check if it is already counted using map of pairs. If not already counted, then we increment count.
C++
// C++ implementation to find count of unique pairs // whose sum exists in given array #include <bits/stdc++.h> using namespace std; // Returns number of pairs in arr[0..n-1] with // sum equal to 'sum' int getPairsCount(int arr[], int n) { // Store counts of all elements in map m // to find pair (arr[i], sum-arr[i]) // because (arr[i]) + (sum - arr[i]) = sum map<int, int> m; for (int i = 0; i < n; i++) m[arr[i]]++; // To remove duplicate items we use result map map<pair<int, int>, int> pairs; int count = 0; // Initialize result // Consider all pairs for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // If sum of current pair exists if (m[arr[i] + arr[j]] > 0 && pairs[{ arr[i], arr[j] }] == 0) { count++; } // Insert current pair both ways to avoid // duplicates. pairs[{ arr[i], arr[j] }]++; pairs[{ arr[j], arr[i] }]++; } } return count; } // Driver function to test the above function int main() { int arr[] = { 1, 5, 6, 4, -1, 5, 10 }; int n = sizeof(arr) / sizeof(arr[0]); cout << getPairsCount(arr, n); return 0; } |
Python3
# Python3 implementation to find count # of unique pairs whose sum exists in # given array # Returns number of pairs in arr[0..n-1] # with sum equal to 'sum' def getPairsCount(arr, n): # Store counts of all elements in map m # to find pair (arr[i], sum-arr[i]) # because (arr[i]) + (sum - arr[i]) = sum m = dict() for i in range(n): m[arr[i]] = m.get(arr[i], 0) + 1 # To remove duplicate items # we use result map pairs1 = dict() count = 0 # Initialize result for i in range(n): for j in range(i + 1, n): l = arr[i] + arr[j] tp = (arr[i], arr[j]) if l in m.keys(): if tp not in pairs1.keys(): count += 1 pairs1[(arr[i], arr[j])] = 1 pairs1[(arr[j], arr[i])] = 1 return count # Driver Code arr = [1, 5, 6, 4, -1, 5, 10] n = len(arr) print(getPairsCount(arr, n)) # This code is contributed by Mohit Kumar |
Output:
6
This article is contributed by Harshit Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Count number of pairs in array having sum divisible by K | SET 2
- Count all distinct pairs with difference equal to k
- Count of pairs between two arrays such that the sums are distinct
- Count distinct pairs from two arrays having same sum of digits
- Count pairs formed by distinct element sub-arrays
- Distinct pairs from given arrays (a[i], b[j]) such that (a[i] + b[j]) is a Fibonacci number
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Count distinct elements in an array
- Absolute distinct count in a sorted array
- Count of distinct substrings of a string using Suffix Array
- Sort an array according to the increasing count of distinct Prime Factors
- Count subarrays having total distinct elements same as original array
- Count of odd and even sum pairs in an array
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j])
- Count of pairs (x, y) in an array such that x < y
Improved By : mohit kumar 29



