human
Number of pairs whose sum is a power of 2 | Function to return the count of valid pairs.
Storing occurrences of each element.
Sort the array in deceasing order.
Start taking largest element each time.
If element has already been paired.
Find the number which is greater than a [ i ] and power of two.
If there is a number which adds up with a [ i ] to form a power of two.
Edge case when a [ i ] and crr - a [ i ] is same and we have only one occurrence of a [ i ] then it cannot be paired.
Remove already paired elements.
Return the count.
Driver code"
How can the above be solved in Python?
 ... What is a code to number of pairs whose sum is a power of 2 and the other steps required?
assistant
#Here is code to number of pairs whose sum is a power of 2 and the other steps required.
 def countPairs ( a , n ) :
	mp = dict . fromkeys ( a , 0 )
	for i in range ( n ) :
		mp [ a [ i ] ] += 1
	a . sort ( reverse = True )
	count = 0
	for i in range ( n ) :
		if ( mp [ a [ i ] ] < 1 ) :
			continue
		cur = 1
		while ( cur <= a [ i ] ) :
			cur = cur << 1
		if ( cur - a [ i ] in mp . keys ( ) ) :
			if ( cur - a [ i ] == a [ i ] and mp [ a [ i ] ] == 1 ) :
				continue
			count += 1
			mp [ cur - a [ i ] ] -= 1
			mp [ a [ i ] ] -= 1
	return count
if __name__ == " _ _ main _ _ " :
	a = [ 3 , 11 , 14 , 5 , 13 ]
	n = len ( a )
	print ( countPairs ( a , n ) )