Given an array arr[] of dimension N, the duty is to search out the entire variety of distinctive pair sums doable from the array parts.
Examples:
Enter: arr[] = {6, 1, 4, 3}
Output: 5
Rationalization: All pair doable are {6, 1}, {6, 4}, {6, 3}, {1, 4}, {1, 3}, {4, 3}. S
ums of those pairs are 7, 10, 9, 5, 4, 7. So distinctive sums 7, 10, 9, 5, 4. So reply is 5.Enter: arr[] = {8, 7, 6, 5, 4, 3, 2, 1}
Output: 13
Â
Strategy: This drawback might be effectively solved through the use of unordered_set.Â
Calculate all doable sum of pairs and retailer them in an unordered set. That is completed to retailer the shop parts in a mean time of O(1) with no worth repeating.Â
Algorithm:
- Use nested loops to get all doable sums of parts of the array.
- Retailer all of the doable sums into an unordered_set.
- Complete doable distinctive sums can be equal to the scale of unordered_set. So return the scale of the unordered set.
Under is the implementation of the above method:
C++
|
Java
|
Python3
|
Javascript
|
Time complexity: O(N2)
Auxiliary House: O(N)