Find the Number of Unique Pairs in an Array using C++

C++Server Side ProgrammingProgramming

We require the appropriate knowledge to create several unique pairs in an array syntax in C++. In finding the number of unique pairs, we count all unique pairs in a given array, i.e., all possible pairs can be formed where each pair should be unique. For example −

Input : array[ ] = { 5, 5, 9 }
Output : 4
Explanation : The number of all unique pairs are (5, 5), (5, 9), (9, 5) and (9, 9).

Input : array[ ] = { 5, 4, 3, 2, 2 }
Output : 16

Approach to find The Solution

There are Two Approaches for this Solution and they are −

Brute Force Approach

In this approach, we will traverse through each possible pair, add those pairs to a set, and finally find out the size of the set. The time complexity of this approach is O(n2 log n).

Example

#include <bits/stdc++.h>
using namespace std;
int main () {
   int arr[] = { 5, 4, 3, 2, 2 };
   int n = sizeof (arr) / sizeof (arr[0]);
   // declaring set to store pairs.
   set < pair < int, int >>set_of_pairs;

   for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
         set_of_pairs.insert (make_pair (arr[i], arr[j]));

   int result = set_of_pairs.size();

   cout <<"Number of unique pairs : " << result;
   return 0;
}

Output

Number of unique pairs : 16

Explanation of the above code

In this code, first, we declare a set variable, and then, using two loops, we are traversing through each possible pair and inserting each pair in the set using i and j. Then we are calculating the size of the set and printing the result.

Efficient Approach

Another approach is to first find out the number of unique numbers in an array; now, every other unique element, including itself, may create a pair with any other unique element, so the number of unique pairs equals the square of the number of all unique numbers. The time complexity of his approach is O(n).

Example

#include <bits/stdc++.h>
using namespace std;

int main () {
   int arr[] = { 5, 4, 3, 2, 2 };
   int n = sizeof (arr) / sizeof (arr[0]);

   // declaring set to store unique elements.

   unordered_set < int >set_of_elements;
   // inserting elements in the set.
   for (int i = 0; i < n; i++)
      set_of_elements.insert (arr[i]);

   int size = set_of_elements.size ();
   // finding number of unique pairs
   int result = size * size;

   cout << "Number of unique pairs in an array: " << result;
   return 0;
}

Output

Number of unique pairs : 16

Explanation of the above code

In this code, we declare a set and then go through each element of the array inserting every element in the set. After that, we calculated the size of the set and found the result from formula n2, and printed the output.

Conclusion

In this article, we solve the problem of finding the number of unique pairs in an array where we discuss two ways to solve the problem, i.e., simple and efficient. In a simple approach, we insert all possible pairs in a set with time complexity of O(n2 log n), and in an efficient approach, we find all unique numbers and find the result with n2. We can write the same program in other languages such as C, java, python, and other languages. Hope you find this article helpful.

raja
Published on 25-Nov-2021 13:02:31
Advertisements