Count of pairs in Array such that bitwise AND of XOR of pair and X is 0 | Function to find the number of pairs that satisfy the given criteria i . e . , i < j and ( arr [ i ] ^ arr [ j ] ) & X is 0 ; Stores the resultant count of pairs ; Initializing the map M ; Populating the map ; Count number of pairs for every element in map using mathematical concept of combination ; As nC2 = n * ( n - 1 ) / 2 ; Return the resultant count ; Driver Code
function countOfPairs ( arr , N , X ) { let count = 0 ; let M = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( M . has ( arr [ i ] & X ) ) { M . set ( arr [ i ] & X , M . get ( arr [ i ] & X ) + 1 ) ; } else { M . set ( arr [ i ] & X , 1 ) ; } } for ( let m of M ) { let p = m [ 1 ] ; count += ( p * ( p - 1 ) ) / 2 ; } return count ; } let arr = [ 3 , 2 , 5 , 4 , 6 , 7 ] ; let X = 6 ; let N = arr . length ; document . write ( countOfPairs ( arr , N , X ) ) ;