Problem:
 Score : 600 points    Problem Statement 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 .      Constraints  1 \leq N \leq 2\times 10^5  1 \leq A_i \leq 10^9  A_i is an integer.         Input Input is given from Standard Input in the following format:  N  A_1  A_2  ...  A_N       Output Print the maximum possible number of pairs such that the sum of the integers written on each pair of balls is a power of 2 .        Sample Input 1 3
1 2 3      Sample Output 1 1  We can form one pair whose sum of the written numbers is 4 by pairing the first and third balls.
Note that we cannot pair the second ball with itself.       Sample Input 2 5
3 11 14 5 13      Sample Output 2 2

Solution (Python):



import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
a.sort()
f = defaultdict(lambda : 0)
for i in a:
    f[i] += 1
ans = 0
lst = [1<<i for i in range(32)]
for i in a[::-1]:
    if not f[i]:
        continue
    f[i] -= 1
    for j in lst:
        tmp = j - i
        if tmp <= 0:
            continue
        if f[tmp]:
            ans += 1
            f[tmp] -= 1
            break
print(ans)

Solution (Python):
from collections import*
from bisect import*
p=[1]
for _ in range(30):p+=p[-1]*2,
n,*a=map(int,open(0).read().split())
d=Counter(a)
c=0
for b in sorted(a)[::-1]:
  if d[b]<1:continue
  d[b]-=1
  x=p[bisect(p,b)]-b
  if d[x]:
    d[x]-=1
    c+=1
print(c)

Solution (Python):
from collections import*
from bisect import*
p=[1]
for _ in range(30):p+=p[-1]*2,
n,*a=map(int,open(0).read().split())
d=Counter(a)
c=0
for i,b in enumerate(sorted(a)[::-1]):
  if d[b]<1:continue
  d[b]-=1
  x=p[bisect(p,b)]-b
  if d[x]:
    d[x]-=1
    c+=1
print(c)

Solution (Python):
from collections import*
from bisect import*
p=[1]
for _ in range(30):p+=p[-1]*2,
n,*a=map(int,open(0).read().split())
d=Counter(a)
c=0
for i,b in enumerate(sorted(a)[::-1]):
  if d[b]<1:continue
  d[b]-=1
  x=p[bisect(p,b)]-b
  if d[x]:
    d[x]-=1
    c+=1
print(c)

Solution (Python):
from collections import defaultdict as d
n=int(input())
a=sorted(list(map(int,input().split())))
b=d(int)
for i in a:
    b[i]+=1
s=n-1
c=0
while s>0:
    if b[a[s]]>0:
        t=2**(len(bin(a[s]))-2)
        b[a[s]]-=1
        if b[t-a[s]]>0:
            b[t-a[s]]-=1
            c+=1
    s-=1
print(c)

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
c = Counter(a)

ans = 0
for i in a:
    if c[i] == 0:
        continue
    c[i] -= 1
    k = 1<<i.bit_length()
    j = k-i
    if j in c and c[j] > 0:
        ans += 1
        c[j] -= 1
print(ans)

Solution (Python):
N = int(input())
if N == 1:
  print(0)
  exit(0)
A = list(map(int, input().split()))
A.sort()
b = (A[-1] + A[-2]).bit_length()

vis = set()
res = 0
for t in range(b, -1, -1):
  s = 2 ** t
  r = N - 1
  
  for l in range(N):
    if l in vis: continue
    if l >= r: break
    while l < r:
      
      if r in vis:
        r -= 1
        continue
      if A[l] + A[r] < s:
        break
      elif A[l] + A[r] > s:
        r -= 1
        if r in vis:
          r -= 1
      else:
        res += 1
        vis.add(l)
        vis.add(r)
        break
print(res)

Solution (Python):
from collections import Counter

N = int(input())
A = sorted(list(map(int, input().split())), reverse=True)
C = Counter(A)
keys = set(A)

ans = 0
for a in A:
    target_2_pow = 2 ** a.bit_length()
    b = target_2_pow - a
    if b not in keys:
        continue

    if a != b:
        if C[a] == 0 or C[b] == 0:
            continue
    else:
        if C[b] < 2:
            continue
    ans += 1
    C[a] -= 1
    C[b] -= 1

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(pow(10, 6))
from collections import defaultdict


def main():
    _ = int(input())
    a = list(sorted(map(int, input().split())))
    dicta = defaultdict(lambda: 0)
    for _a in a:
        dicta[_a] += 1
    
    ans = 0
    for da in reversed(a):
        if dicta[da] <= 0:
            continue
        dicta[da] -= 1
        aa2 = 1
        while da >= aa2:
            aa2 *= 2
        
        if aa2-da in dicta and dicta[aa2-da] > 0:
            dicta[aa2-da] -= 1
            ans += 1
    
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import*
N,*A=map(int,open(0).read().split())
A.sort()
C=Counter(A)
s=0
for a in A[::-1]:
	if C[a]:
		C[a]-=1
		b=2**a.bit_length()-a
		if C[b]:
			C[b]-=1
			s += 1
print(s)

Solution (Python):
from collections import*
N,*A=map(int, open(0).read().split())
A.sort()
C=Counter(A)
s=0
for a in A[::-1]:
	if not C[a]:
		continue
	C[a]-=1
	b=2**a.bit_length()-a
	if C[b]:
		C[b]-=1
		s += 1
print(s)

Solution (Python):
from collections import Counter

N, *A = map(int, open(0).read().split())

C = Counter(A)
A.sort(reverse=True)

ans = 0
for a in A:
    if C[a] == 0:
        continue
    C[a] -= 1

    b = 2 ** a.bit_length() - a
    if C[b] > 0:
        C[b] -= 1
        ans += 1

print(ans)

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))

maxPow = (1 << (max(A) * 2).bit_length())
cntA = Counter(A)

ans = 0
while maxPow > 0:
    for a in A:
        if maxPow - a == a:
            if cntA[a] >= 2:
                ans += cntA[a] // 2
                cntA[a] %= 2
        elif cntA[a] > 0 and cntA[maxPow - a] > 0:
            ans += 1
            cntA[a] -= 1
            cntA[maxPow - a] -= 1
    maxPow //= 2

print(ans)

Solution (Python):
from collections import defaultdict
import math
N = int(input())
A = [int(x) for x in input().split()]
D = defaultdict(int)
for a in A:
  D[a] += 1
cnt = 0
for a in sorted(D.keys(), reverse=True):
  p = 2 ** math.ceil(math.log(a + 1, 2))
  d = p - a
  if a == d:
    cnt += D[a] // 2
    D[a] %= 2
  else:
    m = min(D[a], D[d])
    cnt += m
    D[a] -= m
    D[d] -= m
print(cnt)

Solution (Python):
import bisect


n = int(input())
a = list(map(int, input().split()))

group = {}
for i in range(n):
    ntz = a[i] & (-a[i])
    if ntz not in group:
        group[ntz] = {}
        group[ntz][a[i] // (ntz*2)] = 1
    elif a[i] // (ntz*2) not in group[ntz]:
        group[ntz][a[i] // (ntz*2)] = 1
    else:
        group[ntz][a[i] // (ntz*2)] += 1
      

ans = 0
for ntz in group:
    li = sorted(group[ntz].keys(), reverse = True)
    for num1 in li:
        if num1 == 0:
            tmp_ans = group[ntz][num1] // 2
            ans += tmp_ans
            group[ntz][num1] -= tmp_ans * 2
            continue
        tmp = len(bin(num1)) - 2
        num2 = num1 ^ (2**tmp - 1)
        if num2 in group[ntz]:
            tmp_ans = min(group[ntz][num1], group[ntz][num2])
            ans += tmp_ans
            group[ntz][num1] -= tmp_ans
            group[ntz][num2] -= tmp_ans
print(ans)

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007


def A():
    s = input()
    ans = 0
    w = 0
    for i in range(len(s)):
        if s[i] == "W":
            ans += i-w
            w += 1
    print(ans)
    return


def B():
    n = I()
    a = LI()
    f = defaultdict(lambda : 0)
    a.sort(key = lambda x:-x)
    ans = 0
    m = 32
    k = [1<<i for i in range(m)]
    for i in a:
        for j in k:
            i_ = j-i
            if i_ <= 0:continue
            if f[i_]:
                f[i_] -= 1
                ans += 1
                break
        else:
            f[i] += 1
    print(ans)
    return


def C():
    return


def D():
    return


def E():
    return


def F():
    return


if __name__ == "__main__":
    B()

Solution (Python):
import os
import sys
from collections import Counter

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))




A.sort(reverse=True)
counts = Counter(A)
ans = 0
for a in A:
    if counts[a] <= 0:
        continue
    counts[a] -= 1
    b = (1 << a.bit_length()) - a
    if counts[b] > 0:
        ans += 1
        counts[b] -= 1
print(ans)

Solution (Python):
n = int(input())
d = {}
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
result = 0
for a in list(sorted(d.keys(), reverse = True)):
  if a not in d:
    continue
  t = (1 << a.bit_length()) - a
  if t not in d:
    continue
  if a != t:
    if d[a] < d[t]:
      i = d[a]
    else:
      i = d[t]
  else:
    if d[a] == 1:
      continue
    i = d[a] // 2
  result += i  
  if d[a] == i:
    del d[a]
  else:
    d[a] -= i
  if d[t] == i:
    del d[t]
  else:
    d[t] -= i
print(result)

Solution (Python):
from bisect import bisect_right
n = int(input())
d = {}
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
alist = list(sorted(d.keys(), reverse = True))
result = 0
for a in alist:
  if a not in d:
    continue
  t = (1 << a.bit_length()) - a
  if t not in d:
    continue
  if a != t:
    if d[a] < d[t]:
      i = d[a]
    else:
      i = d[t]
  else:
    if d[a] == 1:
      continue
    i = d[a] // 2
  result += i  
  if d[a] == i:
    del d[a]
  else:
    d[a] -= i
  if d[t] == i:
    del d[t]
  else:
    d[t] -= i
print(result)

Solution (Python):
from bisect import bisect_right
n = int(input())
d = {}
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
alist = list(sorted(d.keys(), reverse = True))
amax = alist[0]
t = 1
power_of_two = []
while t < 2 * amax:
  t *= 2
  power_of_two.append(t)
result = 0
for a in alist:
  if a not in d:
    continue
  t = power_of_two[bisect_right(power_of_two, a)] - a
  if t not in d:
    continue
  if a != t:
    if d[a] < d[t]:
      i = d[a]
    else:
      i = d[t]
  else:
    i = d[a] // 2
  result += i  
  if d[a] == i:
    del d[a]
  else:
    d[a] -= i
  if d[t] == i:
    del d[t]
  else:
    d[t] -= i
print(result)

Solution (Python):
from bisect import bisect_right
n = int(input())
d = {}
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
alist = list(sorted(d.keys(), reverse = True))
amax = alist[0]
t = 1
power_of_two = []
while t < 2 * amax:
  t *= 2
  power_of_two.append(t)
result = 0
for a in alist:
  if a not in d:
    continue
  t = power_of_two[bisect_right(power_of_two, a)] - a
  if t not in d:
    continue
  if a != t:
    if d[a] < d[t]:
      i = d[a]
    else:
      i = d[t]
  else:
    if d[a] == 1:
      continue
    i = d[a] // 2
  result += i  
  if d[a] == i:
    del d[a]
  else:
    d[a] -= i
  if d[t] == i:
    del d[t]
  else:
    d[t] -= i
print(result)

Solution (Python):
from bisect import bisect_right
n = int(input())
d = {}
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
alist = list(sorted(d.keys(), reverse = True))
amax = alist[0]
t = 1
power_of_two = []
while t < 2 * amax:
  t *= 2
  power_of_two.append(t)
result = 0
for a in alist:
  if a not in d:
    continue
  t = power_of_two[bisect_right(power_of_two, a)] - a
  if t not in d:
    continue
  if a != t:
    i = min(d[a], d[t])
  else:
    if d[a] == 1:
      continue
    i = d[a] // 2
  result += i  
  if d[a] == i:
    del d[a]
  else:
    d[a] -= i
  if d[t] == i:
    del d[t]
  else:
    d[t] -= i
print(result)

Solution (Python):
from bisect import bisect_right
n = int(input())
d = {}
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
alist = list(sorted(d.keys(), reverse = True))
amax = alist[0]
t = 1
power_of_two = []
while t < 2 * amax:
  t *= 2
  power_of_two.append(t)
result = 0
for a in alist:
  if d[a] == 0:
    continue
  t = power_of_two[bisect_right(power_of_two, a)] - a
  if t not in d or d[t] == 0:
    continue
  if a != t:
    i = min(d[a], d[t])
  else:
    if d[a] == 1:
      continue
    i = d[a] // 2
  result += i
  d[a] -= i
  d[t] -= i
print(result)

Solution (Python):
from bisect import bisect_right
n = int(input())
d = {}
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
alist = list(sorted(d.keys(), reverse = True))
amax = alist[0]
t = 1
power_of_two = []
while t < 2 * amax:
  t *= 2
  power_of_two.append(t)
result = 0
for a in alist:
  if d[a] == 0:
    continue
  t = power_of_two[bisect_right(power_of_two, a)] - a
  while True:
    if t not in d or d[t] == 0:
      break
    if a != t:
      i = min(d[a], d[t])
    else:
      if d[a] == 1:
        break
      i = d[a] // 2
    result += i
    d[a] -= i
    d[t] -= i
    if d[a] == 0:
      break
print(result)

Solution (Python):
from bisect import bisect_right
n = int(input())
d = {}
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
alist = list(sorted(d.keys(), reverse = True))
amax = alist[0]
t = 1
power_of_two = []
while t < 2 * amax:
  t *= 2
  power_of_two.append(t)
result = 0
for a in alist:
  if d[a] == 0:
    continue
  t = power_of_two[bisect_right(power_of_two, a)] - a
  while True:
    if t not in d or d[t] == 0:
      break
    if a != t:
      i = min(d[a], d[t])
    else:
      i = d[a] // 2
    if i == 0:
      break
    result += i
    d[a] -= i
    d[t] -= i
    if d[a] == 0:
      break
print(result)

Solution (Python):
from bisect import bisect_right
n = int(input())
d = {}
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
alist = list(sorted(d.keys(), reverse = True))
amax = alist[0]
t = 1
power_of_two = []
while t < 2 * amax:
  t *= 2
  power_of_two.append(t)
result = 0
for a in alist:
  if a not in d or d[a] == 0:
    continue
  t = power_of_two[bisect_right(power_of_two, a)] - a
  while True:
    if t not in d or d[t] == 0:
      break
    if a != t:
      i = min(d[a], d[t])
    else:
      i = d[a] // 2
    if i == 0:
      break
    result += i
    d[a] -= i
    d[t] -= i
    if a not in d or d[a] == 0:
      break
print(result)

Solution (Python):
from bisect import bisect_right
n = int(input())
d = {}
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
alist = list(sorted(d.keys(), reverse = True))
amax = alist[0]
t = 1
power_of_two = []
while t < 2 * amax:
  t *= 2
  power_of_two.append(t)
result = 0
for a in alist:
  if a not in d or d[a] == 0:
    continue
  t = power_of_two[bisect_right(power_of_two, a)] - a
  while True:
    if t not in d or d[t] == 0:
      break
    if a == t and d[a] == 1:
      break
    i = 1
    if a != t:
      i = min(d[a], d[t])
    result += i
    d[a] -= i
    d[t] -= i
    if a not in d or d[a] == 0:
      break
print(result)

Solution (Python):
from bisect import bisect_right
n = int(input())
d = {}
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
alist = list(sorted(d.keys(), reverse = True))
amax = alist[0]
t = 1
power_of_two = []
while t < 2 * amax:
  t *= 2
  power_of_two.append(t)
result = 0
for a in alist:
  if a not in d or d[a] == 0:
    continue
  t = power_of_two[bisect_right(power_of_two, a)] - a
  while True:
    if t not in d or d[t] == 0:
      break
    if a == t and d[a] == 1:
      break
    result += 1
    d[a] -= 1
    d[t] -= 1
    if a not in d or d[a] == 0:
      break
print(result)

Solution (Python):
from bisect import bisect_right
n = int(input())
d = {}
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
alist = list(sorted(d.keys(), reverse = True))
amax = alist[0]
t = 1
power_of_two = []
while t < 2 * amax:
  t *= 2
  power_of_two.append(t)
result = 0
for a in alist:
  if a not in d:
    continue
  t = power_of_two[bisect_right(power_of_two, a)] - a
  while True:
    if t not in d:
      break
    if a == t and d[a] == 1:
      break
    result += 1
    if d[a] == 1:
      del d[a]
    else:
      d[a] -= 1
    if d[t] == 1:
      del d[t]
    else:
      d[t] -= 1
    if a not in d:
      break
print(result)

Solution (Python):
from bisect import bisect_right
n = int(input())
d = {}
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
amax = max(d.keys())
t = 1
power_of_two = []
while t < 2 * amax:
  t *= 2
  power_of_two.append(t)
result = 0
keys = list(sorted(d.keys(), reverse = True))
for a in keys:
  if a not in d:
    continue
  t = power_of_two[bisect_right(power_of_two, a)] - a
  while True:
    if t not in d:
      break
    if a == t and d[a] == 1:
      break
    result += 1
    if d[a] == 1:
      del d[a]
    else:
      d[a] -= 1
    if d[t] == 1:
      del d[t]
    else:
      d[t] -= 1
    if a not in d:
      break
print(result)

Solution (Python):
from bisect import bisect_right
n = int(input())
d = {}
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
amax = max(d.keys())
t = 1
valids = []
while t < 2 * amax:
  t *= 2
  valids.append(t)
result = 0
keys = list(sorted(d.keys(), reverse = True))
for a in keys:
  if a not in d:
    continue
  t = valids[bisect_right(valids, a)] - a
  while True:
    if t not in d:
      break
    if a == t and d[a] == 1:
      break
    result += 1
    if d[a] == 1:
      del d[a]
    else:
      d[a] -= 1
    if d[t] == 1:
      del d[t]
    else:
      d[t] -= 1
    if a not in d:
      break
print(result)

Solution (Python):
from bisect import bisect_right
n = int(input())
d = {}
amax = 0
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
amax = max(d.keys())
t = 1
valids = []
while t < 2 * amax:
  t *= 2
  valids.append(t)
result = 0
while True:
  prev = result
  keys = list(sorted(d.keys(), reverse = True))
  for x in keys:
    if x not in d:
      continue
    t = valids[bisect_right(valids, x)] - x
    while True:
      if t not in d:
        break
      if x == t and d[x] == 1:
        break
      result += 1
      if d[x] == 1:
        del d[x]
      else:
        d[x] -= 1
      if d[t] == 1:
        del d[t]
      else:
        d[t] -= 1
      if x not in d:
        break
  if prev == result:
    break
print(result)

Solution (Python):
from bisect import bisect_right
n = int(input())
d = {}
amax = 0
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
amax = max(d.keys())
t = 1
valids = []
while t < 2 * amax:
  t *= 2
  valids.append(t)
result = 0
while True:
  prev = result
  keys = list(d.keys())
  keys.sort(reverse = True)
  for x in keys:
    if x not in d:
      continue
    t = valids[bisect_right(valids, x)] - x
    while True:
      if t not in d:
        break
      if x == t and d[x] == 1:
        break
      result += 1
      if d[x] == 1:
        del d[x]
      else:
        d[x] -= 1
      if d[t] == 1:
        del d[t]
      else:
        d[t] -= 1
      if x not in d:
        break
  if prev == result:
    break
print(result)

Solution (Python):
from bisect import bisect_right
n = int(input())
d = {}
amax = 0
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
  amax = max(amax, a)
t = 1
valids = []
while t < 2 * amax:
  t *= 2
  valids.append(t)
result = 0
while True:
  prev = result
  keys = list(d.keys())
  keys.sort(reverse = True)
  for x in keys:
    if x not in d:
      continue
    t = valids[bisect_right(valids, x)] - x
    while True:
      if t not in d:
        break
      if x == t and d[x] == 1:
        break
      result += 1
      if d[x] == 1:
        del d[x]
      else:
        d[x] -= 1
      if d[t] == 1:
        del d[t]
      else:
        d[t] -= 1
      if x not in d:
        break
  if prev == result:
    break
print(result)

Solution (Python):
from bisect import bisect_right
n = int(input())
d = {}
for a in map(int, input().split()):
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
t = 1
valids = []
while t < 2 * 10 ** 9:
  t *= 2
  valids.append(t)
result = 0
while True:
  prev = result
  keys = list(d.keys())
  keys.sort(reverse = True)
  for x in keys:
    if x not in d:
      continue
    t = valids[bisect_right(valids, x)] - x
    while True:
      if t not in d:
        break
      if x == t and d[x] == 1:
        break
      result += 1
      if d[x] == 1:
        del d[x]
      else:
        d[x] -= 1
      if d[t] == 1:
        del d[t]
      else:
        d[t] -= 1
      if x not in d:
        break
  if prev == result:
    break
print(result)

Solution (Python):
import bisect
N = int(input())
*A, = map(int,input().split())
B = {}
for a in A:B[a] = B.get(a,0) + 1
B = sorted(map(list,B.items()))
bl = bisect.bisect_left

ans = 0
while B:
    b,n = B.pop()
    c = (1<<(len(bin(b))-2)) - b
    if c==b :
        ans += n//2
        continue
    i = bl(B,[c,0])
    if i>=len(B): continue
    if B[i][0] == c:
        m = min(n,B[i][1])
        ans += m
        B[i][1] -= m
print(ans)

Solution (Python):
from collections import Counter

n = int(input())
a = list(map(int, input().split()))

a.sort()
c = Counter(a)

ans = 0
for x in a[::-1]:
    if c[x]>0:
        c[x]-=1
        y = 2**x.bit_length()-x
        if c[y]>0:
            c[y]-=1
            ans += 1
print(ans)

Solution (Python):
import bisect

n = int(input())
aa = list(map(int,input().split()))

aa.sort()

a = [aa[0]]
b = [1]

for i in range(1,n):
    if a[-1]==aa[i]:
        b[-1]+=1
    else:
        a.append(aa[i])
        b.append(1)

ans = 0

for i in range(len(a)):
    while b[-i-1]!=0:
        c = 2**(len(bin(a[-i-1]))-2)-a[-i-1]
        d = bisect.bisect_left(a,c)
        if a[d]==c:
            if b[d]>1:
                ans += 1
                b[-i-1] -= 1
                b[d] -= 1
            elif b[d]==1 and a[d]!=a[-i-1]:
                ans += 1
                b[-i-1] -= 1
                b[d] -= 1
            else:
                break
        else:
            break

print(ans)

Solution (Python):
import bisect
import itertools
from collections import defaultdict
N = int(input())
a = list(map(int, input().split()))
a.sort(reverse = True)
d = defaultdict(int)

for key, value in itertools.groupby(a):
    d[key] = len(list(value))

ans = 0
for i in range(N):
    now = a[i]
    if d[now] > 0:
        pair = 2**((now).bit_length()) - now
        if now == pair:
            if d[pair] >= 2:
                ans += 1
                d[pair] -= 2
        elif d[pair] > 0:
            ans += 1
            d[now] -= 1
            d[pair] -= 1
print(ans)

Solution (Python):
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
A.sort()
d = defaultdict(int)
for a in A:
    d[a] += 1

cnt = 0
for a in reversed(A):
    if d[a] > 0:
        d[a] -= 1
        pair = (1 << a.bit_length()) - a
        if d[pair] > 0:
            cnt += 1
            d[pair] -= 1
print(cnt)

Solution (Python):
import sys
input = sys.stdin.readline

from collections import Counter



N = int(input())
A = [int(x) for x in input().split()]

counter = Counter(A)
A.sort()

answer = 0
for x in A[::-1]:
    while counter[x] > 0:
        counter[x] -= 1
        target = (1 << x.bit_length()) - x
        if counter[target] > 0:
            counter[target] -= 1
            answer += 1

print(answer)

Solution (Python):
from collections import defaultdict

N = int(input())
A = sorted(list(map(int, input().split())), reverse=True)
dic = defaultdict(int)
for a in A:
    dic[a] += 1

ans = 0
for a in A:
    if dic[a] != 0:
        b = 2**(a.bit_length()) - a
        if dic[b] != 0:
            if b == a and dic[b] == 1:
                continue
            ans += 1
            dic[a] -= 1
            dic[b] -= 1

print(ans)

Solution (Python):
from bisect import bisect_left

n=int(input())
arr=list(map(int,input().split()))
arr=sorted(arr,reverse=True)
dic={}
for val in arr:
  if val not in dic:
    dic[val]=1
  else:
    dic[val]+=1
arr2=[]
for i in range(40):
  arr2.append(2**i)
ans=0
for val in arr:
  pos=bisect_left(arr2,val)
  if val==arr2[pos]:
    persuit=arr2[pos+1]
  else:
    persuit=arr2[pos]
  if persuit-val==val:
    if val in dic:
      if dic[val]>=2:
        ans+=1
        dic[val]-=2
        if dic[val]==0:
          del dic[val]
      else:
        continue
    else:
      continue
  else:
    if val in dic and persuit-val in dic:
      ans+=1
      dic[val]-=1
      if dic[val]==0:
        del dic[val]
      dic[persuit-val]-=1
      if dic[persuit-val]==0:
        del dic[persuit-val]
    else:
      continue
print(ans)

Solution (Python):
from bisect import bisect_left

n=int(input())
arr=list(map(int,input().split()))
arr=sorted(arr,reverse=True)
dic={}
for val in arr:
  if val not in dic:
    dic[val]=1
  else:
    dic[val]+=1
arr2=[]
for i in range(40):
  arr2.append(2**i)
ans=0
for val in arr:
  pos=bisect_left(arr2,val)
  if val==arr2[pos]:
    persuit=arr2[pos+1]
  else:
    persuit=arr2[pos]
  if persuit-val==val:
    if val in dic:
      if dic[val]>=2:
        ans+=1
        dic[val]-=2
        if dic[val]==0:
          del dic[val]
      else:
        continue
    else:
      continue
  else:
    if val in dic and persuit-val in dic:
      ans+=1
      dic[val]-=1
      if dic[val]==0:
        del dic[val]
      dic[persuit-val]-=1
      if dic[persuit-val]==0:
        del dic[persuit-val]
    else:
      continue
print(ans)

Solution (Python):
n=int(input())
arr=list(map(int,input().split()))
arr=sorted(arr,reverse=True)
dic={}
for val in arr:
  if val not in dic:
    dic[val]=1
  else:
    dic[val]+=1
ans=0
for val in arr:
  for i in range(40):
    if val<2**i:
      persuit=2**i
      break
  if persuit-val==val:
    if val in dic:
      if dic[val]>=2:
        ans+=1
        dic[val]-=2
        if dic[val]==0:
          del dic[val]
      else:
        continue
    else:
      continue
  else:
    if val in dic and persuit-val in dic:
      ans+=1
      dic[val]-=1
      if dic[val]==0:
        del dic[val]
      dic[persuit-val]-=1
      if dic[persuit-val]==0:
        del dic[persuit-val]
    else:
      continue
print(ans)

Solution (Python):




def main():
    N = int(input())
    A = list(map(int, input().split()))

    M = int(10**9)
    bitOfM = M.bit_length()

    X = {}
    Y = [set() for _ in range(bitOfM+1)]

    for a in A:
        if not a in X:
            X[a] = 1
            Y[a.bit_length()].add(a)
        else:
            X[a] += 1

    c = 0

    for i in range(bitOfM, 0, -1):
        P = 1<<(i-1)
        
        if P in X:
            c += X[P]//2

        for a in Y[i]:
            if a == P:
                continue

            a2 = 2*P - a
            if a2 in X:
                c2 = min(X[a], X[a2])
                c += c2
                X[a2] -= c2


    print(c)



if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict
from math import log
N=int(input())
A=list(map(int,input().split()))
dict=defaultdict(int)
for a in A:
    dict[a]+=1
A.sort(reverse=True)
ans=0
for a in A:
    if dict[a]==0:
        continue
    c=int(log(a,2))
    b=~a & ((1<<(c+1)) -1)
    b+=1
    dict[a]-=1
    if dict[b]:
        ans+=1
        dict[b]-=1
    else:
        dict[a]+=1
print(ans)

Solution (Python):
from collections import defaultdict
from math import log
N=int(input())
A=list(map(int,input().split()))
dict=defaultdict(int)
for a in A:
    dict[a]+=1
A.sort(reverse=True)
ans=0
for a in A:
    if dict[a]==0:
        continue
    c=int(log(a,2))
    b=~a & ((1<<(c+1)) -1)
    b+=1
    dict[a]-=1
    if dict[b]:
        ans+=1
        dict[b]-=1
    else:
        dict[a]+=1
print(ans)

Solution (Python):
from collections import defaultdict
from math import log
N=int(input())
A=list(map(int,input().split()))
dict=defaultdict(int)
for a in A:
    dict[a]+=1
A.sort(reverse=True)
ans=0
for a in A:
    if dict[a]==0:
        continue
    c=int(log(a,2))
    b=~a & ((1<<(c+1)) -1)
    b+=1
    dict[a]-=1
    if dict[b]:
        ans+=1
        dict[b]-=1
    else:
        dict[a]+=1
print(ans)

Solution (Python):
import bisect
import math
import sys
input = sys.stdin.readline


N = int(input())
A = list(map(int, input().split()))
A.sort()
A_set = []
num = []
a_cur = float('inf')
for a in A:
    if a != a_cur:
        A_set.append(a)
        num.append(1)
        a_cur = a
    else:
        num[-1] += 1

ans = 0
for i in range(len(A_set) - 1, -1, -1):
    if num[i] > 0:
        A_i = A_set[i]
        comp = 2 ** math.ceil(math.log(A_i + 1, 2)) - A_i
        j = bisect.bisect_left(A_set, comp, 0, i)
        while True:
            if A_set[j] == comp:
                if num[j] > 0:
                    if i != j:
                        ans += min(num[j], num[i])
                        num[j] = max(num[j] - num[i], 0)
                        break
                    elif num[j] >= 2:
                        add = num[j] // 2
                        ans += add
                        num[j] = num[j] % 2
                        break
                    else:
                        break
                else:
                    break
            else:
                break

print(ans)

Solution (Python):
from collections import defaultdict
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
pow2 = [1]
for i in range(31):
    pow2.append(pow2[-1] * 2)
a = sorted(a, reverse=True)
d = defaultdict(int)
for i in a:
    d[i] += 1


num = 0
for i in range(N):
    if d[a[i]] == 0:
        continue
    for k in range(30, -1, -1):
        inv = pow2[k] - a[i]
        if inv < 0 or inv > a[i]:
            continue
        if inv != a[i]:
            if d[inv] > 0 and d[a[i]] > 0:
                p = min(d[inv], d[a[i]])
                num += p
                d[inv] -= p
                d[a[i]] -= p
                break
        else:
            if d[a[i]] >= 2:
                num += d[a[i]] // 2
                d[a[i]] %= 2
                break
print(num)

Solution (Python):
import bisect
import collections
def f(n):
  return n.bit_length()
n=int(input())
A=[int(i) for i in input().split()]
D=dict(collections.Counter(A))
C=sorted(list(D.keys()))
ans=0
for i in range(len(C)):
  pair=2**f(C[-(i+1)])-C[-(i+1)]
  k=bisect.bisect_left(C,pair)
  if C[k]==pair:
    if k==len(C)-(i+1):
      ans+=D[pair]//2
      D[pair]%=2
    else:
      aa=min(D[pair],D[C[-(i+1)]])
      ans+=aa
      D[pair]-=aa
      D[C[-(i+1)]]-=aa
print(ans)