Count pairs of equal elements possible by excluding each array element once
Given an array arr[] of N integers, the task for each array element is to find the number of ways of choosing a pair of two equal elements excluding the current element.
Examples:
Input: arr[] = {1, 1, 2, 1, 2}
Output: 2 2 3 2 3
Explanation:
For arr[0] (= 1): The remaining array elements are {1, 2, 1, 2}. Possible choice of pairs are (1, 1), (2, 2). Therefore, count is 2.
For arr[1] (= 1): The remaining array elements are {1, 2, 1, 2}. Therefore, count is 2.
For arr[2] (= 2): The remaining array elements are {1, 1, 1, 2}. Possible choice of pairs are (arr[0], arr[1]), (arr[1], arr[2]) and (arr[0], arr[2]). Therefore, count is 3.
For arr[3] (= 1): The remaining elements are {1, 1, 2, 2}. Therefore, count is 2.
For arr[4] (= 2): The remaining elements are {1, 1, 2, 1}. Therefore, count is 3.
Input: arr[] = {1, 2, 1, 4, 2, 1, 4, 1}
Output: 5 7 5 7 7 5 7 5
Naive Approach: The simplest approach to solve this problem is to traverse the array for each array element, count all possible pairs of equal elements from the remaining array.
Implementation:-
C++
#include <bits/stdc++.h>
using namespace std;
void countEqualElementPairs( int arr[], int N)
{
for ( int i=0;i<N;i++)
{
int count=0;
for ( int j=0;j<N-1;j++)
{
if (j==i) continue ;
for ( int k=j+1;k<N;k++)
{
if (k==i) continue ;
if (arr[k]==arr[j])count++;
}
}
cout<<count<< " " ;
}
}
int main()
{
int arr[] = { 1, 1, 2, 1, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
countEqualElementPairs(arr, N);
}
|
Java
import java.io.*;
class GFG {
public static void countEqualElementPairs( int arr[], int N)
{
for ( int i= 0 ;i<N;i++)
{
int count= 0 ;
for ( int j= 0 ;j<N- 1 ;j++)
{
if (j==i) continue ;
for ( int k=j+ 1 ;k<N;k++)
{
if (k==i) continue ;
if (arr[k]==arr[j])count++;
}
}
System.out.print(count+ " " );
}
}
public static void main (String[] args) {
int N = 5 ;
int arr[]={ 1 , 1 , 2 , 1 , 2 };
countEqualElementPairs(arr,N);
}
}
|
Python3
def countEqualElementPairs(arr, N):
for i in range (N):
count = 0
for j in range (N - 1 ):
if j = = i:
continue
for k in range (j + 1 , N):
if k = = i:
continue
if arr[k] = = arr[j]:
count + = 1
print (count, end = " " )
if __name__ = = '__main__' :
arr = [ 1 , 1 , 2 , 1 , 2 ]
N = len (arr)
countEqualElementPairs(arr, N)
|
Javascript
function countEqualElementPairs(arr) {
const N = arr.length;
for (let i = 0; i < N; i++) {
let count = 0;
for (let j = 0; j < N - 1; j++) {
if (j === i) continue ;
for (let k = j + 1; k < N; k++) {
if (k === i) continue ;
if (arr[k] === arr[j]) count++;
}
}
console.log(count);
}
}
const arr = [1, 1, 2, 1, 2];
countEqualElementPairs(arr);
|
C#
using System;
class GFG {
static void countEqualElementPairs( int [] arr, int N)
{
for ( int i = 0; i < N; i++) {
int count = 0;
for ( int j = 0; j < N - 1; j++) {
if (j == i)
continue ;
for ( int k = j + 1; k < N; k++) {
if (k == i)
continue ;
if (arr[k] == arr[j])
count++;
}
}
Console.Write(count + " " );
}
}
public static void Main()
{
int [] arr = { 1, 1, 2, 1, 2 };
int N = arr.Length;
countEqualElementPairs(arr, N);
}
}
|
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the observation that, for any ith index (1 ≤ i ≤ N), calculate the following two values:
- The number of ways to choose two distinct elements having equal values from the array.
- The number of ways to choose an element from the N − 1 array elements other than the ith element such that their values are the same as the value of the ith element.
Follow the steps below to solve the problem:
- Initialize a map, say mp, to store the frequency of every array element.
- Traverse the map to count the number of pairs made up of equal values. Store the count in a variable, say total.
- Traverse the array and for every ith index, print total – (mp[arr[i]] – 1) as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countEqualElementPairs( int arr[], int N)
{
unordered_map< int , int > mp;
for ( int i = 0; i < N; i++) {
mp[arr[i]] += 1;
}
int total = 0;
for ( auto i : mp) {
total += (i.second * (i.second - 1)) / 2;
}
for ( int i = 0; i < N; i++) {
cout << total - (mp[arr[i]] - 1)
<< " " ;
}
}
int main()
{
int arr[] = { 1, 1, 2, 1, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
countEqualElementPairs(arr, N);
}
|
Java
import java.io.*;
import java.util.Map;
import java.util.HashMap;
class GFG
{
public static void countEqualElementPairs( int arr[],
int N)
{
HashMap<Integer, Integer> map = new HashMap<>();
for ( int i = 0 ; i < N; i++)
{
Integer k = map.get(arr[i]);
map.put(arr[i], (k == null ) ? 1 : k + 1 );
}
int total = 0 ;
for (Map.Entry<Integer, Integer> e :
map.entrySet())
{
total
+= (e.getValue() * (e.getValue() - 1 )) / 2 ;
}
for ( int i = 0 ; i < N; i++) {
System.out.print(total - (map.get(arr[i]) - 1 )
+ " " );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 1 , 2 , 1 , 2 };
int N = 5 ;
countEqualElementPairs(arr, N);
}
}
|
Python3
def countEqualElementPairs(arr, N):
mp = {}
for i in range (N):
if arr[i] in mp:
mp[arr[i]] + = 1
else :
mp[arr[i]] = 1
total = 0
for key,value in mp.items():
total + = (value * (value - 1 )) / 2
for i in range (N):
print ( int (total - (mp[arr[i]] - 1 )),end = " " )
if __name__ = = '__main__' :
arr = [ 1 , 1 , 2 , 1 , 2 ]
N = len (arr)
countEqualElementPairs(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void countEqualElementPairs( int [] arr, int N)
{
Dictionary< int ,
int > map = new Dictionary< int ,
int >();
for ( int i = 0; i < N; i++)
{
if (!map.ContainsKey(arr[i]))
map[arr[i]] = 1;
else
map[arr[i]]++;
}
int total = 0;
foreach (KeyValuePair< int , int > e in map)
{
total += (e.Value * (e.Value - 1)) / 2;
}
for ( int i = 0; i < N; i++)
{
Console.Write(total - (map[arr[i]] - 1) + " " );
}
}
public static void Main()
{
int [] arr = { 1, 1, 2, 1, 2 };
int N = 5;
countEqualElementPairs(arr, N);
}
}
|
Javascript
<script>
function countEqualElementPairs(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 total = 0;
mp.forEach((value, key) => {
total += (value * (value - 1)) / 2;
});
for ( var i = 0; i < N; i++)
{
document.write(total -
(mp.get(arr[i]) - 1) + " " );
}
}
var arr = [ 1, 1, 2, 1, 2 ];
var N = arr.length;
countEqualElementPairs(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
22 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...