Count pairs whose Bitwise AND exceeds Bitwise XOR from a given array | Python3 program to implement the above approach ; 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 ; Driver Code ; Given Input ; Function call to count pairs satisfying the given condition
import math NEW_LINE def cntPairs ( arr , N ) : NEW_LINE INDENT res = 0 NEW_LINE bit = [ 0 ] * 32 NEW_LINE for i in range ( N ) : NEW_LINE INDENT pos = ( int ) ( math . log2 ( arr [ i ] ) ) NEW_LINE bit [ pos ] += 1 NEW_LINE DEDENT for i in range ( 32 ) : NEW_LINE INDENT res += ( bit [ i ] * ( bit [ i ] - 1 ) ) // 2 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( cntPairs ( arr , N ) ) NEW_LINE DEDENT