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:
g=[]
def judge(x):
    if x in g:
        return 1
    a=x
    while(x>1):
        if x%2 != 0:
            return 0
        x/=2
    g.append(a)
    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)

Submission status: Time Limit Exceeded

Solution code:
g=[]
def judge(x):
    if x in g:
        return 1
    a=x
    while(x>1):
        if x%2 != 0:
            return 0
        x/=2
    g.append(a)
    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
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
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 and judge(A[i]+A[j])==1:
            A[i]=-1
            A[j]=-1
            c=c+1
print(c)

Submission status: Time Limit Exceeded

Solution code:
import collections
N=int(input())
x=list(map(int,input().split()))
c=collections.Counter(x)
t=[2**i for i in range(len(str(bin(max(x)*2))[2:])+1)[::-1]]
count=0
x.sort()
for i in range(len(x))[::-1]:
    for j in range(len(t)):
        if t[j]-x[i] in c.keys():
            if t[j] < x[i]:
                break
            if t[j]-x[i] == x[i]:
                if c[x[i]] >= 2:
                    c[x[i]] -= 2
                    count +=1
            else:
                if c[t[j]-x[i]]>0 and c[x[i]]>0:
                    c[t[j]-x[i]] -= 1
                    c[x[i]] -= 1
                    count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from math import log2
n = int(input())
a = sorted(list(map(int, input().split())))
ans = 0
while a:
    m = a.pop()
    k = (1<<m.bit_length()) - m
    if k in a:
        a.remove(k)
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
def twopower(n):
    for i in range(32):
        if n < 2**i:
            return 2**i
N = int( input())
A = list( map( int, input().split()))
d = defaultdict( int)
e = defaultdict( int)
for i in range(N):
    d[A[i]] += 1
A.sort(reverse=True)
i = 0
ans = 0
while i < N:
    if e[A[i]] >= d[A[i]]:
        i += 1
        continue
    e[A[i]] += 1
    L = twopower(A[i]) - A[i]
    if e[L] < d[L]:
        ans += 1
        e[L] += 1
    i += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = [int(i) for i in input().split()]

A.sort()
m = 1 << 30
used = [False] * n
ans = 0

def search(ok, ng, n):
    if abs(ok-ng) <= 1:
            return ok
    mid = (ok+ng) // 2
    if A[mid] < n or (A[mid] == n and used[mid]):
        return search(mid, ng, n)
    else:
        return search(ok, mid, n)

for i in range(30):
    for j in range(n):
        if A[j] >= m:
            break
        if used[j]:
            continue
        a = search(-1, n, m - A[j]) + 1
        if a >= n:
            continue
        if A[a] == m - A[j]:
            if a == j:
                a += 1
                if a >= n or not A[a] == A[j]:
                    continue
            used[a] = True
            used[j] = True
            ans += 1
    m >>= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
x=list(map(int,input().split()))
x.sort(reverse=True)
t=[2**i for i in range(len(str(bin(max(x)*2))[2:])+1)[::-1]]
count=0
start=0
for i in range(len(x)):
    if x[i] != 0:
        #print("X=",x[i])
        for j in range(start,len(t)):
            #print(j)
            if t[j] > 2*x[i]:
                start=j
            if t[j] < x[i]:
                break
            if t[j]-x[i] in x[i+1:]:
                if t[j]-x[i] != 0:
                    x[i+1+x[i+1:].index(t[j]-x[i])] = 0
                    x[i]=0
                    count+=1
                    break
#print(x)
print(count)

Submission status: Time Limit Exceeded

Solution code:
import collections
 
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
c=collections.Counter(a)
ans=0
for i in a:
  for j in range(31):
    if(2**j>i):
      k=2**j-i
      break
  if k in c.keys():
    if i==k and c[i]>1:
      c[i]-=2
      ans+=1
    elif i!=k and c[i]>0 and c[k]>0:
      c[i]-=1
      c[k]-=1
      ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
x=list(map(int,input().split()))
x.sort(reverse=True)
t=[2**i for i in range(len(str(bin(max(x)*2))[2:])+1)[::-1]]
count=0
start=0
for i in range(len(x)):
    if x[i] != 0:
        #print("X=",x[i])
        for j in range(start,len(t)):
            #print(j)
            if t[j] > 2*x[i]:
                start=j
            if t[j] < x[i]:
                break
            if t[j]-x[i] in x[i:]:
                if t[j]-x[i] != 0 and x.index(t[j]-x[i]) != i:
                    x[x.index(t[j]-x[i])] = 0
                    x[i]=0
                    count+=1
                    break
            j+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
x=list(map(int,input().split()))
x.sort(reverse=True)
t=[2**i for i in range(len(str(bin(max(x)))[2:])+1)[::-1]]
count=0
for i in range(len(x)):
    if x[i] != 0:
        j=0
        while t[j] > x[i] and j < len(t):
            if t[j]-x[i] in x:
                if t[j]-x[i] != 0 and x.index(t[j]-x[i]) != i:
                    x[x.index(t[j]-x[i])] = 0
                    x[i]=0
                    count+=1
                    break
            j+=1
print(x.count(0)//2)

Submission status: Time Limit Exceeded

Solution code:
from math import log2
n = int(input())
a = sorted(list(map(int, input().split())))
ans = 0
while a:
    m = a.pop()
    for i in range(int(log2(m))+2):
        k = 2**i - m
        if k in a:
            a.remove(k)
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

A.sort(reverse=True)
c = Counter(A)
res = 0
for a in A:
    for i in range(1, 33):
        n = 2 ** i - a
        if n in c.keys():
            if a == n and c[a] > 1:
                c[a] -= 2
                res += 1
            elif a != n and c[a] > 0 and c[n] > 0:
                c[a] -= 1
                c[n] -= 1
                res += 1
    
print(res)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())))
ans = 0
while a:
    m = a.pop()
    for i in range(31):
        k = 2**i - m
        if k in a:
            a.remove(k)
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# cook your dish here
import bisect
import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int,input().split()))
a.sort()
 
b = 1
while b <= a[n-1]:
    b = b*2

c = [None]*n #使ったら1
 
res = 0
 
for i in range(n-1,-1,-1):
    if c[i] == None:
        
        while b > a[i]:
            b = b//2
        b = b*2
        w1 = bisect.bisect_left(a,b-a[i])
        w2 = bisect.bisect_right(a,b-a[i])
        if w2 >w1:
            for p in range(w1,w2):
                if c[p] == None and i != p:
                    res += 1
                    c[p] = 1
                    c[i] = 1
                    break
                
 
print(res)

Submission status: Time Limit Exceeded

Solution code:
# cook your dish here
import bisect
import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int,input().split()))
a.sort()
 
 
c = [None]*n #使ったら1
 
res = 0
 
for i in range(n-1,-1,-1):
    if c[i] == None:
        b = 1
        while b <= a[i]:
            b = b*2
        w1 = bisect.bisect_left(a,b-a[i])
        w2 = bisect.bisect_right(a,b-a[i])
        if w2 >w1:
            for p in range(w1,w2):
                if c[p] == None and i != p:
                    res += 1
                    c[p] = 1
                    c[i] = 1
                    break
                
 
print(res)

Submission status: Time Limit Exceeded

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

A.sort()
#print(a)
A = A[::-1]
res = 0
while len(A) >= 2:
    for a in A[1:]:
        if not (A[0] + a & (A[0] + a - 1)):
            res += 1
            A.remove(a)
            break
    A.pop(0)
            
print(res)

Submission status: Time Limit Exceeded

Solution code:
# cook your dish here
import bisect
import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int,input().split()))
a.sort()
 
 
c = [0]*n #使ったら1
 
res = 0
 
for i in range(n-1,-1,-1):
    if c[i] == 0:
        b = 1
        while b <= a[i]:
            b = b*2
        w1 = bisect.bisect_left(a,b-a[i])
        w2 = bisect.bisect_right(a,b-a[i])
        if w2 >w1:
            for p in range(w1,w2):
                if c[p] == 0 and i != p:
                    res += 1
                    c[p] = 1
                    c[i] = 1
                    break
                
 
print(res)

Submission status: Time Limit Exceeded

Solution code:
# cook your dish here
import bisect
n = int(input())
a = list(map(int,input().split()))
a.sort()
 

c = [0]*n #使ったら1
 
res = 0
 
for i in range(n-1,-1,-1):
    if c[i] == 0:
        b = 1
        while b <= a[i]:
            b = b*2
        w1 = bisect.bisect_left(a,b-a[i])
        w2 = bisect.bisect_right(a,b-a[i])
        if w2 >w1:
            for p in range(w1,w2):
                if c[p] == 0 and i != p:
                    res += 1
                    c[p] = 1
                    c[i] = 1
                    break
                

print(res)

Submission status: Time Limit Exceeded

Solution code:
# cook your dish here
import bisect
n = int(input())
a = list(map(int,input().split()))
a.sort()
 

c = [0]*n #使ったら1
 
res = 0
 
for i in range(n-1,-1,-1):
    if c[i] == 0:
        b = 1
        while b <= a[i]:
            b = b*2
        w1 = bisect.bisect_left(a,b-a[i])
        w2 = bisect.bisect_right(a,b-a[i])
        if w2 >w1:
            p = w1
            while p != w2:
                if c[p] == 0 and i != p:
                    res += 1
                    c[p] = 1
                    c[i] = 1
                    p = w2-1
                p += 1

print(res)

Submission status: Time Limit Exceeded

Solution code:
import collections

N = input()
A = map(int, raw_input().split())
C = collections.Counter(A)
K = C.keys()
K.sort(reverse=1)
res = 0
for i in K:
    for j in xrange(60):
        p = 2 ** j
        if i > p:
            continue
        elif i == p:
            res += C[i] // 2
            C[i] = C[i] // 2 * 2
        else:
            num = min(C[i], C[p - i])
            res += num
            C[i] -= num
            C[p - i] -= num
print res

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

ans = 0
while(len(A) > 0):
    a = A.pop()
    b = (1 << a.bit_length()) - a
    i = bisect.bisect_left(A, b)
    if i < len(A) and A[i] == b:
        ans += 1
        A.pop(i)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

N = input()
A = map(int, raw_input().split())
A.sort(reverse=1)
c = collections.Counter(A)
res = 0
for i in A:
    for j in xrange(60):
        p = 2 ** j
        if i > p:
            continue
        elif i == p:
            res += c[i] // 2
            c[i] = c[i] // 2 * 2
        else:
            num = min(c[i], c[p - i])
            res += num
            c[i] -= num
            c[p - i] -= num
print res

Submission status: Time Limit Exceeded

Solution code:
import math
N=int(input())
A=list(map(int,input().split()))
Ae1=[]
Ao1=[]
for i in range(N):
    if A[i]%2==0:
        Ae1.append(A[i])
    else:
        Ao1.append(A[i])
if Ao1!=[]:
    s=0
    Ao=sorted(Ao1,reverse=True)
    i=0
    while i<=len(Ao)-1:
        a=int(math.log2(Ao[i]))
        try:
            Ao.pop(Ao[i+1:].index(2**(a+1)-Ao[i])+i+1)
            s+=1
            i+=1
        except:
            i+=1
else:
    s=0
if Ae1!=[]:
    t=0
    Ae=sorted(Ae1,reverse=True)
    j=0
    while j<=len(Ae)-1:
        b=int(math.log2(Ae[j]))
        try:
            Ae.pop(Ae[j+1:].index(2**(b+1)-Ae[j])+j+1)
            t+=1
            j+=1
        except:
            j+=1
else:
    t=0
print(s+t)

Submission status: Time Limit Exceeded

Solution code:
import math
N=int(input())
A=list(map(int,input().split()))
Ae1=[]
Ao1=[]
for i in range(N):
    if A[i]%2==0:
        Ae1.append(A[i])
    else:
        Ao1.append(A[i])
if Ao1!=[]:
    s=0
    Ao=sorted(Ao1,reverse=True)
    i=0
    while i<=len(Ao)-1:
        a=int(math.log2(Ao[i]))
        if 2**(a+1)-Ao[i] in Ao[i+1:]:
            s+=1
            Ao.remove(2**(a+1)-Ao[i])
            i+=1
        else:
            i+=1
else:
    s=0
if Ae1!=[]:
    t=0
    Ae=sorted(Ae1,reverse=True)
    j=0
    while j<=len(Ae)-1:
        b=int(math.log2(Ae[j]))
        if 2**(b+1)-Ae[j] in Ae[j+1:]:
            t+=1
            Ae.remove(2**(b+1)-Ae[j])
            j+=1
        else:
            j+=1
else:
    t=0
print(s+t)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n = input()
a = list(map(int, input().split()))
a.sort(reverse=True)
count = 0
for k, i in enumerate(a):
  pair = 2**i.bit_length()-i
  if pair in a[k+1:]:
    count+=1
    a.remove(pair)
print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect
from collections import defaultdict

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

a.sort()
memo = defaultdict(int)
for i in range(n):
    memo[a[i]] += 1

count = 0
for t in reversed(range(1, 31)):
    for i in reversed(range(n)):
        if memo[a[i]] == 0:
            continue
        front = bisect.bisect_left(a, (1 << t) - a[i])
        back = bisect.bisect_right(a, (1 << t) - a[i])
        if front == back:
            continue
        if a[front] == a[i] and memo[a[i]] <= 1:
            continue
        if memo[a[front]] >= 1:
            memo[a[front]] -= 1
            memo[a[i]] -= 1
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect
from collections import defaultdict

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

a.sort()
memo = defaultdict(int)
for i in range(n):
    memo[a[i]] += 1

count = 0
for t in reversed(range(1, 31)):
    for i in reversed(range(n)):
        if memo[a[i]]== 0:
            continue
        front = bisect.bisect_left(a, (1 << t) - a[i])
        back = bisect.bisect_right(a, (1 << t) - a[i])
        if front == back:
            continue
        if a[front] == a[i] and memo[a[i]] <= 1:
            continue
        if memo[a[front]] >= 1:
            memo[a[front]] -= 1
            memo[a[i]] -= 1
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
def main():
    _ = int(input())
    nums = list(map(int, input().split()))
    n = len(nums)
    nums.sort()

    #print(n)
    #print(nums)

    num_bin_digits = [1 << len("{:b}".format(x)) for x in nums]
    cnt = 0
    #print(num_bin_digits)

    for cur in reversed(range(n)):
        num = nums[cur]
        if not num:
            continue
        power_of_2 = num_bin_digits[cur]
        pair = power_of_2 - num
        idx = -1
        if pair in nums[:cur]:
            idx = nums.index(pair)

        if idx != -1:
            nums[idx] = False
            cnt += 1

        #print(idx, num, power_of_2, pair)
        #print(cnt, nums)

    print(cnt)

    return

main()

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

import bisect

n=int(input())
a = [int(i) for i in input().split()]
a.sort()
check = [0]*n
#print(a)

ans=0

while(a):
    c=a.pop()
    d=c
    keta=0
    while(d!=0):
        d=d//2
        keta +=1
    x = 2**keta - c
#    print(c,x)
    i = bisect.bisect_left(a, x)
    if i != len(a) and a[i] == x:
        ans += 1
#        print(a[i],c,"xxx")
        a.remove(x)



print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

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

ans = 0
while(len(A) > 0):
    a = A[0]
    A.pop(0)
    for i in range(len(A)):
        if math.log2(A[i] + a).is_integer():
            ans += 1
            A.pop(i)
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

N = int(input())
a = sorted(list(map(int,input().split())))

count = 0



while(a!=[]):
    temp = a[-1]
    a.pop(-1)
    b=2**math.floor(math.log2(temp))
    if a ==[]:
        break
    if 2*b-temp in a:
        count += 1
        a.pop(a.index(2*b-temp))
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
import math

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

ans = 0
while(len(A) > 0):
    a = A[0]
    A = A[1:]
    for i in range(len(A)):
        if math.log2(A[i] + a).is_integer():
            ans += 1
            A.pop(i)
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)

def check_pow_two(n):
    while(n > 0):
        if n % 2 == 0:
            n //= 2
        elif n == 1:
            return True
        else:
            return False

ans = 0
while(len(A) > 0):
    a = A[0]
    A = A[1:]
    for i in range(len(A)):
        if check_pow_two(A[i] + a):
            ans += 1
            A.pop(i)
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

N = int(input())
a = sorted(list(map(int,input().split())))

count = 0
while(a!=[]):
    temp = a[-1]
    a.pop(-1)
    b=2**math.floor(math.log2(temp))
    if a ==[]:
        break
    if 2*b-temp in a:
        count += 1
        a.pop(a.index(2*b-temp))
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
u = [0 for i in range(len(a))]

def binarySearch(alist, item,exhi):
    first = 0
    last = len(alist)-1
    found = False

    while first<=last and not found:
        midpoint = (first + last)//2
        if alist[midpoint] == item and u[midpoint] <= 0 and midpoint != exhi:
            found = True
            u[midpoint] += 1
        else:
            if item > alist[midpoint]:
                last = midpoint-1
            else:
                first = midpoint+1

    return found


b = [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 
     131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 
     67108864, 134217728, 268435456, 536870912]

sum = 0
i = 0

#print(a)
while True:

    if u[i] > 0:
        i = i + 1
        if i >= len(a)-1:
            break        
        continue

    t = 0
    for bb in b :
        if bb > a[i]:
            t = bb - a[i]
            break
    
    if t > a[i]:
        i = i + 1
        if i >= len(a)-1:
            break
        continue

    if t in a :
        idx = 0
        while True:
            try:
                idx = a.index(t,idx)
                if u[idx] == 0 and idx != i:
                    u[idx] += 1
                    sum += 1
                    break
                elif idx == i:
                    break
            except:
                break
            
    i = i + 1
    if i >= len(a)-1:
        break

print(sum)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def ans_solve(N, As):
    C = Counter(As)
    ans = 0
    while len(C):
        d = max(C.keys())
        n = (2 ** d.bit_length()) - d
        if d != n:
            dc = C[d]
            dn = C[n] if n in C else 1
            mn = min(dc, dn)
            C[d] -= mn
            if n in C:
                C[n] -= mn
                ans += mn
        else:
            if C[d] >= 2:
                ans += C[d]//2
                C[d] -= 2 * C[d]//2
            elif C[d] >= 1:
                C[d] -= 1

        if C[d] == 0:
            del C[d]
        if n in C and C[n] == 0:
            del C[n]
    return ans


if __name__ == "__main__":
    N = input()
    As = [int(c) for c in input().split()]
    print(ans_solve(N, As))

Submission status: Time Limit Exceeded

Solution code:
import math

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

count = 0
a.sort()

while(a!=[]):
    temp = a[-1]
    a.pop(-1)
    b=2**math.floor(math.log2(temp))
    if a ==[]:
        break
    if 2*b-temp in a:
        count += 1
        a.pop(a.index(2*b-temp))
    
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n = input()
a = list(map(int, input().split()))
a.sort(reverse=True)
count = 0
for k, i in enumerate(a):
  pair = 2**(len(format(i,'b')))-i
  if pair in a[k+1:]:
    count+=1
    a.remove(pair)
print(count)

Submission status: Time Limit Exceeded

Solution code:
import math

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

count = 0
a.sort()
a.reverse()

while(a!=[]):
    temp = a[0]
    a.pop(0)
    b=2**math.floor(math.log2(temp))
    if a ==[]:
        break
    if 2*b-temp in a:
        count += 1
        a.pop(a.index(2*b-temp))
    
    


print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n = input()
a = list(map(int, input().split()))
a.sort(reverse=True)
count = 0
ex_list = []
for k, i in enumerate(a):
  ex_list.append(i)
  pair = 2**(len(format(i,'b')))-i
  if a.count(pair) - ex_list.count(pair)>0:
    count+=1
    a.remove(pair)
print(count)

Submission status: Time Limit Exceeded

Solution code:

N = int(input())
aA = sorted([int(_) for _ in input().split()],reverse=True)
aA1 = [ _ for _ in aA if _ % 2 ]
aA0 = [ _ for _ in aA if _ % 2 == 0 ]


def searchPair(aD):
    iL = len(aD)
    dsT =set()
    iC = 0
    for i in range(iL):
        for j in range(i+1,iL):
            if i in dsT or j in dsT:
                continue
            iWa =  aD[i] + aD[j]
            if not ( iWa & (iWa - 1)):
                dsT.add(i)
                dsT.add(j)
                iC += 1
                break

    return iC
iC = 0
iC += searchPair(aA1)
iC += searchPair(aA0)
print(iC)

Submission status: Time Limit Exceeded

Solution code:
import math

N = int(input())
a = list(map(int,input().split()))
a_odds = []
a_even = []

for i in range(N):
    if a[i]%2 != 0:
        a_odds.append(a[i])
    else:
        a_even.append(a[i])

a_odds.sort()
a_odds.reverse()
a_even.sort()
a_even.reverse()

count = 0
alist = []
num= 0
if a_odds!=[] and a_even!=[]:
    num = max(a_odds[0],a_even[0])
elif a_odds ==[]:
    num= a_even[0]
elif a_even == []:
    num = a_odds[0]
    
for i in range(math.floor(math.log2(num))+1,0,-1):
    alist.append(2**i)


while(a_odds!=[]):
    temp = a_odds[0]
    a_odds.pop(0)
    b=2**math.floor(math.log2(temp))
    if a_odds ==[]:
        break
    if 2*b-temp in a_odds:
        count += 1
        a_odds.pop(a_odds.index(2*b-temp))
    
    
while(a_even!=[]):
    temp = a_even[0]
    a_even.pop(0)
    if a_even==[]:
        break
    for i in alist:
        if temp > i:
            break
        if (i-temp) in a_even:
            count += 1
            a_even.pop(a_even.index(i-temp))
            break
    

print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import os, sys
# def debug(*args): print(*args, file=sys.stderr)# os.environ.get("USER")
def debug(*args): os.environ.get("USER")
N=int(input())
A=[int(i) for i in input().split()]
dd=defaultdict(int)
for a in A:
    dd[a]+=1
#print(dd)
B=sorted(A,reverse=True)
ans=0
for x in B:
    debug(A)
    #print(x)
    if dd.get(x,-1)==0:
        continue
    s=len(bin(x))-3
    if x==1<<s:
        t=dd.get(x,-1)
        if t!=-1:
            ans+=t//2
            dd[x]-=(t//2)*2
            #print(ans)
            #print(dd)
    else:
        s+=1
        #print((1<<s)-x)
        t=dd.get((1<<s)-x,-1)
        if t>=1:
            ans+=1
            dd[x]-=1
            dd[(1<<s)-x]-=1
            #print(ans)
            #print(dd)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def ans_solve(N, As):
    C = Counter(As)
    ans = 0
    while len(C):
        d = max(C.keys())
        C[d] -= 1
        if C[d] == 0:
            del C[d]

        n = (2 ** d.bit_length()) - d
        if n in C:
            C[n] -= 1
            ans += 1
            if C[n] == 0:
                del C[n]
    return ans


if __name__ == "__main__":
    N = input()
    As = [int(c) for c in input().split()]
    print(ans_solve(N, As))

Submission status: Time Limit Exceeded

Solution code:
import bisect
n = int(input())
a = list(map(int,input().split()))
a.sort()
 
b = [2147483648, 1073741824, 536870912, 268435456, 134217728, 67108864, 33554432, 16777216, 8388608, 4194304, 2097152, 1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2,0]
 
c = [0]*n #使ったら1
 
res = 0
 
for i in range(31):
    k1 = bisect.bisect_left(a,b[i+1])
    k2 = bisect.bisect_right(a,b[i])
 
    if k2 > k1:
 
        for j in range(k1,k2):
            if c[j] == 0:
                w1 = bisect.bisect_left(a,b[i]-a[j])
                w2 = bisect.bisect_right(a,b[i]-a[j])
 
                if w2 >w1:
                    p = w1
                    while p != w2:
                        if c[p] == 0 and j != p:
                            res += 1
                            c[p] = 1
                            c[j] = 1
                            p = w2-1
                        p += 1
 
print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import os, sys
def debug(*args): print(*args, file=sys.stderr)# os.environ.get("USER")
N=int(input())
A=[int(i) for i in input().split()]
dd=defaultdict(int)
for a in A:
    dd[a]+=1
#print(dd)
B=sorted(A,reverse=True)
ans=0
for x in B:
    debug(A)
    #print(x)
    if dd.get(x,-1)==0:
        continue
    s=len(bin(x))-3
    if x==1<<s:
        t=dd.get(x,-1)
        if t!=-1:
            ans+=t//2
            dd[x]-=(t//2)*2
            #print(ans)
            #print(dd)
    else:
        s+=1
        #print((1<<s)-x)
        t=dd.get((1<<s)-x,-1)
        if t>=1:
            ans+=1
            dd[x]-=1
            dd[(1<<s)-x]-=1
            #print(ans)
            #print(dd)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys, os
try: from typing import Any, Union, List, Tuple, Dict
except ImportError: pass
sys.setrecursionlimit(10**6)
def debug(*args): True if os.environ.get("USER")=="shun" else None
def exit(): sys.exit(0)


n = int(input())
a = list(map(int, input().split()))
a.sort()
max_a = a[-1]
m = 0
while True:
    if 2**m > max_a:
        break
    m += 1
used = [False] * n


def bisect(val, start, end):
    debug(used)
    if start >= end:
        return None
    elif start + 1 == end:
        if a[start] == val:
            if used[start]:
                return None
            used[start] = True
            return start
        else:
            return None
    mid = (start + end) // 2
    if a[mid] == val:
        if used[mid]:
            res = bisect(val, start, mid)
            return res
        else:
            res = bisect(val, mid+1, end)
            if res is not None:
                return res
            used[mid] = True
            return mid
        # if used[mid]:
        #     res = bisect(val, mid+1, end)
        #     if res is not None:
        #         return res
        #     res = bisect(val, start, mid)
        #     return res
        # else:
        #     used[mid] = True
        #     debug("fuga", used)
        #     return mid
    elif a[mid] > val:
        return bisect(val, start, mid)
    else:
        return bisect(val, mid, end)
    # for i in reversed(range(end)):
    #     if (not used[i]) and (a[i]  == val):
    #         used[i] = True
    #         return i
    return None


def _find(b, end, e):
    # debug(b, end, e)
    val = 2**e - b
    if val <= 0:
        return None
    return bisect(val, 0, end)
    # for i in reversed(range(end)):
    #     if (not used[i]) and (a[i] + b == 2**e):
    #         used[i] = True
    #         return i
    # return None


def find(b, end):
    for e in reversed(range(m+1)):
        res = _find(b, end, e)
        if res is not None:
            return True
    return False


ans = 0
for j in reversed(range(n)):
    if used[j]:
        continue
    b = a[j]
    used[j] = True
    res = find(b, j)
    if res:
        ans += 1


print(ans)

Submission status: Time Limit Exceeded

Solution code:
def make_pair(l):
    cpair = 0
    for c in range(len(l)):
        if l[c] == None:
            continue
        for cp in range(c+1, len(l)):
            if l[cp] == None:
                continue
            if is_power_of_two(l[c] + l[cp]):
                # print("ok: {}\t{}".format(l[c], l[cp]))
                cpair += 1
                l[cp] = None
                break
    return cpair


def is_power_of_two(num):
    return bin(num).count("1") == 1


def main():
    input()
    a = tuple(int(ns) for ns in input().split())
    ki = []
    gu = []

    for i in range(len(a)):
        if a[i] % 2:
            ki.append(a[i])
        else:
            gu.append(a[i])

    # print(gu, ki)
    ans = make_pair(sorted(gu, reverse=True))
    ans += make_pair(sorted(ki, reverse=True))
    print(ans)
main()