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 ; Initialize the dictionary M ; Populate 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
def countOfPairs ( arr , N , X ) : NEW_LINE INDENT count = 0 NEW_LINE M = dict ( ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT k = arr [ i ] & X NEW_LINE if k in M : NEW_LINE INDENT M [ k ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT M [ k ] = 1 NEW_LINE DEDENT DEDENT for m in M . keys ( ) : NEW_LINE INDENT p = M [ m ] NEW_LINE count += p * ( p - 1 ) // 2 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 5 , 4 , 6 , 7 ] NEW_LINE X = 6 NEW_LINE N = len ( arr ) NEW_LINE print ( countOfPairs ( arr , N , X ) ) NEW_LINE DEDENT