Count pairs from an array having sum of twice of their AND and XOR equal to K | Python3 program for the above approach ; Function to count number of pairs satisfying the given conditions ; Stores the frequency of array elements ; Stores the total number of pairs ; Traverse the array ; Add it to cnt ; Update frequency of current array element ; Print the count ; Given array ; Size of the array ; Given value of K
from collections import defaultdict NEW_LINE def countPairs ( arr , N , K ) : NEW_LINE INDENT mp = defaultdict ( int ) NEW_LINE cnt = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT cnt += mp [ K - arr [ i ] ] NEW_LINE mp [ arr [ i ] ] += 1 NEW_LINE DEDENT print ( cnt ) NEW_LINE DEDENT arr = [ 1 , 5 , 4 , 8 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE K = 9 NEW_LINE countPairs ( arr , N , K ) NEW_LINE