

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++
|
Java
|
Python3
|
Javascript
|
Time complexity: O(N2)
Auxiliary Space: O(N)