# Count of pairs between two arrays such that the sums are distinct

Given two arrays **a[]** and **b[]**, the task is to find the count of all pairs **(a[i], b[j])** such that **a[i] + b[j]** is unique among all the pairs i.e. if two pairs have equal sum then only one will be counted in the result.

**Examples:**

Input:a[] = {3, 3}, b[] = {3}

Output:1

The two possible pairs are (a[0], b[0]) and (a[1], b[0]).

Pair 1: 3 + 3 = 6

Pair 2: 3 + 3 = 6

Input:a[] = {12, 2, 7}, b[] = {4, 3, 8}

Output:7

**Approach:** Initialise **count = 0** and run two loops to consider all possible pairs and store the sum of every pair in an unordered_set to check whether the sum has been obtained before. If it has then ignore the current pair else increment the **count**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count ` `// of pairs with distinct sum ` `int` `countPairs(` `int` `a[], ` `int` `b[], ` `int` `n, ` `int` `m) ` `{ ` ` ` ` ` `// To store the required count ` ` ` `int` `cnt = 0; ` ` ` ` ` `// Set to store the sum ` ` ` `// obtained for each pair ` ` ` `unordered_set<` `int` `> s; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `for` `(` `int` `j = 0; j < m; j++) { ` ` ` ` ` `// Sum of the current pair ` ` ` `int` `sum = a[i] + b[j]; ` ` ` ` ` `// If the sum obtained is distinct ` ` ` `if` `(s.count(sum) == 0) { ` ` ` ` ` `// Increment the count ` ` ` `cnt++; ` ` ` ` ` `// Insert sum in the set ` ` ` `s.insert(sum); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 12, 2, 7 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `int` `b[] = { 4, 3, 8 }; ` ` ` `int` `m = ` `sizeof` `(b) / ` `sizeof` `(b[0]); ` ` ` ` ` `cout << countPairs(a, b, n, m); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count ` `# of pairs with distinct sum ` `def` `countPairs(a, b, n, m): ` ` ` ` ` `# To store the required count ` ` ` `cnt ` `=` `0` ` ` ` ` `# Set to store the sum ` ` ` `# obtained for each pair ` ` ` `s` `=` `dict` `() ` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `for` `j ` `in` `range` `(m): ` ` ` ` ` `# Sum of the current pair ` ` ` `sum` `=` `a[i] ` `+` `b[j] ` ` ` ` ` `# If the sum obtained is distinct ` ` ` `if` `(` `sum` `not` `in` `s.keys()): ` ` ` `# Increment the count ` ` ` `cnt` `+` `=` `1` ` ` ` ` `# Insert sum in the set ` ` ` `s[` `sum` `]` `=` `1` ` ` ` ` `return` `cnt ` ` ` ` ` `# Driver code ` ` ` `a ` `=` `[ ` `12` `, ` `2` `, ` `7` `] ` `n ` `=` `len` `(a) ` `b ` `=` `[ ` `4` `, ` `3` `, ` `8` `] ` `m ` `=` `len` `(b) ` ` ` `print` `(countPairs(a, b, n, m)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

7

## Recommended Posts:

- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Count distinct pairs from two arrays having same sum of digits
- Count pairs formed by distinct element sub-arrays
- Find k pairs with smallest sums in two arrays
- Count number of ordered pairs with Even and Odd Sums
- Distinct pairs from given arrays (a[i], b[j]) such that (a[i] + b[j]) is a Fibonacci number
- Total distinct pairs of ugly numbers from two arrays
- Count all distinct pairs with difference equal to k
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Count number of distinct pairs whose sum exists in the given array
- Count Pairs from two arrays with even sum
- Given two arrays count all pairs whose sum is an odd number
- Count equal pairs from given string arrays
- Count pairs from two sorted arrays whose sum is equal to a given value x
- Count pairs from two arrays whose modulo operation yields K

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.