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



Sample input:
3
1 2 3


Sample output:
1

Submission status: Time Limit Exceeded

Solution code:
import math# 3 11 14 5 13
n=int(input())
a=input()
#n=10**5
#s=list(range(1,10**5))
s=a.split(" ")
s=[int(x) for x in s]

#print(s)
count=0
for i in range(n):
    if len(s)<2:
        break
    Max=max(s)
    index=s.index(Max)
    x=s.pop(index)
    y=2**(int(math.log(x,2))+1)-x
    if y in s:
        count+=1
        s.pop(s.index(y))
    #if count%100==0:
        #print("count"+str(count))
        #print(s)
print(count)

Submission status: Time Limit Exceeded

Solution code:
import math
n=int(input())
a=input()
s=a.split(" ")
s=[int(x) for x in s]
s.sort(reverse=True)
count=0
for i in range(n-1):
    if len(s)<2:
        break
    x=s.pop(0)
    for j in range(len(s)):
        if math.log(x+s[j],2).is_integer():
            count+=1
            s.pop(j)
            break
print(count)

Submission status: Time Limit Exceeded

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

a.sort(reverse=True)

ans = 0
while len(a) > 1:
    x = a[0]
    del a[0]
    temp = (1 << x.bit_length()) - x
    if temp in a:
        ans += 1
        a.remove(temp)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

a = sorted(a)

temp = 2
while temp <= a[len(a) - 1]:
    temp *= 2

ans = 0
while len(a) > 1:
    m = a[len(a) - 1]
    del a[len(a) - 1]
    while 1:
        if temp > m * 2:
            temp //= 2
        else:
            break
    if temp < m:
        temp *= 2
        continue
    if (temp - m) in a:
        ans += 1
        a.remove(temp - m)

print(ans)

Submission status: Time Limit Exceeded

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

a = sorted(a)
ans = 0
while len(a) > 1:
    m = a[len(a) - 1]
    del a[len(a) - 1]
    temp = 2
    for i in range(n):
        if temp > m:
            break
        else:
            temp *= 2
    if temp > m * 2:
        continue
    if (temp - m) in a:
        ans += 1
        a.remove(temp - m)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def hersCode(L, n):
  ok = len(L) - 1
  ng = 0
  while abs(ok - ng) > 1:
    mid = abs(ok + ng) // 2
    if L[mid] > n:
      ok = mid
    else:
      ng = mid
  return ok

def hersCode2(L, n):
  ok = 0
  ng = len(L)
  while abs(ok - ng) > 1:
    mid = abs(ok + ng) // 2
    if L[mid] <= n:
      ok = mid
    else:
      ng = mid
  return ok

N = int(input())

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

D = [pow(2, i) for i in range(31)]
L = [True for i in range(N + 1)]
Ans = 0
A = [-1e11] + A
for i in range(len(A) - 1, 1, -1):
  if L[i]:
    k = D[hersCode(D, A[-1])] - A[-1]
    del A[-1]
    t = hersCode2(A, k)
    cnt = 0
    while A[t - cnt] == k:
      if L[t - cnt]:
        L[t - cnt] = False
        Ans += 1
        break
      else:
        cnt += 1
  else:
    del A[-1]

print(Ans)

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
ar = list(map(int,input().split(" ")))
ar = sorted(ar,reverse=True)
br = []
i = 1
while True:
    if 2 ** i > ar[0]+ar[1]:
        break
    else:
        br.append(2 ** i)
        i += 1
count = 0
while len(ar) > 1:
    for i in range(1,len(ar)):
        if ar[0] + ar[i] in br:
            del ar[i]
            count += 1
            sw = "F"
            break
    del ar[0]
print(count)

Submission status: Time Limit Exceeded

Solution code:
import math
N = int(input())
l = list(map(int, input().split()))
ld = sorted(l, reverse=True)

k = 0
two = []
while 2**k < ld[0] + ld[1]:
    k+=1
    if 2**k >= ld[-1] + ld[-2]:
        two.append(2**k)
p = 0

while len(ld) > 0:
    for m in range(1,len(ld)):
        if (ld[0] + ld[m]) in two:
            del ld[m]
            p += 1
            break
    if len(ld) > 0:
        del ld[0]
print(p)

Submission status: Time Limit Exceeded

Solution code:
import math
N = int(input())
l = list(map(int, input().split()))
ld = sorted(l, reverse=True)

k = 0
two = []
while 2**k < ld[0] + ld[1]:
    k+=1
    two.append(2**k)
p = 0

while len(ld) > 0:
    for m in range(1,len(ld)):
        if (ld[0] + ld[m]) in two:
            del ld[m]
            p += 1
            break
    if len(ld) > 0:
        del ld[0]
print(p)

Submission status: Time Limit Exceeded

Solution code:
import sys

nbs = list(sorted(map(int, list(sys.stdin)[1].strip().split())))
used = set()
count = 0

for k in reversed(range(1, 31)):
    p = 1 << k
    j = len(nbs) - 1
    for i, a in enumerate(nbs):
        if i not in used:
            while j > i and (nbs[j] > p - a or j in used):
                j -= 1
            if j <= i:
                break
            if nbs[j] == p - a:
                used.add(i)
                used.add(j)
                count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
#AGC030 B - Powers of two
beki=[]
for i in range(1,32):
    beki.append(2**i)#bekiは２のべき乗の配列
N=int(input())
A=input().split()
for i in range(N):
    A[i]=int(A[i])
for i in (reversed(beki)):
    inverseA=[]
    for j in range(len(A)):
        inverseA.append(i-A[j])#inverseA
    for j in range(len(A)):
        if inverseA[j] in A:
            A.remove(inverseA[j])
print((N-len(A))//2)

Submission status: Time Limit Exceeded

Solution code:
#AGC030 B - Powers of two
beki=[]
for i in range(1,32):
    beki.append(2**i)#bekiは２のべき乗の配列
N=int(input())
A=input().split()
for i in range(N):
    A[i]=int(A[i])
sorted(A)#Aを昇順にソート
for i in (reversed(beki)):
    inverseA=[]
    for j in range(len(A)):
        inverseA.append(i-A[j])#inverseA
    for j in range(len(A)):
        if inverseA[j] in A:
            A.remove(inverseA[j])
print((N-len(A))//2)

Submission status: Time Limit Exceeded

Solution code:
#AGC030 B - Powers of two
beki=[]
for i in range(1,32):
    beki.append(2**i)#bekiは２のべき乗の配列
def tasitekesu(data,n):#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])
for i in (reversed(beki)):
    R=tasitekesu(A,i)
    A=R[0]
    ans=ans+R[1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#AGC030 B - Powers of two
beki=[]
for i in range(1,32):
    beki.append(2**i)#bekiは２のべき乗の配列
N=int(input())
A=input().split()
ans=0
for i in range(N):
    A[i]=int(A[i])
for j in (reversed(beki)):
    for k in range(N):
        for l in range(k):
            if A[k]*A[l]!=0 and A[k]+A[l]==j:
                A[k]=0
                A[l]=0
                ans=ans+1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def solve():
    N = int(input())
    data = sorted(list(map(int, input().split())), reverse=True)
    rest = data
    ind = 0
    ind2 = 1
    ans = 0
    i=0

    while True:
        flag = False
        for j in range(i+1, len(rest)):
            tmp = rest[i] + rest[j]
            if not(tmp & tmp - 1):
                ans += 1
                rest = [item for ind, item in enumerate(rest) if not ind in [i, j]]
                flag = True
                break
        if flag:
            i = 0
            continue

        i+=1
        if i==j:
            break
    print(ans)

solve()

Submission status: Time Limit Exceeded

Solution code:
import math
a=int(input())
o = []
e = []
x = list(map(int, input().split()))
for i in x:
    if i%2 ==0:e.append(i)
    else:o.append(i)
e.sort()
o.sort()
e = e[::-1]
o = o[::-1]
def cal(k):
    p = 0
    r=0
    l = len(k)
    if l <2:return 0
    else:
        i = 0
        while p!=l:
            for j in range(i+1,len(k)):
                lo = math.log2(k[i] + k[j])
                #print(lo,int(lo))
                #print(k[j],k[i])
                if lo == int(lo):
                    r+=1
                    #print(k[j],k[i])
                    k.remove(k[j])
                    k.remove(k[i])
                    p+=1
                    i -=1
                    break
            i+=1
            p+=1
    return r
print(cal(o)+cal(e))

Submission status: Time Limit Exceeded

Solution code:
import math
a=int(input())
o = []
e = []
x = list(map(int, input().split()))
for i in x:
    if i%2 ==0:e.append(i)
    else:o.append(i)
e.sort()
o.sort()
e = e[::-1]
o = o[::-1]
def cal(k):
    r=0
    l = len(k)
    if l <2:return 0
    else:
        i = 0
        while i != len(k):
            for j in range(i+1,len(k)):
                lo = math.log2(k[i] + k[j])
                #print(lo,int(lo))
                #print(k[j],k[i])
                if lo == int(lo):
                    r+=1
                    #print(k[j],k[i])
                    k.remove(k[j])
                    k.remove(k[i])
                    i -=1
                    break
            i+=1
    return r
print(cal(o)+cal(e))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A = sorted(A)
count = 0
i = 0
reg = 0
while True:
    if len(A) == 0:
        break
    now = A.pop()
    if now == -1:
        continue
    reg = 1 << (len(str(bin(now))) - 2)
    try:
        A[A.index(reg - now)] = -1
        count += 1
        i += 1
        continue
    except:
        continue
    i += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A = sorted(A)
count = 0
i = 0
reg = 1
while True:
    if i >= N - 1:
        break
    now = A.pop()
    if now == -1:
        continue
    reg = 1
    for j in reversed(range(0, 32)):
        reg *= 2
        if reg <= now:
            continue
        else:
            try:
                A[A.index(reg - now)] = -1
                count += 1
                i += 1
                break
            except:
                break
    i += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A = sorted(A)
count = 0
i = 0
reg = 1
while True:
    if i >= N - 1:
        break
    now = A.pop()
    if now == -1:
        continue
    reg = 1
    for j in range(32):
        reg *= 2
        if reg <= now:
            continue
        else:
            try:
                A[A.index(reg - now)] = -1
                count += 1
                i += 1
                break
            except:
                break
    i += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A = sorted(A)
count = 0
i = 0
reg = 1
while True:
    if i >= N - 1:
        break
    now = A.pop()
    if now == -1:
        continue
    reg = 1
    for j in range(32):
        reg *= 2
        if reg <= now:
            continue
        elif A.count(reg - now) == 0:
            break
        else:
            A[A.index(reg - now)] = -1
            count += 1
            i += 1
    i += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A = sorted(A)
count = 0
i = 0
reg = 1
while True:
    if i >= N - 1:
        break
    now = A.pop()
    reg = 1
    for j in range(32):
        reg *= 2
        if reg <= now:
            continue
        elif A.count(reg - now) == 0:
            break
        else:
            del(A[A.index(reg - now)])
            count += 1
            i += 1
    i += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A = sorted(A)
count = 0
i = 0
while True:
    if i >= N - 1:
        break
    now = A.pop()
    for j in range(N - i - 1):
        if (2 ** 31) % (A[j] + now) == 0:
            count += 1
            i += 1
            del A[j]
            break
    i += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
ball=list(map(int,input().split()))
ball.sort(reverse=True)
ans=0
while ball:
  for j in range(1,len(ball)):
    pkouho=ball[0]+ball[j]
    if pkouho&(pkouho-1)==0:
      del ball[j]
      del ball[0]
      ans+=1
      break
  else:  
    del ball[0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
cnt = 0
l = []
flag = False
for i in range(N):
  #print("check:", A[i])
  for j in l:
    if not (A[i]+j) & (A[i]+j-1):
      #print(A[i], j)
      l.remove(j)
      cnt += 1
      flag = True
      break
  if flag == False:
    l.append(A[i])
  else:
    flag = False

print(cnt)
#print(l)

Submission status: Time Limit Exceeded

Solution code:
N=input()
A=map(int,raw_input().split())
import math
Result=[]

A.sort(reverse=True)
A_2=[int(math.log(a,2)) for a in A]


for i in range(N):
    if i in Result:
        continue
    A_3=[A[k]+A[i]-2**(A_2[i]+1) for k in range(i+1,N) if k not in Result]
    K=[k for k in range(i+1,N) if k not in Result]
    if A_3.count(0)>0:
        Result.extend([i,K[A_3.index(0)]])

print len(Result)/2

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np

d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()

N = d_in()
A_list = np.array(ds_in())
A_list.sort()
EPSILON = 10**(-6)

cand_A = 2 ** (np.log2(A_list + EPSILON).astype(int) + 1) - A_list
idx = np.searchsorted(A_list, cand_A)
flag = np.zeros(N, dtype=int)

ans = 0
for i in range(N-1, -1, -1):
    if flag[i]:
        continue
    if A_list[idx[i]] == cand_A[i]:
        cand_idx = idx[i]
        while (cand_idx < i) and (A_list[cand_idx] == cand_A[i]):
            if not flag[cand_idx]:
                ans += 1
                flag[cand_idx] = 1
                break
            cand_idx += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
from bisect import bisect_left

d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()

N = d_in()
A_list = ds_in()
A_list.sort()

ans = 0
while N > 1:
    max_a = A_list.pop()
    N -= 1
    order = len(bin(max_a)) - 3
    if max_a == (1 << order) == A_list[-1]:
        del A_list[-1]
        ans += 1
        N -= 1
    else:
        cand = (1 << (order+1)) - max_a
        idx = bisect_left(A_list, cand)
        if (idx < N) and (cand == A_list[idx]):
            del A_list[idx]
            ans += 1
            N -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
from bisect import bisect_left

d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()

N = d_in()
A_list = ds_in()
A_list.sort()

ans = 0
while len(A_list) > 1:
    max_a = A_list.pop()
    order = len(bin(max_a)) - 3
    if max_a == (1 << order) == A_list[-1]:
        del A_list[-1]
        ans += 1
    else:
        cand = (1 << (order+1)) - max_a
        idx = bisect_left(A_list, cand)
        if (idx < len(A_list)) and (cand == A_list[idx]):
            del A_list[idx]
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
from bisect import bisect_left

d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()

N = d_in()
A_list = ds_in()
A_list.sort()

ans = 0
while len(A_list) > 1:
    max_a = A_list.pop()
    binary = bin(max_a)
    cand = (1 << len(binary[2:])) - max_a
    if cand == 0:
        if max_a == A_list[-1]:
            del A_list[-1]
            ans += 1
    else:
        idx = bisect_left(A_list, cand)
        if (idx < len(A_list)) and (cand == A_list[idx]):
            del A_list[idx]
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
from bisect import bisect_left

d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()

N = d_in()
A_list = ds_in()
A_list.sort()

ans = 0
while len(A_list) > 1:
    max_a = A_list.pop()
    binary = bin(max_a)
    cand = 2 ** len(binary[2:]) - max_a
    if cand == 0:
        if max_a == A_list[-1]:
            A_list.pop()
            ans += 1
    else:
        idx = bisect_left(A_list, cand)
        if idx < len(A_list) and cand == A_list[idx]:
            A_list.pop(idx)
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
from bisect import bisect_left

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()

N = d_in()
A_list = ds_in()

A_list.sort()

ans = 0
while len(A_list) > 1:
    max_a = A_list.pop()
    if bin(max_a)[3:].find('1') == -1:
        if max_a == A_list[-1]:
            A_list.pop()
            ans += 1
    else:
        cand = 2 ** len(bin(max_a)[2:]) - max_a
        idx = bisect_left(A_list, cand)
        if idx < len(A_list) and cand == A_list[idx]:
            A_list.pop(idx)
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

cc = 0
while A:
    a = A.pop()
    i = len(bin(a)) - 2
    se = 2**i - a
    res = bisect.bisect_left(A, se)
    try:
        if A[res] == se:
            del A[res]
            cc += 1
    except:
        pass

print(cc)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

cc = 0
while A:
    a = A.pop()
    i = len(bin(a)) - 2
    se = 2**i - a
    res = bisect.bisect_left(A, se)
    try:
        if A[res] == se:
            A.pop(res)
            cc += 1
    except:
        pass

print(cc)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

cc = 0
while True:
    if A:
        a = A.pop()
    else:
        break
    if not A:
        break
    i = len(bin(a)) - 2
    se = 2**i - a
    res = bisect.bisect_left(A, se)
    try:
        if A[res] == se:
            A.pop(res)
            cc += 1
    except:
        pass

print(cc)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

cc = 0
while True:
    if A:
        a = A.pop()
    else:
        break
    if not A:
        break
    for i in range(30):
        if a < 2 ** i:
            break
    se = 2**i - a
    res = bisect.bisect_left(A, se)
    try:
        if A[res] == se:
            A.remove(se)
            cc += 1
    except:
        pass

print(cc)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

cc = 0
while True:
    if len(A) == 0 or len(A) == 1:
        break
    a = A.pop()
    for i in range(30):
        if a < 2 ** i:
            break
    se = 2**i - a
    res = bisect.bisect_left(A, se)
    if res != len(A):
        if A[res] == se:
            A.remove(se)
            cc += 1

print(cc)

Submission status: Time Limit Exceeded

Solution code:
N = int(input().rstrip())
A = [int(s) for s in input().rstrip().split()]
A.sort()

squared = [2]
tmp = 2
while tmp < 2 * 10**9:
    tmp *= 2
    squared.append(tmp)

cnt = 0
while A:
    max_num = A.pop()
    for i in range(len(A)):
        if max_num + A[i] in squared:
            cnt += 1
            A.pop(i)
            break

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = list(map(int,input().split()))

used = collections.Counter(a)
a.sort()
a.reverse()

two = [2]
i = 0
while two[i]*2 <= 2*10**9:
    two.append(two[i]*2)
    i += 1
two.reverse()

counter = 0
for i in range(n):
    if used[a[i]] > 0:
        while two[-1] > 2*a[i]:
            two.pop(-1)
        for k in range(len(two)):
            if used[two[k]-a[i]] >= 1:
                if two[k]-a[i] != a[i]:
                    counter += 1
                    used[two[k]-a[i]] -= 1
                    used[a[i]] -= 1
                else:
                    if used[a[i]] >= 2:
                        counter += 1
                        used[two[k]-a[i]] -= 1
                        used[a[i]] -= 1
print(counter)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split(" ")))
A.sort()
used = [0] * N
pow2_set = reversed([ 1<<i for i in range(36)])
ans = 0
for pow2 in pow2_set:
    l,r = 0,N-1
    while l < r:
        if A[l] + A[r] < pow2:
            l += 1
        elif A[l] + A[r] == pow2:
            if used[l] == 0 and used[r] == 0:
                ans += 1
                used[l],used[r] = 1,1
            else:
                if used[l] == 1:
                    l += 1
                if used[r] == 1:
                    r -= 1
        else:
            r -= 1
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
AN = list((map(int,input().split(" "))))
 
AN.sort(reverse=True)
 
num = 0
count = 0
 
def bbb(ccc):
    ddd = ccc
    while True:
        if ddd == 2:
            return True
        ddd = ddd / 2
        if ddd % 2 != 0:
            return False
 
while True:
 
    if len(AN) == 0:
        break
 
    A = AN.pop(num)
 
    for i in range(len(AN)):
        n = A + AN[i]
        if (n & (n - 1) == 0):
            AN.pop(i)
            count += 1
            break
 
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split(" ")))
A.sort()
used = [0] * N
pow2_set = reversed([ 1<<i for i in range(36)])
ans = 0

for pow2 in pow2_set:
    l,r = 0,N-1
    while l < r:
        if A[l] + A[r] < pow2:
            l += 1
        elif A[l] + A[r] == pow2:
            if used[l] == 0 and used[r] == 0:
                ans += 1
                used[l],used[r] = 1,1
            else:
                if used[l] == 1:
                    l += 1
                if used[r] == 1:
                    r -= 1
        else:
            r -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
def getN():
    return int(input())

def getMN():
    a = input().split()
    b = [int(i) for i in a]
    return b[0],b[1]

def getlist():
    a = input().split()
    b = [int(i) for i in a]
    return b

def check_t(num):
    i = 1
    while(True):
        if num < 2**i:
            return 2**i
        else:
            i += 1


from collections import defaultdict
n = getN()
nums = getlist()
numhash = defaultdict(int)
for i in nums:
    numhash[i] += 1
ans = 0
for num in sorted(numhash.keys(), key=lambda x: -x):
    while(numhash[num] != 0):
        target  = (check_t(num)) - num
        if numhash[target]:
            if target == num:
                if numhash[target] >= 2:
                    ans += 1
                    numhash[target] -= 2
                else:
                    numhash[num] = 0
            else:
                if numhash[target] >= 1:
                    ans += 1
                    numhash[target] -= 1
                    numhash[num] -= 1
                else:
                    numhash[num] = 0
        else:
            numhash[num] = 0


print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
AN = list((map(int,input().split(" "))))

AN.sort(reverse=True)

num = 0
count = 0
ccc = 0

while True:

    if len(AN) == 0:
        break

    A = AN.pop(num)
    ccc += 1

    for i in range(len(AN)):
        n = A + AN[i]
        if (n & (n - 1) == 0):
            AN.pop(i)
            count += 1
            break
    
    if ccc > 200001:
        break

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
AN = list((map(int,input().split(" "))))

AN.sort(reverse=True)

num = 0
count = 0

def bbb(ccc):
    ddd = ccc
    while True:
        if ddd == 2:
            return True
        ddd = ddd / 2
        if ddd % 2 != 0:
            return False

while True:

    if len(AN) == 0:
        break

    A = AN.pop(num)

    for i in range(len(AN)):
        n = A + AN[i]
        if (n & (n - 1) == 0):
            AN.pop(i)
            count += 1
            break

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
AN = list((map(int,input().split(" "))))

AN.sort(reverse=True)

num = 0
count = 0

def bbb(ccc):
    ddd = ccc
    while True:
        if ddd == 2:
            return True
        ddd = ddd / 2
        if ddd % 2 != 0:
            return False

while True:

    if len(AN) == 0:
        break

    A = AN.pop(num)

    for i in range(len(AN)):
        if bbb(A + AN[i]):
            AN.pop(i)
            count += 1
            break

print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
buf=[]
buf=input().split()
A=[]
for i in range(N):
    A.append(int(buf[i]))
A.sort()
c=0
g=0
index=[i for i in reversed(range(0,32))]
loop=[i for i in range(N-1)]
for i in index:
    sum=1<<i
    left=0
    right=N-1-g
    for j in loop:
        if right>0 and left<right :
            if (A[left]+A[right])==sum:
                A.remove(A[left])
                A.remove(A[right-1])
                right -= 2
                c=c+1
                g+=2
            elif(A[left]+A[right])>sum:
                right=right-1
            else:
                left=left+1
print(c)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
buf=[]
buf=input().split()
A=[]
flug=[0 for i in range(N)]
for i in range(N):
    A.append(int(buf[i]))
A.sort()
c=0
index=[i for i in reversed(range(0,32))]
loop=[i for i in range(N-1)]
for i in index:
    sum=1<<i
    left=0
    right=N-1
    for j in loop:
        if left<right:
            if flug[left]==1:
                left=left+1
            elif flug[right]==1:
                right=right-1
            elif (A[left]+A[right])==sum:
                flug[left]=1
                flug[right]=1
                left=left+1
                right=right-1
                c=c+1
            elif(A[left]+A[right])>sum:
                right=right-1
            else:
                left=left+1
print(c)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
buf=[]
buf=input().split()
A=[]
flug=[]
for i in range(N):
    A.append(int(buf[i]))
    flug.append(0)
A.sort()
c=0

for i in reversed(range(0,32)):
    sum=1<<i
    left=0
    right=N-1
    while left<right:
        if flug[left]==1:
            left=left+1
        elif flug[right]==1:
            right=right-1
        elif (A[left]+A[right])==sum:
            flug[left]=1
            flug[right]=1
            left=left+1
            right=right-1
            c=c+1
        elif(A[left]+A[right])>sum:
            right=right-1
        else:
            left=left+1
print(c)

Submission status: Time Limit Exceeded

Solution code:

def judge(x):
    while x>1:
        if x%2 != 0:
            return 0
        x/=2
    return 1

N=int(input())
buf=[]
buf=input().split()
A=[]
flug=[]
for i in range(N):
    A.append(int(buf[i]))
    flug.append(0)
A.sort()
c=0

for i in reversed(range(0,32)):
    sum=1<<i
    left=0
    right=N-1
    while left<right:
        if flug[left]==1:
            left=left+1
        elif flug[right]==1:
            right=right-1
        elif (A[left]+A[right])==sum:
            flug[left]=1
            flug[right]=1
            left=left+1
            right=right-1
            c=c+1
        elif(A[left]+A[right])>sum:
            right=right-1
        else:
            left=left+1
print(c)

Submission status: Time Limit Exceeded

Solution code:

def judge(x):
    while x>1:
        if x%2 != 0:
            return 0
        x/=2
    return 1

N=int(input())
buf=[]
buf=input().split()
A=[]
flug=[]
for i in range(N):
    A.append(int(buf[i]))
    flug.append(0)
A.sort()
c=0

for i in reversed(range(0,32)):
    sum=1<<i
    left=0
    right=N-1
    while left<right:
        if flug[left]==1:
            left=left+1
        elif flug[right]==1:
            right=right-1
        elif flug[left]!=1 and flug[right]!=1 and (A[left]+A[right])==sum:
            flug[left]=1
            flug[right]=1
            left=left+1
            right=right-1
            c=c+1
        elif(A[left]+A[right])>sum:
            right=right-1
        elif (A[left]+A[right])<sum:
            left=left+1
print(c)

Submission status: Time Limit Exceeded

Solution code:
import math
N = int(input())
A = sorted(int(i) for i in input().split())

k = 2**int(math.log(A[-1], 2) + 1)
c = 0
while A:
    v = A.pop()
    while k > 2*v:
        k //= 2
    w = k - v
    if w in A:
        c += 1
        A.remove(w)
print(c)

Submission status: Time Limit Exceeded

Solution code:
def judge(x):
    while(x>1):
        if x%2 != 0:
            return 0
        x/=2
    return 1

N=int(input())
buf=[]
buf=input().split()
A=[]
for i in range(N):
    A.append(int(buf[i]))
A.sort()
A.reverse()
c=0
g=0
a=0
while(N-1-g>1):
    for i in range(N-1-g):
        for j in range(i+1,N-g):
            if A[i]!=-1 and  A[j]!=-1:
                if judge(A[i]+A[j])==1:
                    A.remove(A[i])
                    A.remove(A[j-1])
                    c=c+1
                    a=g
                    g=g+2
                    break
        if(a-g)>0:
            break

print(c)

Submission status: Time Limit Exceeded

Solution code:

def judge(x):
    
    
    while(x>1):
        if x%2 != 0:
            return 0
        x/=2
    
    return 1

N=int(input())
buf=[]
buf=input().split()
A=[]
for i in range(N):
    A.append(int(buf[i]))
A.sort()
A.reverse()
c=0
for i in range(N-1):
    for j in range(i+1,N):
        if A[i]!=-1 and  A[j]!=-1:
            if judge(A[i]+A[j])==1:
                A[i]=-1
                A[j]=-1
                c=c+1
                break
print(c)