Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int const N = 2e5 + 1;
int n;
long long a[N], powers[35];
vector<long long> sol, tmp;
bool check(long long x) {
for(int i = 0; i < tmp.size(); ++i)
if(!binary_search(powers, powers + 31, abs(tmp[i] - x)))
return false;
return true;
}
int main() {
powers[0] = 1;
for(int i = 1; i < 31; ++i)
powers[i] = powers[i - 1] * 2;
scanf("%d", &n);
for(int i = 0; i < n; ++i)
scanf("%lld", a + i);
sort(a, a + n);
for(int i = 0; i < n; ++i) {
long long cur = a[i];
tmp.push_back(cur);
for(int j = 0; j < 31; ++j) {
if(binary_search(a, a + n, cur + powers[j]) && check(cur + powers[j]))
tmp.push_back(cur + powers[j]);
if(binary_search(a, a + n, cur - powers[j]) && check(cur - powers[j]))
tmp.push_back(cur - powers[j]);
}
if(tmp.size() > sol.size())
sol.swap(tmp);
tmp.clear();
}
printf("%d\n", int(sol.size()));
for(int i = 0; i < sol.size(); ++i)
printf("%s%lld", i == 0 ? "" : " ", sol[i]);
puts("");
return 0;
}
Copy The Code &
Try With Live Editor
Input
6
3 5 4 7 10 12
3 5 4 7 10 12
Output
3
7 3 5
7 3 5
Demonstration
Codeforces Solution-D. Points and Powers of Two-Solution in C, C++, Java, Python,Points and Powers of Two