Count of pairs from Array with sum equal to twice their bitwise AND | Python3 implementation to find the pairs with equal sum and twice the bitwise AND of the pairs ; Map to store the occurrence of elements of array ; Function to find the pairs with equal sum and twice the bitwise AND of the pairs ; Loop to find the frequency of elements of array ; Function to find the count such pairs in the array ; If an element occurs more than once then the answer will by incremented by nC2 times ; Driver Code ; Function Call
from collections import defaultdict NEW_LINE mp = defaultdict ( int ) NEW_LINE def find_pairs ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT for i in mp . values ( ) : NEW_LINE INDENT count = i NEW_LINE if ( count > 1 ) : NEW_LINE INDENT ans += ( ( count * ( count - 1 ) ) // 2 ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 3 , 4 , 5 , 5 , 7 , 8 ] NEW_LINE arr_size = len ( arr ) NEW_LINE print ( find_pairs ( arr , arr_size ) ) NEW_LINE DEDENT