Count pairs from an array whose Bitwise OR is greater than Bitwise AND | Python 3 program for the above approach ; Function to count the number of pairs ( i , j ) their Bitwise OR is greater than Bitwise AND ; Total number of pairs possible from the array ; Stores frequency of each array element ; Traverse the array A [ ] ; Increment ump [ A [ i ] ] by 1 ; Traverse the Hashmap ump ; Subtract those pairs ( i , j ) from count which has the same element on index i and j ( i < j ) ; Print the result ; Driver Code ; Function Call
from collections import defaultdict NEW_LINE def countPairs ( A , n ) : NEW_LINE INDENT count = ( n * ( n - 1 ) ) // 2 NEW_LINE ump = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT ump [ A [ i ] ] += 1 NEW_LINE DEDENT for it in ump . keys ( ) : NEW_LINE INDENT c = ump [ it ] NEW_LINE count = count - ( c * ( c - 1 ) ) // 2 NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 4 , 7 ] NEW_LINE N = len ( A ) NEW_LINE countPairs ( A , N ) NEW_LINE DEDENT