Count pairs whose Bitwise AND exceeds Bitwise XOR from a given array | Function to count pairs that satisfy the above condition ; Stores the count of pairs ; Stores the count of array elements having same positions of MSB ; Traverse the array ; Stores the index of MSB of array elements ; Calculate number of pairs ; Given Input ; Function call to count pairs satisfying the given condition
function cntPairs ( arr , N ) { var res = 0 ; var bit = Array ( 32 ) . fill ( 0 ) ; var i ; for ( i = 0 ; i < N ; i ++ ) { var pos = Math . ceil ( Math . log2 ( arr [ i ] ) ) ; bit [ pos ] += 1 ; } for ( i = 0 ; i < 32 ; i ++ ) { res += Math . ceil ( ( bit [ i ] * ( bit [ i ] - 1 ) ) / 2 ) ; } return res ; } arr = [ 1 , 2 , 3 , 4 ] ; N = arr . length ; document . write ( cntPairs ( arr , N ) ) ;