Maximize count of pairs whose Bitwise AND exceeds Bitwise XOR by replacing such pairs with their Bitwise AND | 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
function countPairs ( arr , N ) { var freq = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( freq . has ( parseInt ( Math . log2 ( arr [ i ] ) ) ) ) { freq . set ( parseInt ( Math . log2 ( arr [ i ] ) ) , freq . get ( parseInt ( Math . log2 ( arr [ i ] ) ) ) + 1 ) ; } else { freq . set ( parseInt ( Math . log2 ( arr [ i ] ) ) , 1 ) ; } } var pairs = 0 ; freq . forEach ( ( value , key ) => { pairs += value - 1 ; } ) ; return pairs ; } var arr = [ 12 , 9 , 15 , 7 ] ; var N = arr . length ; document . write ( countPairs ( arr , N ) ) ;