Count all distinct pairs of repeating elements from the array for every array element
Last Updated :
10 Nov, 2021
Given an array arr[] of N integers. For each element in the array, the task is to count the possible pairs (i, j), excluding the current element, such that i < j and arr[i] = arr[j].
Examples:
Input: arr[] = {1, 1, 2, 1, 2}
Output: 2 2 3 2 3
Explanation:
For index 1, remaining elements after excluding current element are [1, 2, 1, 2]. So the count is 2.
For index 2, remaining elements after excluding element at index 2 are [1, 2, 1, 2]. So the count is 2.
For index 3, remaining elements after excluding element at index 3 are [1, 1, 1, 2]. So the count is 3.
For index 4, remaining elements after excluding element at index 4 are [1, 1, 2, 2]. So the count is 2.
For index 5, remaining elements after excluding element at index 5 are [1, 1, 2, 1. So the count is 3.
Input: arr[] = {1, 2, 3, 4}
Output: 0 0 0 0
Explanation:
Since all the elements are distinct, so no pair with equal value exists.
Naive Approach: The naive idea is to traverse the given array and for each element exclude the current element from the array and with the remaining array elements find all the possible pairs (i, j) such that arr[i] is equal to arr[j] and i < j. Print the count of pairs for each element.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The idea is to store the frequency of every element and count all the possible pairs(say cnt) with the given conditions. After the above steps for each element remove the count of equal possible pairs from the total count cnt and print that value. Follow the below steps to solve the problem:
- Store the frequency of each element in Map.
- Create a variable to store the contribution of each element.
- Contribution of some number x can be calculated as freq[x]*(freq[x] – 1) divided by 2, where freq[x] is the frequency of x.
- Traverse the given array and remove the contribution of each element from the total count and store it in ans[].
- Print all the elements stored in ans[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define int long long int
using namespace std;
void solve( int arr[], int n)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++) {
mp[arr[i]]++;
}
int cnt = 0;
for ( auto x : mp) {
cnt += ((x.second)
* (x.second - 1) / 2);
}
int ans[n];
for ( int i = 0; i < n; i++) {
ans[i] = cnt - (mp[arr[i]] - 1);
}
for ( int i = 0; i < n; i++) {
cout << ans[i] << " " ;
}
}
int32_t main()
{
int arr[] = { 1, 1, 2, 1, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
solve(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void solve( int arr[],
int n)
{
HashMap<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < n; i++)
{
if (mp.containsKey(arr[i]))
{
mp.put(arr[i], mp.get(arr[i]) + 1 );
}
else
{
mp.put(arr[i], 1 );
}
}
int cnt = 0 ;
for (Map.Entry<Integer,
Integer> x : mp.entrySet())
{
cnt += ((x.getValue()) *
(x.getValue() - 1 ) / 2 );
}
int []ans = new int [n];
for ( int i = 0 ; i < n; i++)
{
ans[i] = cnt - (mp.get(arr[i]) - 1 );
}
for ( int i = 0 ; i < n; i++)
{
System.out.print(ans[i] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 1 , 2 , 1 , 2 };
int N = arr.length;
solve(arr, N);
}
}
|
Python3
def solve(arr, n):
mp = {}
for i in arr:
mp[i] = mp.get(i, 0 ) + 1
cnt = 0
for x in mp:
cnt + = ((mp[x]) *
(mp[x] - 1 ) / / 2 )
ans = [ 0 ] * n
for i in range (n):
ans[i] = cnt - (mp[arr[i]] - 1 )
for i in ans:
print (i, end = " " )
if __name__ = = '__main__' :
arr = [ 1 , 1 , 2 , 1 , 2 ]
N = len (arr)
solve(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void solve( int []arr,
int n)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
{
if (mp.ContainsKey(arr[i]))
{
mp[arr[i]] = mp[arr[i]] + 1;
}
else
{
mp.Add(arr[i], 1);
}
}
int cnt = 0;
foreach (KeyValuePair< int ,
int > x in mp)
{
cnt += ((x.Value) *
(x.Value - 1) / 2);
}
int []ans = new int [n];
for ( int i = 0; i < n; i++)
{
ans[i] = cnt - (mp[arr[i]] - 1);
}
for ( int i = 0; i < n; i++)
{
Console.Write(ans[i] + " " );
}
}
public static void Main(String[] args)
{
int []arr = {1, 1, 2, 1, 2};
int N = arr.Length;
solve(arr, N);
}
}
|
Javascript
<script>
function solve(arr, n)
{
var mp = new Map();
for ( var i = 0; i < n; i++) {
if (mp.has(arr[i]))
mp.set(arr[i], mp.get(arr[i])+1)
else
mp.set(arr[i], 1);
}
var cnt = 0;
mp.forEach((value, key) => {
cnt += ((value)
* (value - 1) / 2);
});
var ans = Array(n);
for ( var i = 0; i < n; i++) {
ans[i] = cnt - (mp.get(arr[i]) - 1);
}
for ( var i = 0; i < n; i++) {
document.write( ans[i] + " " );
}
}
var arr = [1, 1, 2, 1, 2];
var N = arr.length;
solve(arr, N);
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...