Count of unique pairs (arr[i], arr[j]) such that i < j
Last Updated :
07 Aug, 2022
Given an array arr[], the task is to print the count of unique pairs (arr[i], arr[j]) such that i < j.
Examples:
Input: arr[] = {1, 2, 1, 4, 5, 2}
Output: 11
The possible pairs are (1, 2), (1, 1), (1, 4), (1, 5), (2, 1), (2, 4), (2, 5), (2, 2), (4, 5), (4, 2), (5, 2)
Input: arr[] = {1, 2, 3, 4}
Output: 6
The possible pairs are (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)
Naive approach: The easiest way is to iterate through every possible pair and if it satisfies the condition, then add it to a set. Then, we can return the size of the set as our answer.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <set>
using namespace std;
int getPairs( int arr[], int n)
{
set<pair< int , int >> h;
for ( int i = 0; i < (n - 1); i++)
{
for ( int j = i + 1; j < n; j++)
{
h.insert(make_pair(arr[i], arr[j]));
}
}
return h.size();
}
int main()
{
int arr[] = { 1, 2, 2, 4, 2, 5, 3, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
printf ( "%d" , getPairs(arr, n)) ;
return 0;
}
|
Java
import java.util.HashSet;
import javafx.util.Pair;
class GFG {
static int getPairs( int arr[], int n)
{
HashSet<Pair> h = new HashSet<Pair>();
for ( int i = 0 ; i < n - 1 ; i++) {
for ( int j = i + 1 ; j < n; j++) {
Pair<Integer, Integer> p
= new Pair<>(arr[i], arr[j]);
h.add(p);
}
}
return h.size();
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 2 , 4 , 2 , 5 , 3 , 5 };
int n = arr.length;
System.out.println(getPairs(arr, n));
}
}
|
Python3
def getPairs(arr, n) :
h = set ()
for i in range (n - 1 ) :
for j in range (i + 1 , n) :
h.add((arr[i], arr[j]));
return len (h);
if __name__ = = "__main__" :
arr = [ 1 , 2 , 2 , 4 , 2 , 5 , 3 , 5 ]
n = len (arr)
print (getPairs(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int getPairs( int []arr, int n)
{
HashSet<Tuple< int ,
int >> h = new HashSet<Tuple< int ,
int >>();
for ( int i = 0; i < n - 1; i++)
{
for ( int j = i + 1; j < n; j++)
{
Tuple< int ,
int > p = new Tuple< int ,
int >(arr[i],
arr[j]);
h.Add(p);
}
}
return h.Count;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 2, 4, 2, 5, 3, 5 };
int n = arr.Length;
Console.WriteLine(getPairs(arr, n));
}
}
|
Javascript
<script>
function getPairs(arr, n)
{
var h = new Set();
for ( var i = 0; i < (n - 1); i++)
{
for ( var j = i + 1; j < n; j++)
{
h.add([arr[i], arr[j]]);
}
}
return h.size/2;
}
var arr = [ 1, 2, 2, 4, 2, 5, 3, 5 ];
var n = arr.length;
document.write(getPairs(arr, n));
</script>
|
Time Complexity: O(n2)
Note: Please use an offline IDE to compile the above code. Online compilers might not support JavaFX.
Efficient Approach: Every element arr[i] can form a pair with the element arr[j] if i < j. But (arr[i], arr[j]) should be unique therefore for every unique arr[i], possible pairs will be equal to the number of distinct numbers in the sub-array arr[i + 1], arr[i + 2], …, arr[n – 1]. So for every arr[i], we will find the unique elements from right to left. For this task, it is easy to keep track of the elements visited by using a Hash Table. In this way, we will have unique arr[i] for every unique arr[j]. Now, we will sum these values for every unique arr[i] which is the desired count of pairs.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int getPairs( int a[], int n)
{
set< int > visited1;
int un[n] ;
un[n - 1] = 0;
int count = 0;
for ( int i = n - 1; i > 0; i--)
{
auto pos = visited1.find(a[i]);
if (pos != visited1.end())
un[i - 1] = count;
else
un[i - 1] = ++count;
visited1.insert(a[i]);
}
set< int >visited2;
int answer = 0;
for ( int i = 0; i < n - 1; i++)
{
auto pos = visited2.find(a[i]);
if (pos != visited2.end())
continue ;
answer += un[i];
visited2.insert(a[i]);
}
return answer;
}
int main()
{
int a[] = { 1, 2, 2, 4, 2, 5, 3, 5 };
int n = sizeof (a)/ sizeof (a[0]);
cout<<(getPairs(a, n));
}
|
Java
import java.util.HashSet;
public class GFG {
static int getPairs( int a[], int n)
{
HashSet<Integer> visited1 = new HashSet<Integer>();
int un[] = new int [n];
un[n - 1 ] = 0 ;
int count = 0 ;
for ( int i = n - 1 ; i > 0 ; i--) {
if (visited1.contains(a[i]))
un[i - 1 ] = count;
else
un[i - 1 ] = ++count;
visited1.add(a[i]);
}
HashSet<Integer> visited2 = new HashSet<Integer>();
int answer = 0 ;
for ( int i = 0 ; i < n - 1 ; i++) {
if (visited2.contains(a[i]))
continue ;
answer += un[i];
visited2.add(a[i]);
}
return answer;
}
public static void main(String[] args)
{
int a[] = { 1 , 2 , 2 , 4 , 2 , 5 , 3 , 5 };
int n = a.length;
System.out.println(getPairs(a, n));
}
}
|
Python3
def getPairs(a, n):
visited1 = set ()
un = [ 0 ] * n
un[n - 1 ] = 0
count = 0
for i in range (n - 1 , - 1 , - 1 ):
if (a[i] in visited1):
un[i - 1 ] = count
else :
count + = 1
un[i - 1 ] = count
visited1.add(a[i])
visited2 = set ()
answer = 0
for i in range (n - 1 ):
if (a[i] in visited2):
continue
answer + = un[i]
visited2.add(a[i])
return answer
a = [ 1 , 2 , 2 , 4 , 2 , 5 , 3 , 5 ]
n = len (a)
print (getPairs(a, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int getPairs( int []a, int n)
{
HashSet< int > visited1 = new HashSet< int >();
int []un = new int [n];
un[n - 1] = 0;
int count = 0;
for ( int i = n - 1; i > 0; i--)
{
if (visited1.Contains(a[i]))
un[i - 1] = count;
else
un[i - 1] = ++count;
visited1.Add(a[i]);
}
HashSet< int > visited2 = new HashSet< int >();
int answer = 0;
for ( int i = 0; i < n - 1; i++)
{
if (visited2.Contains(a[i]))
continue ;
answer += un[i];
visited2.Add(a[i]);
}
return answer;
}
public static void Main(String[] args)
{
int []a = { 1, 2, 2, 4, 2, 5, 3, 5 };
int n = a.Length;
Console.WriteLine(getPairs(a, n));
}
}
|
Javascript
<script>
function getPairs(a, n)
{
let visited1 = new Set();
let un = Array.from({length: n}, (_, i) => 0);
un[n - 1] = 0;
let count = 0;
for (let i = n - 1; i > 0; i--) {
if (visited1.has(a[i]))
un[i - 1] = count;
else
un[i - 1] = ++count;
visited1.add(a[i]);
}
let visited2 = new Set();
let answer = 0;
for (let i = 0; i < n - 1; i++) {
if (visited2.has(a[i]))
continue ;
answer += un[i];
visited2.add(a[i]);
}
return answer;
}
let a = [ 1, 2, 2, 4, 2, 5, 3, 5 ];
let n = a.length;
document.write(getPairs(a, n));
</script>
|
Time Complexity: O(nlogn)
Auxiliary Space: O(n) as using auxiliary set
Share your thoughts in the comments
Please Login to comment...