Algorithm


D. Points and Powers of Two
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n distinct points on a coordinate line, the coordinate of i-th point equals to xi��. Choose a subset of the given set of points such that the distance between each pair of points in a subset is an integral power of two. It is necessary to consider each pair of points, not only adjacent. Note that any subset containing one element satisfies the condition above. Among all these subsets, choose a subset with maximum possible size.

In other words, you have to choose the maximum possible number of points xi1,xi2,,xim��1,��2,…,��� such that for each pair xij���xik��� it is true that |xijxik|=2d|���−���|=2� where d is some non-negative integer number (not necessarily the same for each pair of points).

Input

The first line contains one integer n (1n21051≤�≤2⋅105) — the number of points.

The second line contains n pairwise distinct integers x1,x2,,xn�1,�2,…,�� (109xi109−109≤��≤109) — the coordinates of points.

Output

In the first line print m — the maximum possible number of points in a subset that satisfies the conditions described above.

In the second line print m integers — the coordinates of points in the subset you have chosen.

If there are multiple answers, print any of them.

Examples
input
Copy
6
3 5 4 7 10 12
output
Copy
3
7 3 5
input
Copy
5
-1 2 5 8 11
output
Copy
1
8
Note

In the first example the answer is [7,3,5][7,3,5]. Note, that |73|=4=22|7−3|=4=22|75|=2=21|7−5|=2=21 and |35|=2=21|3−5|=2=21. You can't find a subset having more points satisfying the required property.

 



 

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

x
+
cmd
6
3 5 4 7 10 12

Output

x
+
cmd
3
7 3 5
Advertisements

Demonstration


Codeforces Solution-D. Points and Powers of Two-Solution in C, C++, Java, Python,Points and Powers of Two

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+