Count of pairs from Array with sum equal to twice their bitwise AND | C ++ implementation to find the pairs with equal sum and twice the bitwise AND of the pairs ; Map to store the occurrence of elements of array ; Function to find the pairs with equal sum and twice the bitwise AND of the pairs ; Loop to find the frequency of elements of array ; Function to find the count such pairs in the array ; if an element occurs more than once then the answer will by incremented by nC2 times ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; map < int , int > mp ; int find_pairs ( int ar [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mp [ ar [ i ] ] ++ ; } for ( auto i : mp ) { int count = i . second ; if ( count > 1 ) { ans += ( ( count * ( count - 1 ) ) / 2 ) ; } } return ans ; } int main ( ) { int ar [ ] = { 1 , 2 , 3 , 3 , 4 , 5 , 5 , 7 , 8 } ; int arr_size = ( sizeof ( ar ) / sizeof ( ar [ 0 ] ) ) ; cout << find_pairs ( ar , arr_size ) ; return 0 ; }