Count pairs of equal array elements remaining after every removal
Given an array arr[] of size N, the task for every array element arr[i], is to count the number of pairs of equal elements that can be obtained by removing arr[i] from the array.
Examples:
Input: arr[] = { 1, 1, 1, 2 }
Output: 1 1 1 3
Explanation:
Removing arr[0] from the array modifies arr[] to { 1, 1, 2 } and count of pairs of equal elements = 1
Removing arr[1] from the array modifies arr[] to { 1, 1, 2 } and count of pairs of equal elements = 1
Removing arr[2] from the array modifies arr[] to { 1, 1, 2 } and count of pairs of equal elements = 1
Removing arr[3] from the array modifies arr[] to { 1, 1, 1 } and count of pairs of equal elements = 3
Therefore, the required output is 1 1 1 3.
Input: arr[] = { 2, 3, 4, 3, 2 }
Output: 1 1 2 1 1
Brute Force Approach:
A brute force approach to solve this problem would be to iterate through each element in the array, remove it, and then count the number of pairs of equal elements in the modified array. This can be done using a nested loop. The outer loop iterates through each element of the array, and the inner loop iterates through the remaining elements to count the number of pairs of equal elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void pairs_after_removing( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
int count = 0;
int modified[N-1];
int index = 0;
for ( int j = 0; j < N; j++) {
if (j != i) {
modified[index++] = arr[j];
}
}
for ( int j = 0; j < N-1; j++) {
for ( int k = j+1; k < N-1; k++) {
if (modified[j] == modified[k]) {
count++;
}
}
}
cout << count << " " ;
}
}
int main()
{
int arr[] = { 2, 3, 4, 3, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
pairs_after_removing(arr, N);
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
static void pairs_after_removing( int arr[], int N) {
for ( int i = 0 ; i < N; i++) {
int count = 0 ;
int modified[] = new int [N - 1 ];
int index = 0 ;
for ( int j = 0 ; j < N; j++) {
if (j != i) {
modified[index++] = arr[j];
}
}
for ( int j = 0 ; j < N - 1 ; j++) {
for ( int k = j + 1 ; k < N - 1 ; k++) {
if (modified[j] == modified[k]) {
count++;
}
}
}
System.out.print(count + " " );
}
}
public static void main(String[] args) {
int arr[] = { 2 , 3 , 4 , 3 , 2 };
int N = arr.length;
pairs_after_removing(arr, N);
}
}
|
Python3
def pairs_after_removing(arr, N):
for i in range (N):
count = 0
modified = [arr[j] for j in range (N) if j ! = i]
for j in range (N - 1 ):
for k in range (j + 1 , N - 1 ):
if modified[j] = = modified[k]:
count + = 1
print (count, end = " " )
if __name__ = = "__main__" :
arr = [ 2 , 3 , 4 , 3 , 2 ]
N = len (arr)
pairs_after_removing(arr, N)
|
C#
using System;
public class GFG {
static void pairs_after_removing( int [] arr, int N) {
for ( int i = 0; i < N; i++) {
int count = 0;
int [] modified = new int [N - 1];
int index = 0;
for ( int j = 0; j < N; j++) {
if (j != i) {
modified[index++] = arr[j];
}
}
for ( int j = 0; j < N - 1; j++) {
for ( int k = j + 1; k < N - 1; k++) {
if (modified[j] == modified[k]) {
count++;
}
}
}
Console.Write(count + " " );
}
}
public static void Main( string [] args) {
int [] arr = { 2, 3, 4, 3, 2 };
int N = arr.Length;
pairs_after_removing(arr, N);
}
}
|
Javascript
function pairs_after_removing(arr) {
const N = arr.length;
for (let i = 0; i < N; i++) {
let count = 0;
const modified = arr.filter((_, index) => index !== i);
for (let j = 0; j < N - 1; j++) {
for (let k = j + 1; k < N - 1; k++) {
if (modified[j] === modified[k]) {
count++;
}
}
}
process.stdout.write(count + " " );
}
}
function main() {
const arr = [2, 3, 4, 3, 2];
pairs_after_removing(arr);
}
main();
|
Time Complexity: O(N^3), where N is the size of the input array.
Auxiliary Space: O(N^2), where N is the size of the input array. This is because the modified array created in the inner loop has a maximum size of N-1, and we create N-1 modified arrays. Therefore, the total space required is (N-1) * (N-1) = N^2 – 2N + 1, which is O(N^2) asymptotically.
Efficient Approach: Follow the steps below to solve the problem:
- Initialize a map, say mp, to store the frequency of each distinct element of the array.
- Initialize a variable, say cntPairs, to store the total count of pairs of equal array elements.
- Traverse the map and store the total count of pairs of equal elements by incrementing the value of cntPairs by (mp[i] * (mp[i] – 1)) / 2.
- Finally, traverse the array. For every ith element, print the value of (cntPairs – mp[i] + 1), which denotes the count of pairs of equal array elements by removing arr[i] from the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void pairs_after_removing( int arr[], int N)
{
int cntPairs = 0;
unordered_map< int , int > mp;
for ( int i = 0; i < N; i++) {
mp[arr[i]]++;
}
for ( auto element : mp) {
int i = element.first;
cntPairs += mp[i] * (mp[i] - 1) / 2;
}
for ( int i = 0; i < N; i++) {
int pairs_after_arr_i_removed
= cntPairs + 1 - mp[arr[i]];
cout << pairs_after_arr_i_removed << ' ' ;
}
return ;
}
int main()
{
int arr[] = { 2, 3, 4, 3, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
pairs_after_removing(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void pairs_after_removing( int arr[], int N)
{
int cntPairs = 0 ;
Map<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < N; i++)
{
mp.put(arr[i], mp.getOrDefault(arr[i], 0 ) + 1 );
}
for (Map.Entry<Integer,
Integer> element : mp.entrySet())
{
int i = element.getKey();
cntPairs += mp.get(i) * (mp.get(i) - 1 ) / 2 ;
}
for ( int i = 0 ; i < N; i++)
{
int pairs_after_arr_i_removed = cntPairs +
1 - mp.get(arr[i]);
System.out.print(pairs_after_arr_i_removed + " " );
}
return ;
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 4 , 3 , 2 };
int N = arr.length;
pairs_after_removing(arr, N);
}
}
|
Python3
def pairs_after_removing(arr, N):
cntPairs = 0
mp = {}
for i in arr:
mp[i] = mp.get(i, 0 ) + 1
for element in mp:
i = element
cntPairs + = mp[i] * (mp[i] - 1 ) / / 2
for i in range (N):
pairs_after_arr_i_removed = cntPairs + 1 - mp[arr[i]]
print (pairs_after_arr_i_removed, end = ' ' )
return
if __name__ = = '__main__' :
arr = [ 2 , 3 , 4 , 3 , 2 ]
N = len (arr)
pairs_after_removing(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void pairs_after_removing( int [] arr, int N)
{
int cntPairs = 0;
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < N; i++)
{
if (mp.ContainsKey(arr[i]))
{
mp[arr[i]]++;
}
else
{
mp[arr[i]] = 1;
}
}
foreach (KeyValuePair< int ,
int > element in mp)
{
int i = element.Key;
cntPairs += mp[i] * (mp[i] - 1) / 2;
}
for ( int i = 0; i < N; i++)
{
int pairs_after_arr_i_removed = cntPairs +
1 - mp[arr[i]];
Console.Write(pairs_after_arr_i_removed + " " );
}
return ;
}
public static void Main()
{
int [] arr = { 2, 3, 4, 3, 2 };
int N = arr.Length;
pairs_after_removing(arr, N);
}
}
|
Javascript
<script>
function pairs_after_removing(arr, N)
{
var cntPairs = 0;
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);
}
}
mp.forEach((value, key) => {
var i = key;
cntPairs += mp.get(i) * (mp.get(i) - 1) / 2;
});
for ( var i = 0; i < N; i++) {
var pairs_after_arr_i_removed
= cntPairs + 1 - mp.get(arr[i]);
document.write( pairs_after_arr_i_removed + ' ' );
}
return ;
}
var arr = [2, 3, 4, 3, 2];
var N = arr.length;
pairs_after_removing(arr, N);
</script>
|
Time complexity: O(N)
Auxiliary space: O(N)
Last Updated :
07 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...