Count pairs with Bitwise XOR greater than both the elements of the pair | Python3 program for the above approach ; Function to count pairs whose XOR is greater than the pair itself ; Stores the count of pairs ; Sort the array ; Traverse the array ; If current element is 0 , then ignore it ; Traverse all the bits of element A [ i ] ; If current bit is set then update the count ; Update bits [ ] at the most significant bit of A [ i ] ; Print the count ; Driver Code ; Function Call
import math NEW_LINE def countPairs ( A , N ) : NEW_LINE INDENT count = 0 NEW_LINE A . sort ( ) NEW_LINE bits = [ 0 ] * 32 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( A [ i ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT for j in range ( 0 , 32 ) : NEW_LINE INDENT if ( ( ( 1 << j ) & A [ i ] ) == 0 ) : NEW_LINE INDENT count += bits [ j ] NEW_LINE DEDENT DEDENT bits [ ( int ) ( math . log ( A [ i ] , 2 ) ) ] += 1 NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE countPairs ( arr , N ) NEW_LINE DEDENT