Sum of Bitwise AND of sum of pairs and their Bitwise AND from a given array | Python 3 program for the above approach ; Function to find the sum of Bitwise AND of sum of pairs and their Bitwise AND from a given array ; Stores the total sum ; Check if jth bit is set ; Stores the right shifted element by ( i + 1 ) ; Update the value of X ; Push in vector vec ; Sort the vector in ascending order ; Traverse the vector vec ; Stores the value 2 ^ ( i + 1 ) - 2 ^ ( i ) - vec [ j ] ; Stores count of numbers whose value > Y ; Update the ans ; Return the ans ; Driver Code
M = 1000000007 NEW_LINE from bisect import bisect_left NEW_LINE def findSum ( A , N ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 30 ) : NEW_LINE INDENT vec = [ ] NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( ( A [ j ] >> i ) & 1 ) : NEW_LINE INDENT X = ( A [ j ] >> ( i + 1 ) ) NEW_LINE X = X * ( 1 << ( i + 1 ) ) NEW_LINE X %= M NEW_LINE vec . append ( A [ j ] - X ) NEW_LINE DEDENT DEDENT vec . sort ( reverse = False ) NEW_LINE for j in range ( len ( vec ) ) : NEW_LINE INDENT Y = ( 1 << ( i + 1 ) ) + ( 1 << i ) - vec [ j ] NEW_LINE temp = vec [ j + 1 : ] NEW_LINE idx = int ( bisect_left ( temp , Y ) ) NEW_LINE ans += ( ( len ( vec ) - idx ) * ( 1 << i ) ) NEW_LINE ans %= M NEW_LINE DEDENT DEDENT ans /= 7 NEW_LINE print ( int ( ans % M ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE findSum ( arr , N ) NEW_LINE DEDENT