Problem: Takahashi has N balls with positive integers written on them. The integer written on the i-th ball is A_i. He would like to form some number of pairs such that the sum of the integers written on each pair of balls is a power of 2. Note that a ball cannot belong to multiple pairs. Find the maximum possible number of pairs that can be formed.  Here, a positive integer is said to be a power of 2 when it can be written as 2^t using some non-negative integer t.

Submission attempts (in order):
#AGC030 B - Powers of two
import collections
import math
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
cnter=collections.Counter(A)
ans=0
for a in A:
    inv=2**(int(math.log2(a))+1)
    if cnter[a]==0:
        continue
    if cnter[a]>=2 and a==inv//2:a
        cnter[a]=cnter[a]-2
        ans=ans+1
        continue
    if inv-a in cnter:
        cnter[inv-a]=cnter[inv-a]-1
        ans=ans+1
print(ans)

#AGC030 B - Powers of two
import collections
import math
beki=[]
for i in range(1,32):
    beki.append(2**i)
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
cnter=collections.Counter(A)
ans=0
for a in A:
    inv=2**(int(math.log2(a))+1)
    if cnter[a]==0:
        continue
    if cnter[a]>=2 and a=inv//2:
        cnter[a]=cnter[a]-2
        ans=ans+1
        continue
    if inv-a in cnter:
        cnter[inv-a]=cnter[inv-a]-1
        ans=ans+1
print(ans)

#AGC030 B - Powers of two
import collections
beki=[]
for i in range(1,32):
    beki.append(2**i)
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
cnter=collections.Counter(A)
ans=0
for i in (reversed(beki)):
    for a in A:
        if (i//2<a<i and (i-a in cnter) )or(i//2==a and cnter[a]>=2):
            ans=ans+1
            A.remove(i-a)
print((N-len(A))//2)

#AGC030 B - Powers of two
beki=[]
for i in range(1,32):
    beki.append(2**i)
def tasitekesu(data,n):
    result=[]
    kosu=0
    for i in range(len(data)):
        if i in result:
            continue
        for j in range(i):
            if j in result:
                continue
            if data[i]+data[j]==n:
                result.append(i)
                result.append(j)
                kosu=kosu+1
                break
    result.sort(reverse=True)
    for i in result:
        del data[i]
    return [data,kosu]

N=int(input())
A=input().split()
ans=0
for i in range(N):
    A[i]=int(A[i])
sorted(A)
for i in (reversed(beki)):
    kesi=[]
    for j in range(len(A)):
        if j==len(A):
            break
        if i-A[j] in A:
            A.remove(i-A[j])
            kesi.append(j)
            ans=ans+1
    for j in (reversed(kesi)):
        A.pop(j)
print(ans)

#AGC030 B - Powers of two
import collections
beki=[]
for i in range(1,32):
    beki.append(2**i)
N=int(input())
A=map(int,input().split())
A.sorted(reverse=True)
cnter=collections.Counter(A)
ans=0
for i in (reversed(beki)):
    for a in A:
        if (i//2<a<i and (i-a in cnter) )or(i//2==a and cnter[a]>=2):
            ans=ans+1
            A.remove(i-a)
print((N-len(A))//2)
            

#AGC030 B - Powers of two
import collections
import math
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
cnter=collections.Counter(A)
ans=0
for a in A:
    inv=2**(int(math.log2(a))+1)
    if cnter[a]==0:
        continue
    if cnter[a]>=2 and a==inv//2:a
        cnter[a]=cnter[a]-2
        ans=ans+1
        continue
    if inv-a in cnter:
        cnter[inv-a]=cnter[inv-a]-1
        ans=ans+1
print(ans)

#AGC030 B - Powers of two
import collections
import math
beki=[]
for i in range(1,32):
    beki.append(2**i)
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
cnter=collections.Counter(A)
ans=0
for a in A:
    inv=2**(int(math.log2(a))+1)
    if cnter[a]==0:
        continue
    if inv-a in cnter:
        cnter[inv-a]=cnter[inv-a]-1
        ans=ans+1
print(ans)

#AGC030 B - Powers of two
beki=[]
for i in range(1,32):
    beki.append(2**i)
N=int(input())
A=input().split()
for i in range(N):
    A[i]=int(A[i])
sorted(A)
for i in (reversed(beki)):
    B=A
    for j in B:
        if j==i/2 and B.count(j)=>2:
            A.remove(j)
            A.remove(j)
        if i-j in A:
            A.remove(j)
            A.remove(i-j)
print((N-len(A))//2)

#AGC030 B - Powers of two
import collections
import math
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
cnter=collections.Counter(A)
ans=0
for a in A:
    inv=2**(int(math.log2(a))+1)
    if cnter[a]==0:
        continue
    if cnter[a]>=2 and a==inv//2:
        cnter[a]=cnter[a]-2
        ans=ans+1
        continue
    if inv-a in cnter:
        cnter[inv-a]=cnter[inv-a]-1
        ans=ans+1
print(ans)

#AGC030 B - Powers of two
import collections
import math
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
cnter=collections.Counter(A)
ans=0
for a in A:
    inv=2**(int(math.log2(a))+1)
    if cnter[a]==0:
        continue
    if inv==2*a and cnter[inv-a]<=1:
      	continue
    if cnter[inv-a]>0:
        cnter[inv-a]=cnter[inv-a]-1
        cnter[a]=cnter[a]-1
        ans=ans+1
print(ans)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Runtime Error

Wrong Answer

Accepted