Count of distinct pair sum in given Array
Last Updated :
01 Aug, 2022
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 <bits/stdc++.h>
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<Integer> s = new HashSet<Integer>();
// 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
<script>
// JavaScript program to find Count the pairs
// with unique sums
// Function to return the
// total count of required pairs
function count(arr, n) {
let s = new Set();
// Add all possible sums into the set
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
s.add(arr[i] + arr[j]);
}
}
// Return the size of set
return s.size;
}
// Driver code
let arr = [6, 1, 4, 3];
let N = arr.length;
// Function call
document.write(count(arr, N));
</script>
Time complexity: O(N2)
Auxiliary Space: O(N)
Similar Reads
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.
7 min read
Count pairs having distinct sum from a given range Given two positive integers L and R, the task is to find the number of pairs with elements from the range [L, R], whose sums are distinct. Examples: Input: L = 2, R = 3Output: 3Explanation: All possible pairs with elements from the range [2, 3] are {(2, 2), (2, 3), (3, 2), (3, 3)}. Since sum of the
5 min read
Count pairs formed by distinct element sub-arrays Given an array, count number of pairs that can be formed from all possible contiguous sub-arrays containing distinct numbers. The array contains positive numbers between 0 to n-1 where n is the size of the array. Examples: Input: [1, 4, 2, 4, 3, 2] Output: 8 The subarrays with distinct elements are
7 min read
2 Sum - Count distinct pairs with given sum Given an array arr[] of size n and an integer target, the task is to count the number of distinct pairs in the array whose sum is equal to target.Examples:Input: arr[] = { 5, 6, 5, 7, 7, 8 }, target = 13 Output: 2 Explanation: Distinct pairs with sum equal to 13 are (5, 8) and (6, 7). Input: arr[] =
15 min read
Count distinct sum of pairs possible from a given range Given two positive integers L and R ( where L ? R ), the task is to count the number of distinct integers that can be obtained by adding any pair of integers from the range [L, R]. Examples: Input: L = 3, R = 5Output: 11Explanation: All possible distinct sum of pairs are as follows: (3, 3). Sum = 6.
3 min read