# Count of distinct pair sum in given Array

Given an array arr[] of size N, the task is to find the total number of unique pair sums possible from the array elements.

Examples:

Input: arr[] = {6, 1, 4, 3}
Output: 5
Explanation: All pair possible are {6, 1}, {6, 4}, {6, 3}, {1, 4}, {1, 3}, {4, 3}. S
ums of these pairs are 7, 10, 9, 5, 4, 7. So unique sums 7, 10, 9, 5, 4. So answer is 5.

Input: arr[] = {8, 7, 6, 5, 4, 3, 2, 1}
Output: 13

Approach: This problem can be efficiently solved by using unordered_set

Calculate all possible sum of pairs and store them in an unordered set. This is done to store the store elements in an average time of O(1) with no value repeating.

Algorithm:

• Use nested loops to get all possible sums of elements of the array.
• Store all the possible sums into an unordered_set.
• Total possible unique sums would be equal to the size of unordered_set. So return the size of the unordered set.

Below is the implementation of the above approach:

## C++

 `// C++ program to count the pairs with unique sums` `#include ` `using` `namespace` `std;`   `// Function to return the` `// total count of required pairs` `int` `count(``int` `arr[], ``int` `n)` `{` `    ``unordered_set<``int``> s;` `    ``// Add all possible sums into the set` `    ``for` `(``int` `i = 0; i < n - 1; i++) {` `        ``for` `(``int` `j = i + 1; j < n; j++) {` `            ``s.insert(arr[i] + arr[j]);` `        ``}` `    ``}` `    ``// Return the size of set` `    ``return` `s.size();` `}`   `// Driver code` `int` `main()` `{` `    ``int` `arr[] = { 6, 1, 4, 3 };` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);`   `    ``// Function call` `    ``cout << count(arr, N);` `    ``return` `0;` `}`

## Java

 `// Java program to find Count the pairs` `// with unique sums` `import` `java.util.*;`   `class` `GFG {` `    ``// Function to return the` `    ``// total count of required pairs` `    ``static` `int` `count(``int` `arr[], ``int` `n)` `    ``{` `        ``Set s = ``new` `HashSet();`   `        ``// Add all possible sums into the set` `        ``for` `(``int` `i = ``0``; i < n - ``1``; i++) {` `            ``for` `(``int` `j = i + ``1``; j < n; j++) {` `                ``s.add(arr[i] + arr[j]);` `            ``}` `        ``}` `        ``// Return the size of set` `        ``return` `s.size();` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `main(String args[])` `    ``{` `        ``int` `arr[] = { ``6``, ``1``, ``4``, ``3` `};` `        ``int` `N = arr.length;`   `        ``// Function call` `        ``System.out.println(count(arr, N));` `    ``}` `}`

## Python3

 `# Python program to find Count the pairs` `# with unique sums`   `# Function to return the ` `# total count of required pairs` `def` `count(arr, n):` `    ``s ``=` `set``()` `    `  `    ``# Add all possible sums into the set` `    ``for` `i ``in` `range``(n``-``1``):` `        ``for` `j ``in` `range``(i ``+` `1``, n):` `            ``s.add(arr[i] ``+` `arr[j])`   `    ``# Return the size of set` `    ``return` `int``(``len``(s))`   `# Driver code` `if` `__name__ ``=``=` `'__main__'``:` `    ``arr ``=` `[``6``, ``1``, ``4``, ``3``]` `    ``N ``=` `len``(arr)` `    `  `    ``# Function call` `    ``print``(count(arr, N))`

## C#

 `// C# program to count the pairs with unique sums` `using` `System;` `using` `System.Collections.Generic;`   `public` `class` `GFG {`   `    ``// Function to return the` `    ``// total count of required pairs` `    ``static` `int` `count(``int` `[]arr, ``int` `n)` `    ``{` `        ``HashSet<``int``> s = ``new` `HashSet<``int``>();`   `        ``// Add all possible sums into the set` `        ``for` `(``int` `i = 0; i < n - 1; i++) {` `            ``for` `(``int` `j = i + 1; j < n; j++) {` `                ``s.Add(arr[i] + arr[j]);` `            ``}` `        ``}` `        ``// Return the size of set` `        ``return` `s.Count;` `    ``}`   `    ``// Driver code` `    ``static` `public` `void` `Main()` `    ``{` `        ``int` `[]arr = { 6, 1, 4, 3 };` `        ``int` `N = arr.Length;`   `        ``// Function call` `        ``Console.WriteLine(count(arr, N));` `    ``}` `}`   `// This code is contributed by Rohit Pradhan`

## Javascript

 ``

Output

`5`

Time complexity: O(N2)
Auxiliary Space: O(N)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!