Maximize count of pairs whose Bitwise AND exceeds Bitwise XOR by replacing such pairs with their Bitwise AND | Python3 program for the above approach ; 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
from math import log2 NEW_LINE def countPairs ( arr , N ) : NEW_LINE INDENT freq = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = int ( log2 ( arr [ i ] ) ) NEW_LINE freq [ x ] = freq . get ( x , 0 ) + 1 NEW_LINE DEDENT pairs = 0 NEW_LINE for i in freq : NEW_LINE INDENT pairs += freq [ i ] - 1 NEW_LINE DEDENT return pairs NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 9 , 15 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countPairs ( arr , N ) ) NEW_LINE DEDENT