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

Submission attempts (in order):
class Node:
    def __init__(self,data):
        self.data=data
        self.left=None
        self.right=None
class BST:
    def __init__(self,number_list):
        self.root=None
        for node in number_list:
            self.insert(node)
    def insert(self,data):
        n=self.root
        if n is None:
            self.root=Node(data)
            return
        else:
            while True:
                entry=n.data
                if data<entry:
                    if n.left is None:
                        n.left=Node(data)
                        return
                    n=n.left
                elif data>entry:
                    if n.right is None:
                        n.right=Node(data)
                        return
                    n=n.right
                else:
                    n.data=data
                    return
    def search(self,target):
        n = self.root
        if n is None:
            return None
        else:
            lst = []
            lst.append(n)
            while len(lst) > 0:
                node = lst.pop()
                if node.data == target:
                    return True
                if node.right is not None:
                    lst.append(node.right)
                if node.left is not None:
                    lst.append(node.left)
            return False
        
import math
N=int(input())
a=list(map(int,input().split()))
t_max=int(math.log2(max(a)*2)+1)
ans=0
for t in range(t_max,0,-1):
    i=0
    while i<len(a):
    
        x,y=0,0
        bst=BST(a)
        if bst.search(2**t-a[i]):
            if a[i]!=2**t-a[i]:
                
                ans+=1
                x+=a[i]
                y+=2**t-a[i]
                
                a.remove(x)
                a.remove(y)
        else:
            i+=1 
print(ans)

class Node:
    def __init__(self,data):
        self.data=data
        self.left=None
        self.right=None
class BST:
    def __init__(self,number_list):
        self.root=None
        for node in number_list:
            self.insert(node)
    def insert(self,data):
        n=self.root
        if n is None:
            self.root=Node(data)
            return
        else:
            while True:
                entry=n.data
                if data<entry:
                    if n.left is None:
                        n.left=Node(data)
                        return
                    n=n.left
                elif data>entry:
                    if n.right is None:
                        n.right=Node(data)
                        return
                    n=n.right
                else:
                    n.data=data
                    return
    def search(self,target):
        n = self.root
        if n is None:
            return None
        else:
            lst = []
            lst.append(n)
            while len(lst) > 0:
                node = lst.pop()
                if node is None:
                    return False
                elif node.data == target:
                    self.node_recreate(node)
                    node=None
                    return True
                elif node.data < target :
                    lst.append(node.right)
                elif node.data > target:
                    lst.append(node.left)
            return False
        
    def node_recreate(self,node):
        
        num_lst=[]
        node_lst=[node]
        while len(node_lst)>0:
            node_=node_lst.pop()
            if node_.right is not None:
                node_lst.append(node_.right)
                num_lst.append(node_.right.data)
            if node_.left is not None:
                node_lst.append(node_.left)
                num_lst.append(node_.left.data)
        for num in num_lst:
            self.insert(num)
            
                
    
import math
N=int(input())
a=list(map(int,input().split()))
t_max=int(math.log2(max(a)*2)+1)
ans=0
bst=BST(a)
for t in range(t_max,0,-1):
    i=0
    while i<len(a):
    
        x,y=0,0


        if bst.search(2**t-a[i]):
            if a[i]!=2**t-a[i]:
                
                ans+=1
                x+=a[i]
                y+=2**t-a[i]
                
                a.remove(x)
                bst.search(x)
                print(bst.search(y))
                print(x,y)
                a.remove(y)
            else:
                i+=1
        else:
            i+=1 
print(ans)

class Node:
    def __init__(self,data):
        self.data=data
        self.left=None
        self.right=None
class BST:
    def __init__(self,number_list):
        self.root=None
        for node in number_list:
            self.insert(node)
    def insert(self,data):
        n=self.root
        if n is None:
            self.root=Node(data)
            return
        else:
            while True:
                entry=n.data
                if data<entry:
                    if n.left is None:
                        n.left=Node(data)
                        return
                    n=n.left
                elif data>entry:
                    if n.right is None:
                        n.right=Node(data)
                        return
                    n=n.right
                else:
                    n.data=data
                    return
    def search(self,target):
        n = self.root
        if n is None:
            return None
        else:
            lst = []
            lst.append(n)
            while len(lst) > 0:
                node = lst.pop()
                if node is None:
                    return False
                if node.data == target:
                    self.node_recreate(node)
                    return True
                elif node.data < target :
                    lst.append(node.right)
                elif node.data > target:
                    lst.append(node.left)
            return False
        
    def node_recreate(self,node):
        
        num_lst=[]
        node_lst=[node]
        while len(node_lst)>0:
            node_=node_lst.pop()
            if node_.right is not None:
                node_lst.append(node_.right)
                num_lst.append(node_.right.data)
            if node_.left is not None:
                node_lst.append(node_.left)
                num_lst.append(node_.left.data)
        node=None
        for num in num_lst:
            self.insert(num)
            
                
    
import math
N=int(input())
a=list(map(int,input().split()))
t_max=int(math.log2(max(a)*2)+1)
ans=0
bst=BST(a)
for t in range(t_max,0,-1):
    i=0
    while i<len(a):
    
        x,y=0,0


        if bst.search(2**t-a[i]):
            if a[i]!=2**t-a[i]:
                
                ans+=1
                x+=a[i]
                y+=2**t-a[i]
                
                a.remove(x)
                bst.search(x)
                a.remove(y)
        else:
            i+=1 
print(ans)

import math
N=int(input())
a=list(map(int,input().split()))
a_=sorted(a)
t_max=int(math.log2(a_[-1]+a_[-2])+1)
ans=0
for t in range(t_max,0,-1):
    i=0
    while i<len(a_):
        x,y=0,0
        if 2**t-a_[i] in a_:
            if a[i]!=2**t-a[i]:
                ans+=1
                x+=a_[i]
                y+=2**t-a_[i]
                a_.remove(x)
                a_.remove(y)
        else:
            i+=1
print(ans)

N=int(input())
a=list(map(int,input().split()))
a_=sorted(a,key=lambda x:-x)
ans=0
from collections import Counter
c=Counter(a_)
for i in a_:
    if c[i]==0:
        continue
    c[i]-=1
    m=1<<i.bit_length()
    j=m-i
    if j in c and c[j]>0:
        ans+=1
        c[j]-=1
print(ans)

Verdicts: Time Limit Exceeded

Runtime Error

Runtime Error

Runtime Error

Accepted