Language: C++

#include<bits/stdc++.h>
#define int long long
#define f(i,n) for(int i=0;i<n;i++)
#define F first
#define S second
#define mod 1000000007
#define P pair<int,int>
using namespace std;
vector<int> v;
signed main(){
	int n,c,ans=0,j[30];
	cin>>n;
	f(i,n){
		cin>>c;
		v.push_back(c);
	}
	sort(v.begin(),v.end());
	j[0]=1;
	for(int i=1;i<30;i++)j[i]=j[i-1]*2;
	for(int i=n-1;i>=1;i--){
		f(k,30){
			if(j[k]>v[i]){
				if(binary_search(v.begin(),v.begin()+i,j-v[i])){
					ans++;
					v.erase(upper_bound(v.begin(),v.begin()+i,j-v[i])-1);
					i--;
				}
			}
		}
	}
	cout<<ans<<endl;
}


Compiler output:
In file included from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algo.h: In instantiation of 'bool std::binary_search(_FIter, _FIter, const _Tp&) [with _FIter = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Tp = long long int*]':
a.cc:23:21:   required from here
   23 |                                 if(binary_search(v.begin(),v.begin()+i,j-v[i])){
      |                                    ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:2207:39: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
 2207 |       return __i != __last && !(__val < *__i);
      |                                ~~~~~~~^~~~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:71,
                 from /usr/include/c++/14/algorithm:60:
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_less_val::operator()(_Iterator, _Value&) const [with _Iterator = __gnu_cxx::__normal_iterator<long long int*, std::vector<long long int> >; _Value = long long int* const]':
/usr/include/c++/14/bits/stl_algobase.h:1504:14:   required from '_ForwardIterator std::__lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&, _Compare) [with _ForwardIterator = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Tp = long long int*; _Compare = __gnu_cxx::__ops::_Iter_less_val]'
 1504 |           if (__comp(__middle, __val))
      |               ~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:2205:22:   required from 'bool std::binary_search(_FIter, _FIter, const _Tp&) [with _FIter = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Tp = long long int*]'
 2205 |         = std::__lower_bound(__first, __last, __val,
      |           ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
 2206 |                              __gnu_cxx::__ops::__iter_less_val());
      |                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:23:21:   required from here
   23 |                                 if(binary_search(v.begin(),v.begin()+i,j-v[i])){
      |                                    ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:69:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
   69 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Val_less_iter::operator()(_Value&, _Iterator) const [with _Value = long long int* const; _Iterator = __gnu_cxx::__normal_iterator<long long int*, std::vector<long long int> >]':
/usr/include/c++/14/bits/stl_algo.h:1993:14:   required from '_ForwardIterator std::__upper_bound(_ForwardIterator, _ForwardIterator, const _Tp&, _Compare) [with _ForwardIterator = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Tp = long long int*; _Compare = __gnu_cxx::__ops::_Val_less_iter]'
 1993 |           if (__comp(__val, __middle))
      |               ~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:2028:32:   required from '_FIter std::upper_bound(_FIter, _FIter, const _Tp&) [with _FIter = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Tp = long long int*]'
 2028 |       return std::__upper_bound(__first, __last, __val,
      |              ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
 2029 |                                 __gnu_cxx::__ops::__val_less_iter());
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:25:25:   required from here
   25 |                                         v.erase(upper_bound(v.begin(),v.begin()+i,j-v[i])-1);
      |                                                 ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:98:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
   98 |       { return __val < *__it; }
      |                ~~~~~~^~~~~~~