Count pairs from an array having sum of twice of their AND and XOR equal to K | C ++ program for the above approach ; Function to count number of pairs satisfying the given conditions ; Stores the frequency of array elements ; Stores the total number of pairs ; Traverse the array ; Add it to cnt ; Update frequency of current array element ; Print the count ; Driver Code ; Given array ; Size of the array ; Given value of K
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countPairs ( int arr [ ] , int N , int K ) { unordered_map < int , int > mp ; int cnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cnt += mp [ K - arr [ i ] ] ; mp [ arr [ i ] ] ++ ; } cout << cnt ; } int main ( ) { int arr [ ] = { 1 , 5 , 4 , 8 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 9 ; countPairs ( arr , N , K ) ; return 0 ; }