Maximize count of pairs whose Bitwise AND exceeds Bitwise XOR by replacing such pairs with their Bitwise AND | C ++ program for the above approach ; Function to count the number of pairs whose Bitwise AND is greater than the Bitwise XOR ; Stores the frequency of MSB of array elements ; Traverse the array ; Increment count of numbers having MSB at log ( arr [ i ] ) ; Stores total number of pairs ; Traverse the Map ; Return total count of pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int arr [ ] , int N ) { unordered_map < int , int > freq ; for ( int i = 0 ; i < N ; i ++ ) { freq [ log2 ( arr [ i ] ) ] ++ ; } int pairs = 0 ; for ( auto i : freq ) { pairs += i . second - 1 ; } return pairs ; } int main ( ) { int arr [ ] = { 12 , 9 , 15 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , N ) ; return 0 ; }