Robinhood OA
Anonymous User
10969

Any optimal approach for this?
Question: count all the pairs with indices i<=j whose sum is a power of 2.

def pairSummingToPowerOfTwo(a):
    
    if len(a)==1 and (a[0] and (not(a[0] & (a[0] - 1))) ):
        return 1
    
    count  = 0
    for i in range(len(a)):
        for j in range(i, len(a)):

            sum = a[i] + a[j]
            if (sum and (not(sum & (sum - 1)))) == True:
                count += 1
                        
    return count
            
            
Comments (13)