Count pairs of equal array elements remaining after every removal | C ++ program to implement the above approach ; Function to count pairs of equal elements by removing arr [ i ] from the array ; Stores total count of pairs of equal elements ; Store frequency of each distinct array element ; Traverse the array ; Update frequency of arr [ i ] ; Traverse the map ; Stores key of an element ; Traverse the array ; Stores count of pairs of equal element by removing arr [ i ] ; Driver Code ; Given Array
#include <bits/stdc++.h> NEW_LINE 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 ; }