We are given n distinct integer points on a coordinate line. We need to select a subset of maximum size such that for every pair in the subset, the absolute difference is a power of two (2^d, d non-negative integer, i.e., 1,2,4,8,...). Note: 2^0=1. So differences can be 1,2,4,8,...
We need to output the size and any such subset.
Constraints: n up to 200,000, coordinates in [-1e9,1e9].
Goal: Find maximum subset where all pairwise distances are powers of two.
Observations:
This is essentially a graph problem: vertices are points, edges exist if difference is a power of two. We want the largest clique (complete subgraph) in this graph. That's NP-hard in general, but the graph has special structure because edges are defined by distances being powers of two. Perhaps the graph is "power-of-two" distance graph, which might have limited structure.
We need to find a set of points where all differences are powers of two. Let's think in terms of integers: We have a set S of integers (distinct). We need S such that for any two a,b in S, a-b is a power of two (positive, so absolute difference is exactly 2^k for some k>=0). That implies that S is a set where all pairwise differences are powers of two.
What are possible sets with this property? Let's explore small sizes.
Size 1: trivial.
Size 2: any two points with difference power of two.
Size 3: We need three numbers a,b,c such that each pair difference is a power of two. Let's denote differences: |a-b| = 2^i, |a-c| = 2^j, |b-c| = 2^k, with i,j,k >=0. Are there triples? For example, {0,2,4}? Differences: 2,4,2 -> 2 is power of two, 4 is power of two, okay. {0,1,3}? differences:1,3,2 -> 3 not power of two. {0,2,6}? 2,6,4 -> 6 not. {0,4,6}? 4,6,2 -> 6 not. {0,1,2}? 1,1,2 -> all powers? 1=2^0,2=2^1, so yes! {0,1,2}: differences: 1,2,1 all powers. So {0,1,2} works. {0,2,3}? 2,3,1 -> 3 not. {0,4,8}? 4,8,4 -> all powers. So {0,4,8} works. {0,2,6}? no. {1,2,3}? differences: 1,2,1 -> works! So any three numbers forming an arithmetic progression with difference 1? But 1 and 2 are powers, but also need difference between endpoints: 2 = 2^1. So any three consecutive integers? For example {3,4,5}: differences: 1,2,1 -> works. So three consecutive integers works. Actually any three numbers equally spaced? Suppose {a, a+d, a+2d} with d>0. Differences: d, 2d, d. For these to be powers of two, we need d = power of two, and 2d = 2 * (power of two) must be power of two. That requires d = 2^k, then 2d = 2^(k+1) also power of two. So any three-term arithmetic progression with common difference a power of two works. But also we have the possibility of non-equally spaced triples, e.g., {0,1,3}? fails. {0,2,3}? fails. Let's think systematically: Suppose we have numbers a<b<c. Conditions: b-a = 2^i, c-b = 2^j, c-a = 2^k. Since c-a = (b-a)+(c-b) = 2^i+2^j. For it to be a power of two, sum of two powers of two must be a power of two. That happens only if 2^i = 2^j (i=j) because sum of two distinct powers of two is not a power of two (unless one is zero, but powers are positive). Example: 2^i + 2^j with i != j: say 1+2=3 not power of two; 1+4=5; 2+4=6; 4+8=12; etc. So for 2^i+2^j to be power of two, we must have i=j, because then 2^i+2^i = 2*2^i = 2^(i+1). So condition: b-a = c-b = 2^i, and then c-a = 2^(i+1). Therefore, three numbers must be equally spaced with common difference a power of two. So any three-point set satisfying condition must be arithmetic progression with step a power of two. But wait: is that necessary? Could there be triples where the smallest and largest difference is a power of two, but the middle point is not between them? But since points are on a line, ordering is fixed. So yes, a<b<c implies b-a and c-b positive. So condition forces b-a = c-b = d, a power of two. So any triple of distinct points must be equally spaced with step a power of two. So any set of size >=3 must be such that when sorted, consecutive differences are equal and equal to some power of two? But careful: For sets larger than 3, consider size 4: a<b<c<d. Conditions: For any pair, differences are powers of two. In particular, consecutive differences: b-a, c-b, d-c must each be powers of two. Also differences like a-c, a-d, b-d must be powers of two. Let's see what constraints arise.
Take any three points, they must be equally spaced with step equal to some power of two. For four points, consider a,b,c. They must satisfy b-a = c-b = d (some power). Consider a,b,d: must satisfy b-a = d-b? Actually for triple (a,b,d): they must be equally spaced. So we have b-a = d-b? That would imply d = 2b - a. But b = a+d, so d = 2(a+d)-a = a+2d => d = a+2d => a + d = 0? That seems forced. Let's do properly.
Let sorted points: x1 < x2 < x3 < x4. For any three indices i<j<k, we must have x_j - x_i = x_k - x_j = some power, and then x_k - x_i = 2*(x_j - x_i). So the three are in arithmetic progression with common difference equal to some power of two.
Now, take (x1,x2,x3): they must be equally spaced: x2 - x1 = x3 - x2 = d1 (power of two). So x2 = x1 + d1, x3 = x1 + 2d1.
Take (x1,x2,x4): must be equally spaced: x2 - x1 = x4 - x2 = d2. So x4 = x2 + d2 = x1 + d1 + d2. But we also have x4 - x1 = (x1+d1+d2) - x1 = d1+d2 must be a power of two (since x1 and x4 pair). And also must equal 2*(x2 - x1) = 2d1? Wait, from (x1,x2,x4) equally spaced: x2-x1 = x4-x2 => x4 = x2 + (x2-x1) = x1+2d1. Actually careful: For triple (x1,x2,x4), the condition is that the two differences are equal: x2 - x1 = x4 - x2. That gives x4 = 2x2 - x1 = 2(x1+d1) - x1 = x1+2d1. So that forces d2 = d1? Because from earlier we set x4 = x1+d1+d2, equating: x1+2d1 = x1+d1+d2 => d2 = d1. So indeed (x1,x2,x4) forces x4 = x1+2d1. So x4 = x1+2d1. But from (x1,x2,x3) we have x3 = x1+2d1 as well. That would mean x3 = x4, contradiction because points distinct. So impossible? But we know there are sets of size 4 that satisfy the condition? Let's test with example: maybe {0,1,2,3}? Check differences: all pairs? 0-3 = 3 not power of two. So fails. {0,2,4,6}? Differences: 0-6=6 not power. {0,2,4,8}? 0-8=8 power; 2-8=6 not; fails. {0,4,8,12}? 0-12=12 not; fails. {0,2,4,8}? already fails. {0,1,2,4}? 1-4=3; fails. Could there be a size-4 set? Let's brute think: Suppose we have set {a, a+d, a+2d, a+3d}? Then differences like a and a+3d = 3d, need 3d power of two. Since d is power of two, say d=2^k, then 3d = 32^k, not power of two unless k such that 32^k is power of two, impossible. So arithmetic progression of length 4 fails.
Maybe non-equally spaced? Let's attempt to find four numbers where all pairwise distances are powers of two. Known from problem statements: The answer is at most 3? Actually the problem examples show answer 3 for first, and 1 for second. Could the maximum size be at most 3? Let's explore. Might be that maximum clique size in this graph is at most 3. Is that always true? Let's test small sets: can we find 4 numbers with all pairwise differences powers of two? Let's try to search manually: Suppose we have numbers 0,1,2,4? Check pairs: 0-1=1 ok;0-2=2 ok;0-4=4 ok;1-2=1 ok;1-4=3 not ok. So fails. 0,2,4,6? 0-6=6 fails. 0,1,2,3? fails. 0,4,8,12? 0-12=12 fails. 0,4,8,16? 4-16=12 fails. 0,8,16,24? 0-24=24 fails. 0,1,3,4? 0-3=3 fails. 0,2,6,8? 0-6=6 fails. 0,2,8,10? 2-10=8 ok? Let's check: 0,2,8,10. Differences: 0-2=2 ok;0-8=8 ok;0-10=10 not; fail. 0,4,6,8? 0-6=6 fail. 2,4,8,10? 2-10=8 ok;4-10=6 fail. 1,3,5,7? many odd differences. It seems improbable.
Let's attempt systematic reasoning: For any four distinct points sorted a<b<c<d. The conditions impose that for each triple they must be equally spaced. As we saw, triple (a,b,c) forces b-a = c-b = d1 (power). Triple (a,b,d) forces b-a = d-b => d = 2b - a = 2(a+d1) - a = a+2d1 => d = a+2d1. But that would make c = a+2d1 as well, so c = d, contradiction. Therefore no four distinct points can satisfy the condition. Wait, is the triple (a,b,d) necessarily required? Yes, because condition applies to every pair, so any three points must satisfy the equally spaced property. So indeed (a,b,d) must be equally spaced. That gave d = a+2(b-a). Since b-a = d1, we get d = a+2d1. And from (a,b,c) we already have c = a+2d1. So c=d, contradiction. Thus it is impossible to have four distinct points. Therefore maximum size is at most 3.
But is there any nuance? The triple condition we derived assumed that the three points are distinct and ordered. We used the fact that for three numbers x<y<z, we need y-x = z-y = power of two, because sum of two powers of two must be a power of two only if they are equal. That reasoning is correct: Since y-x = 2^i, z-y = 2^j, then z-x = 2^i+2^j. For that to be a power of two, 2^i+2^j must be a power of two. This occurs iff i=j (or one of them zero? But they are positive since differences positive, i,j >=0). Actually if i=j, sum = 2^(i+1) which is power of two. If i != j, sum has two bits set, thus not a power of two. So indeed must be equal. So triple condition holds.
Thus any three points must be equally spaced with step a power of two. That leads to the impossibility of 4 points: as soon as we have three smallest points equally spaced, the fourth must coincide with the third if we try to include it with the first two.
But wait, could there be a set of four points where not all triples are "ordered consecutively"? The condition applies to all triples, including non-consecutive ones. So even if we try to avoid the triple (a,b,d) by having a, b, d such that maybe a, b, d are not in increasing order? They are always sorted: a<b<d. So indeed triple (a,b,d) is present. So contradiction.
Therefore maximum possible size of a subset with all pairwise distances powers of two is at most 3. But is it always possible to achieve size 3? Not always; sometimes only 2 or 1. So answer m is either 1,2, or 3.
But wait, what about sets like {0, 2, 4, 6}? Already fails because 0-6 not power. So indeed no 4.
However, we need to be careful: Are there sets of size 3 that are not arithmetic progressions? As argued, for three points sorted, we need the two differences to be equal. So they must be arithmetic progression. So any valid triple is of the form {a, a+d, a+2d} with d a power of two. Also note that d can be any power of two: 1,2,4,... Also, what about negative coordinates? Since differences absolute, ordering works the same. So triple must be equally spaced with step power of two.
But is it possible to have triple where points are not in arithmetic progression but still all pairwise distances are powers of two? Let's check example: {0, 1, 3}? differences:1,3,2 -> 3 not power. {0, 2, 5}? 2,5,3 fails. So no.
Thus the problem reduces to: find the largest subset of points that is either:
-
Size 3: three points that are in arithmetic progression with common difference = power of two. i.e., there exist indices i,j,k such that after sorting, x_j - x_i = x_k - x_j = d where d is a power of two. And all three pairwise differences are powers of two (they are d, d, 2d which is also power of two because 2d=2^(k+1) if d=2^k). So any three points forming an arithmetic progression with step a power of two works.
-
Size 2: any pair with difference a power of two.
-
Size 1: any single point.
So we need to check if there exists a triple meeting the condition. If yes, answer is 3 (or maybe more? but we argued max 3). If not, check if there exists any pair with power-of-two difference. If yes, answer is 2. Else answer is 1 (any point).
But wait: Is it possible to have a set of size 3 that is not exactly arithmetic progression? What about three points where two are the same? No distinct points.
Thus algorithm: Given n distinct integers.
-
Sort the points? Possibly not necessary, but we can process.
-
For each point x, we can check if there exist y and z such that y = x - d, z = x + d, where d is a power of two, and both y and z exist in the set. That would give triple centered at x with step d. Also triples could be not centered (i.e., three points not necessarily symmetric around middle? But since they are arithmetic progression, they can be represented as {a, a+d, a+2d} for some a,d. That is either centered at a+d, or we can just check all possible a and d. But checking all triples O(n^2) is too large (n=2e5). So we need efficient detection.
We need to detect if there exists an arithmetic progression of length 3 with step = power of two.
We can iterate over all points as the middle element. For a candidate middle point m, we need to see if there exists a power-of-two d such that both m - d and m + d are in the set. Because if we have three numbers equally spaced, the middle is exactly the average of the other two. So we can test for each point m and for each power-of-two d (until some bound) whether both m-d and m+d are present. But d can be up to large values (since coordinates up to 1e9). However, the number of distinct powers of two up to 2e9 (since differences can be up to 2e9) is about 31 (2^0 to 2^30 ~ 1e9, 2^31 ~2.147e9 > 2e9). So at most 31 powers. So for each point, we can check at most 31 d's. That's O(31*n) = about 6.2e6, which is fine.
But careful: The triple might be oriented such that the middle point is not exactly the average? Actually arithmetic progression of three numbers has middle as average. So yes, for any valid triple, the middle point (by value) is exactly (a + (a+2d))/2 = a+d. So it's symmetric. So we can simply iterate over all points as possible middle, and for each power of two d, check if both x-d and x+d exist.
Edge case: Could there be a triple where the points are not symmetric because the middle is not the average? But in an arithmetic progression, they are symmetric: the middle is exactly the average. So yes.
Thus we can detect a triple in O(n log C) where C ~ 31.
We need to ensure that we handle negative coordinates: d positive integer. So x-d and x+d are well-defined integers.
We'll store the points in a hash set (unordered_set) for O(1) lookups.
If we find any such triple, we can output those three points.
If no triple, we need to find any pair with difference a power of two. That can be done by iterating through points and for each point, for each power d, check if x+d (or x-d) exists. Since we only need one pair, we can break early. Complexity O(n log C) as well.
If no pair, output any single point.
But there is nuance: Could there be a subset of size 3 that is not of the form symmetric around a middle? Actually all triples must be arithmetic progression; hence symmetric. So the method works.
But wait, are there any subsets of size 3 where the points are not equally spaced but still all pairwise differences are powers of two? Let's double-check the earlier proof. For points a<b<c, we require b-a = 2^i, c-b = 2^j, c-a = 2^k. We have 2^i + 2^j = 2^k. This equation implies that 2^i (1 + 2^{j-i}) = 2^k. If i<j, then 1+2^{j-i} is an odd number greater than 1, so not a power of two. Similarly if j<i. So must have i=j. So indeed b-a = c-b. So it is arithmetic progression. So correct.
Thus max size is at most 3.
But is there any possibility of size > 3 if the points are not distinct? No, they are distinct.
Thus problem solved.
However, there's a nuance: The condition says "the distance between each pair of points in a subset is an integral power of two." It doesn't say that the distances have to be the same power; they can be different. So for four points, maybe it's possible if some distances are 1, some 2, some 4, etc. But we already argued impossible due to triple constraints.
We should double-check with a potential counterexample: Consider points {0, 1, 4, 5}? Let's check all pairs: 0-1=1 ok;0-4=4 ok;0-5=5 no; fails. {0,2,4,6}? 0-6=6 fails. {0,2,4,8}? 2-8=6 fails. {1,3,5,7}? 1-7=6 fails. {0,8,16,24}? 8-24=16 ok? 0-24=24 fail. {0,4,6,10}? 0-10=10 fail. Could there be a set where all differences are powers of two? Let's attempt with small numbers programmatically in mind. But our logical derivation seems solid: from any three points we get equally spaced. So for four points, pick the smallest three: they must be equally spaced: a, a+d, a+2d. Now consider the largest point e. For triple (a, a+d, e): They must be equally spaced. This forces e = (a+d) + ( (a+d) - a ) = a+2d. So e = a+2d = the third point, contradiction. Therefore e cannot be distinct from the third point. So no four distinct points. So indeed impossible.
Thus answer max size is 3.
Now we need to produce any maximum subset. So we should first attempt to find any triple of the form (x-d, x, x+d) with d power of two. If found, output these three. If not, find any pair with difference power of two. If not, output any single point.
But caution: Is it possible that there exists a triple but not of that symmetric form? No, we already argued they must be symmetric. So detection method is correct.
One more nuance: Could there be a triple where the middle is not present? Actually we need three points in the set. For triple we need three points. The symmetric representation uses the middle point. So it's fine.
Implementation details:
- Read n and list x.
- Build unordered_set or set. But unordered_set may be faster. Since coordinates up to 1e9, but n up to 2e5, fine.
- We need to generate powers of two up to max difference. The maximum possible absolute coordinate is 1e9, so difference up to 2e9. Powers of two up to 2^31 = 2147483648 > 2e9, so include 2^30 = 1073741824, 2^31 maybe not needed because difference cannot exceed 2e9? Actually if coordinates are -1e9 and 1e9, difference 2e9. 2^31=2,147,483,648 > 2e9, so we might still need to consider 2^30? 2^30=1,073,741,824; 2^31 is larger than 2e9, but could there be a pair with difference exactly 2^31? Since max difference 2e9 < 2^31, it can't be. So we only need powers up to <= max possible difference. But we can just generate all powers from 2^0 to 2^30 inclusive (since 2^30 ~1.07e9) and also check 2^31? Actually 2^31 = 2,147,483,648 > 2e9, so any difference equal to that is impossible because coordinates range [-1e9,1e9] => max diff = 2e9 < 2^31. So we can limit to powers <= 2e9. The next power after 2^30 is 2^31 > 2e9, so we only need up to 2^30. But is 2^30 itself within possible diff? Yes, if coordinates -1e9 and 1e9-2^30? Actually 2^30 = 1073741824, which is > 1e9? 1e9 = 1000000000, so 1073741824 > 1e9, but difference could be up to 2e9=2000000000, so 2^30 is possible, e.g., -500 million and +573 million. So include 2^30. So maximum exponent = 30 (since 2^30 = 1073741824, < 2e9). Actually 2^31 is 2147483648 > 2e9, so not needed. But we might also consider 2^0 =1. So exponents 0..30 inclusive, total 31.
But careful: Could difference be exactly 2^30? Yes if the coordinates are such that |x-y|=2^30. Since max coordinate magnitude is 1e9, which is less than 2^30? Actually 2^30 = 1,073,741,824 > 1e9. So if both coordinates are within [-1e9, 1e9], the maximum absolute value is 1e9, so the maximum possible difference is at most 2e9. 2^30 is about 1.07e9, so it's within range. So fine.
Now, for each point x in the list (or for each middle candidate), we check each power p in powers. For each p, if set contains x-p and x+p, we have a triple. But need to ensure that x-p and x+p are distinct from x and each other? They are distinct because p>0. So okay.
We can output the three points: x-p, x, x+p.
If we find such a triple, we are done.
If after scanning all points and all powers, no triple, we look for any pair.
For pair detection: For each point x, and for each power p, check if x+p exists (or x-p, but to avoid duplicates, we can check x+p only). If found, output the two points (x and x+p). Note that we may find the same pair twice, but we can break after first.
If no pair, output any single point (e.g., the first in input).
But there's a subtlety: Could there be a triple that is not centered? Already argued no.
But what about triples where the points are not equally spaced because the middle point is not exactly the average? That cannot happen. So fine.
Now, is there any possibility of a subset of size 3 that does not have the middle point as one of the given points? Actually in any triple of distinct numbers, the middle (by value) is one of the points. So yes.
Thus algorithm seems straightforward.
But wait: The problem asks to output the subset with maximum possible size. Could there be a case where we can have a subset of size 3 but also we could have size 2 or 1, obviously. But we need max size, so we output a triple if exists.
However, we must ensure that indeed the maximum size cannot exceed 3. We argued. So answer m is 1,2, or 3.
Edge Cases:
-
n=1: answer 1, that point.
-
n=2: check if difference is power of two. If yes, answer 2; else answer 1.
-
n>=3: attempt to find triple. If not, then find pair if exists, else 1.
But is there any possibility of having a subset of size 3 that is not arithmetic progression? Suppose we have points a, b, c with a < b < c, we derived that b-a must equal c-b. So it is arithmetic. So no.
Double-check condition: The distances must be an integral power of two. That includes 2^0=1. So yes.
Now we must also consider the possibility of having three points that are not equally spaced but still all pairwise differences are powers of two if we allow negative exponents? No, integer power means non-negative integer exponent as per problem: "integral power of two" and "d is some non-negative integer number". So only non-negative.
Thus solved.
But wait: Are there sets of size 4 where all pairwise distances are powers of two but some points coincide? No, distinct.
Let's attempt to see if our impossibility proof for size 4 could be bypassed if the four points are not all required to satisfy triple condition for all triples? But condition is for each pair, not each triple? Wait: "the distance between each pair of points in a subset is an integral power of two." That means for any two points, their distance is power of two. That automatically implies that for any three points, all three pairwise distances must be powers of two. So triple condition holds. So our argument stands.
Thus maximum size = 3.
Now implementation:
We'll read n, vector arr(n).
We'll create unordered_set s(arr.begin(), arr.end()).
We'll generate list powers: for i in 0..30: 1<<i.
Now find triple:
for each x in arr:
for each p in powers:
// Check both exist. But careful: if x-p or x+p may be out of range? Not necessary to check bounds because set will not contain them if out of range. But computing x-p and x+p may overflow? x can be up to 1e9, p up to 2^30=~1.07e9, so x-p could be as low as -2.07e9, which is within 32-bit signed int? 1e9 - (-1e9) = 2e9, but x-p could be -2.07e9 if x=-1e9 and p=1.07e9 => -2.07e9 which is less than -2e9? Actually coordinates limited to -1e9..1e9, but x is from that range, p up to 1.07e9, so x-p could be as low as -1e9 - 1.07e9 = -2.07e9, which is less than -2e9. However, set only contains numbers within [-1e9,1e9]. So if x-p is less than -1e9, it cannot be in the set, so we can skip check? But we could still compute and the integer will be outside possible range but still an integer, and we would look it up in set; set will return false. However, risk of integer overflow? In C++, int is typically 32-bit signed, range about -2.147e9 to 2.147e9. x-p for x = -1e9 and p=1.07e9 yields -2.07e9, which is within int range (since INT_MIN is -2147483648, -2070000000 > -2147483648). So it's safe. But p can be up to 2^30 = 1073741824, plus x = -1000000000 gives -2073741824, still > INT_MIN (-2147483648). So safe. However, if we had p=2^31 (not using), would cause overflow. So fine.
Thus we can just compute.
But we need to ensure we don't double-count the same triple from different middles? Not needed, just find any.
Now if we find triple, output size 3 and those three numbers.
If not, find pair:
for each x in arr:
for each p in powers:
if s.count(x+p) > 0:
output 2 and x, x+p. break out of loops.
// Don't need to check x-p because eventually it will be seen when the other point is considered as x.
But careful: if we only check x+p, we may miss a pair where the smaller point's x+p exists, but for larger point we might not check because its x+p would be even larger and might be out of set? Actually if we have two points a<b such that b-a = p, then for x=a, we check x+p = b, so we catch it. So checking only x+p for each x suffices.
Need to ensure we don't output the same point twice? Distinct.
If we find none, output size 1 and any point, e.g., arr[0].
Complexities: O(n * 31) ~ 6.2e6, which is fine.
But note: For triple detection we might also need to avoid considering the case where x-p == x or x+p == x (p>0, so not). So okay.
Now we need to ensure we output coordinates in any order, but the example outputs "7 3 5" which is not sorted. That's fine.
Edge Cases:
-
Could there be a triple that is not symmetric because the middle point is not exactly the arithmetic mean? Already argued impossible.
-
However, consider points: {0, 2, 4, 6}? Triple {0,2,4} works? Check: 0-2=2, 0-4=4, 2-4=2. Yes that triple works. So if we have four points, the triple subset exists. But the set of four points itself is not valid. So we could have answer 3 from a subset of three points, even if there are more points. So our algorithm correctly finds a triple if any three points satisfy the condition.
Now, is it possible that there is a triple but not of the form (x-d, x, x+d) because the middle point might not be the one in the set? Actually if we have three points a, a+d, a+2d, then the middle point is a+d, which is in the set. So it fits.
Thus algorithm is correct.
But before finalizing, we should test on some cases.
Test 1: Input: 6, 3 5 4 7 10 12. Let's see if our algorithm finds a triple. We'll check each point as middle. Let's compute powers: 1,2,4,8,16,... For point 5: check 5-1=4 and 5+1=6? 6 not in set; 5-2=3 and 5+2=7 -> both 3 and 7 are in set? 3 yes, 7 yes. So triple {3,5,7} works. So answer 3: output could be 3,5,7. The sample output is 7 3 5, which is same set. Good.
Test 2: Input: -1 2 5 8 11. Check triples: For middle candidate 2: check 1 and 3? 2-1=1 not in set; 2-2=0 not; 2-4=-2 not; 2-8=-6 not; etc. For 5: 5-1=4 not; 5-2=3 not; 5-4=1 not; 5-8=-3 not; 5-16=-11 not. For 8: 8-1=7 not; 8-2=6 not; 8-4=4 not; 8-8=0 not; 8-16=-8? -8 not; etc. 11: similar. So no triple. Check pairs: any pair difference power of two? -1 and 7? not. -1 and 1? not. -1 and 3? not. 2 and 6? not. 2 and 4? not. 5 and 7? not. 5 and 9? not. 8 and 7? not. 8 and 10? not. 11 and 9? not. Probably none. So answer 1, sample output 8 (any single). Good.
Test 3: Suppose points: 0,1,2. Triples: middle 1: 0 and 2 exist. So answer 3.
Test 4: Points: 0,3,6. Differences: 3 and 6 and 3. 3 is not power of two. So no triple. But check pairs: 0-3=3 not; 0-6=6 not; 3-6=3 not. So answer 1. Could there be size 2? No. So output 1.
Test 5: Points: 0,2,5,7? Triples: 0,2,4? 4 not present. 2,5,8? not. 0,5,10? not. So no triple. Pairs: 0-2=2 yes. So answer 2: {0,2}. Good.
Thus algorithm works.
But wait: Could there be a situation where there is a triple but our algorithm fails to detect it because we only check for the middle point? For a triple {a, a+d, a+2d}, the middle is a+d, and we check for each point as middle. So it will be detected when we process that middle point and d. However, what if we consider a triple that is not equally spaced? Not possible. So fine.
Now confirm that the condition for triple detection: we check both x-d and x+d exist. However, note that there might be a triple where the middle is not exactly the middle? Actually no. So okay.
Now, let's consider potential pitfalls: The unordered_set lookup for numbers that are not present is fine. But we should be careful with integer overflow when computing x+p if we use signed int and p up to 2^30 and x up to 1e9, sum up to ~2.07e9 < INT_MAX (2,147,483,647). So safe.
But we could also use long long to be safe, but int should be fine in C++ with 32-bit int? 1e9 + 2^30 = 1e9 + 1073741824 = 2073741824 < 2147483647. So within signed int. However, if x = 1e9 and p=1073741824, sum = 2073741824 < 2.147e9. So safe. If x = -1e9, x-p = -2073741824 > -2147483648. So safe. So int works.
But to be extra safe, use long long, but it's okay.
Now, we need to ensure we break out of loops once we find answer, to avoid unnecessary work.
But there is one nuance: The subset we output for triple might not be valid if the three points are not distinct? They are distinct because p>0. So fine.
Now, we need to output m and then m integers (coordinates). The order does not matter.
Now, is it possible that the maximum subset is 2 even if a triple exists? No, triple gives larger size, so we always prefer triple.
Now, think about the proof that maximum size is 3. That's key. Let's ensure rigorousness: For any set S of points with the property that all pairwise distances are powers of two. Take any four distinct points. Order them: a<b<c<d. Consider triple (a,b,c): must satisfy b-a = c-b = d1 (power). Consider triple (a,b,d): must satisfy b-a = d-b => d = 2b-a = a+2d1. Then d = a+2d1. But c = a+2d1, so c=d, contradiction. Hence no four distinct points. So maximum size ≤3.
Thus answer is 1,2,3.
Now, it's possible that there exists a set of three points where the step d is not a power of two but still all pairwise distances are powers of two? As argued, no.
Therefore, algorithm stands.
But wait: Could there be a triple where the three points are not equally spaced because one of the differences is zero? No, distinct points.
Thus done.
Now, but maybe there is a case where three points are such that a<b<c, and b-a = 2^i, c-b = 2^j, c-a = 2^k. If i=j, then step equal. But what if i and j are not equal but 2^i+2^j still a power of two? That would require 2^i(1+2^{j-i}) is power of two. Since (1+2^{j-i}) is odd >1, the only way the product is power of two is if the odd factor is 1. But it's >1. So impossible. So must i=j.
Thus step equal.
Now, we can implement.
Potential edge: For large n (200k), unordered_set with int may have collisions but fine.
Now, check if there is any scenario where we can have a subset of size 3 that is not captured by the middle-check because the middle point might not be present? Actually if we have three points a, b, c, the middle by value is always one of them. So it's captured.
But consider a triple like {0, 1, 2} - middle 1. Check for point 1: p=1 gives 0 and 2 -> works.
Now, what about triple where the points are {0, 4, 6}? Check: Differences: 4,2,6? 0-4=4 OK;0-6=6 not power of two? 6 is not power. So invalid.
Thus fine.
Now, regarding performance: For each point we iterate over 31 powers. That's 6.2M lookups, which is fine. But we can also optimize by breaking early after finding triple. However, we need to ensure we don't miss any triple due to checking only one orientation? Not needed.
One more nuance: The triple detection algorithm might also find a triple where the three points are not distinct? For p>0, they are distinct.
Now, we should output any subset with maximum size. So for triple we output the found triple.
But what if there are multiple triples? We output any.
Now, is there any scenario where the maximum size is 2 but we still have a triple? That can't happen because triple is larger.
Thus implementation steps:
- Read n.
- Read vector a(n).
- Build unordered_set s(a.begin(), a.end()).
- Precompute powers: vector pow2; for i=0..30: pow2.push_back(1<<i).
- For each x in a:
for p in pow2:
if s.find(x-p) != s.end() && s.find(x+p) != s.end():
// Found triple.
Print 3 and x-p, x, x+p. Return.
- If not found triple:
For each x in a:
for p in pow2:
if s.find(x+p) != s.end():
// Found pair
Print 2 and x, x+p. Return.
- If no pair:
Print 1 and a[0] (or any). Return.
But need to be careful: In pair detection, if we find a pair, we must ensure that the two points are distinct. x+p != x because p>0.
Now, potential bug: When checking for triple, we might consider p such that x-p == x+p? That would require p=0, but p>0, so no.
Now, check for overflow: p up to 2^30, x up to 1e9, sum and difference within int range as argued.
Now, we must be cautious about using unordered_set with negative numbers; it's fine.
Now, but is there any scenario where we might have a triple but the middle point is not the one that yields both neighbors because the step d might be a power of two but the points are a, a+d, a+2d. For middle point a+d, the step d is a power of two. So it will be found.
However, what if the triple is not exactly centered because the middle point is not exactly the midpoint? Actually for any three numbers in arithmetic progression, the middle is the average. So it is centered.
Thus all good.
Now, potential alternative: Could there be a subset of size 3 that is not arithmetic progression but still all pairwise distances are powers of two? Let's double-check with some examples: Suppose numbers 0, 2, 3? Differences: 2,3,1 -> 3 not power. 0,1,4? 1,4,3 -> 3 not. 0,4,5? 4,5,1 -> 5 not. 0,8,9? 8,9,1 -> 9 not. 0,4,8? arithmetic. 1,2,3? arithmetic with d=1. So indeed.
Thus final answer.
But wait, what about the possibility of three points with negative coordinates? Same logic.
Now, consider if there are multiple points with same coordinate? Input says pairwise distinct, so no duplicates.
Now, let's consider complexity: O(n * 31) ~ 6 million, which is fine.
We could also reduce to O(n log n) by sorting and scanning, but not needed.
Now, is there any case where the maximum subset size could be 0? Not, n>=1.
Now, we should test additional cases:
Case: n=200000, all points random. Likely no triple, maybe some pair. Works.
Now, potential corner: Could we have a triple where the step d is not a power of two, but the distances are powers of two because the differences are 2^i, 2^j, 2^k with i=j? Actually if i=j, then d = 2^i which is power of two. So step is power of two.
Thus all good.
Now, let's double-check the proof for size 4 impossibility: we used triple (a,b,d) but what if the points are not such that (a,b,d) are the three smallest? Actually a is smallest, b is second smallest, d is largest. Yes.
But what if there are four points and they are not all in increasing order? They always have an order. So the argument holds.
Now, consider four points where some points have the same coordinate? No, distinct.
Thus maximum size is 3.
Now, but there is a nuance: Could there be a set of three points that are not an arithmetic progression but still satisfy because one of the distances is 0? No, distinct.
Thus done.
Now, the problem might be from Codeforces. I recall a problem "Maximum Subset with pairwise distances powers of two" where the answer is at most 3. Yes, I think I've seen this: "Make It Good" maybe not. Actually I recall problem "XOR Guessing" no. There is a problem "Little Pony and Harmony Chest"? Not. But I've seen a problem: "Points and Powers of Two" maybe. Anyway.
Now, we must produce code.
But before finalizing, consider whether there could be a subset of size 3 that is not captured by our detection because we check all powers up to 2^30. What if the step d is larger than 2^30? Since coordinates are within [-1e9,1e9], the maximum possible step for a triple is such that a+2d <= 1e9 and a >= -1e9. So d <= (1e9 - (-1e9))/2 = 1e9. So d <= 1e9. 2^30 = 1,073,741,824 > 1e9. So d can be at most 1e9. The next smaller power of two less than or equal to 1e9 is 2^29 = 536,870,912, or 2^30 is 1,073,741,824 > 1e9, so d cannot be 2^30? Actually d could be 2^30 if a is negative enough: a+2d ≤ 1e9. Suppose a = -1e9, d = 2^30 = 1,073,741,824, then a+2d = -1e9 + 2,147,483,648 = 1,147,483,648 > 1e9, which exceeds max coordinate. So not allowed because all coordinates must be within [-1e9,1e9]. So d cannot be as large as 2^30? Could be if a is smaller than -1e9? But coordinates limited. So maximum d is such that a+2d ≤ 1e9 and a ≥ -1e9. So the maximum possible d is (1e9 - (-1e9))/2 = 1e9. So d max = 1,000,000,000. This is less than 2^30 (1,073,741,824) but greater than 2^29 (536,870,912). So there is a potential d that is a power of two, 2^30 is >1e9, so not possible; 2^29 is possible. But could there be a power of two d=2^29=536,870,912. That is within range. So we need to include 2^29? Already included. Actually our list includes up to 2^30. But if we include 2^30, we may still check it. That's fine; we may do unnecessary checks.
But is there a chance that a valid triple has step d=2^30? As argued, it would require a+2d ≤ 1e9, impossible because a >= -1e9, 2d=2,147,483,648, so a+2d >= 1,147,483,648 > 1e9. So not possible. So we could limit to powers up to 2^29. But including 2^30 doesn't harm; it just never yields a valid triple because if x is a coordinate, x-p and x+p must be within the coordinate range, so they will not be in set if p too large. So we may waste some lookups. 31 vs 30 powers, negligible.
Thus fine.
Now, let's think about the possibility of negative powers? No.
Now, confirm that the condition "integral power of two" includes 2^0 = 1. So d=1 is allowed. Good.
Now, final step: Write code.
But wait: The problem might have twist: Could there be a subset of size 3 that is not arithmetic progression if we allow distances like 2^0=1, 2^1=2, etc. We already proved impossible. So it's correct.
Now, before finalizing, I'll attempt to prove more formally: For any three numbers x<y<z with y-x=2^a, z-y=2^b, we need z-x=2^c. Then 2^a+2^b=2^c. WLOG a≤b. Then factor 2^a(1+2^{b-a})=2^c => 1+2^{b-a} = 2^{c-a}. Since left side is odd and >1 unless b=a, then the right side is a power of two, which is even unless exponent 0 => 2^0=1, but left side would be 2 if b=a? Actually if b=a, then 1+2^0? Wait if b=a, then 1+2^{0}=1+1=2, which is a power of two (2^1). So works. If b>a, then left side is 1 + even = odd >1, and any power of two greater than 1 is even, contradiction. So indeed a=b.
Thus triple must be arithmetic progression with step a power of two.
Now, we've reasoned about size 4.
Thus solution is easy.
Now, implement.
Potential pitfalls:
-
When checking for triple, we must ensure that x-p and x+p are in the set. But what if x-p equals x+p? That would require p=0, not.
-
We should be careful not to use the same point multiple times? For triple, all three are distinct. Good.
-
For pair detection, we must not output a pair where both are same; impossible.
Now, test on more examples.
Example: n=3, points = 0,3,6. Our algorithm: For each middle: 3? check p=1 -> 2 and4 not; p=2->1 and5 not; p=4-> -1 and7 not; ... no triple. Then pair detection: For each x: 0 check p=1->1 not; p=2->2 not; p=4->4 not; ... 3 check p=1->4 not; p=2->5 not; ... 6 check p=1->7 not; ... none. So output 1. That's correct.
Example: n=3, points=1,2,4. Check middle 2: p=1 -> 1 and3? 3 not; p=2 -> 0 and4? 0 not; p=4 -> -2 and6 not; So no triple? Wait, but is {1,2,4} valid? Differences: 1-2=1 ok;1-4=3 not;2-4=2 ok; but 1-4=3 not power, so invalid. So no triple. Good.
Example: n=3, points=0,2,4. Middle 2: p=2 -> 0 and4 exist -> triple. Output {0,2,4}. Good.
Now, another test: n=4, points=0,1,2,4. Here triple {0,1,2} exists. Our algorithm will detect when middle=1, p=1 gives 0 and2 -> triple, output three. That's correct max size is 3.
Now, test: n=2, points=0,2. Pair detection: for 0, p=2 -> 2 exists -> output {0,2}. Good.
Now, test: n=2, points=0,3. No pair, output 1.
Now, test with negative: points = -5, -3, -1. Sorted: -5, -3, -1. Differences: 2,2,4 all powers. Middle -3: p=2 -> -5 and -1 exist. Output triple.
Now, test with mixed: -1, 1, 3? Differences: 2,4,2 -> triple? -1,1,3: step=2, works. Middle 1: p=2 -> -1 and 3 exist.
Now, test with larger d: -1000000000, -1000000000+536870912, -1000000000+2*536870912 = -1000000000, -463129088, 73670912? Actually compute: -1e9 + 536,870,912 = -463,129,088; plus another 536,870,912 = 73,741,824. That's within range. So triple exists if all three coordinates are present. So algorithm checks p=536870912 (2^29). For middle point -463,129,088, check x-p = -1e9, x+p = 73,741,824; both must be in set. So will find if present.
Now, check p=2^30=1073741824: For middle point maybe 0? then x-p=-1073741824 which is less than -1e9, not in set; x+p=1073741824 >1e9, not in set. So no false positives.
Thus algorithm works.
But wait: Could there be a triple with step d that is not a power of two but still all pairwise differences are powers of two? We argued no because from the three numbers the step must be power of two. However, what if the three points are not in arithmetic progression but we have something like a=0, b=2, c=2? Not distinct. So no.
Now, we need to output any maximum subset. So we can output the triple we find.
But is there any possibility that the maximum subset is 3 but there is no triple of the form (x-d, x, x+d) because the three points are not symmetric? As argued, they are always symmetric because they are equally spaced. So they can be represented as (a, a+d, a+2d). That is symmetric with middle a+d. So our detection method will catch it.
But wait: For three numbers that are equally spaced, the middle is (a + (a+2d))/2 = a+d. So yes. So detection works.
One more nuance: The condition requires distances to be an integral power of two. That includes 2^0=1. So for triple like {0,1,2}, d=1 works.
Now, let's think about if there could be a subset of size 3 where the points are not sorted? That doesn't matter.
Thus final solution is straightforward.
However, I recall a Codeforces problem "1092C" or something? Not. Actually problem "1646B" maybe. But I think the problem is "D. Maximum Subset with pairwise distances powers of two" or something like that. And the solution indeed is at most 3.
But I want to double-check if there could be a set of size 4 where the points are not all distinct? Not allowed.
What about the possibility of using 0 as a power? 2^0=1, so only positive distances.
Now, is there any possibility of four points where the pairwise distances are all powers of two but not all triples are equally spaced? But any triple from those four must satisfy the condition, so they must be equally spaced. So our argument still holds.
Thus answer is 3 max.
Now, implement.
But we must be cautious about using unordered_set with large n. It's fine.
Now, we also need to handle case where we find triple but the triple might have points that are not in the original order? We output coordinates; we can output x-p, x, x+p.
But note: x-p and x+p may not be in the input order, but they exist in set, so it's fine.
Now, test with example where there are multiple triples, but we break at first.
Now, I'll produce final code in C++.
Potential improvements: Could generate powers as long long to avoid overflow, but int works.
Implementation plan:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i=0; i<n; i++) cin >> a[i];
unordered_set<int> s(a.begin(), a.end());
vector<int> pow2;
for (int i=0; i<=30; i++) pow2.push_back(1 << i);
// search for triple
for (int x : a) {
for (int p : pow2) {
if (s.count(x-p) && s.count(x+p)) {
cout << 3 << endl;
cout << x-p << " " << x << " " << x+p << endl;
return 0;
}
}
}
// search for pair
for (int x : a) {
for (int p : pow2) {
if (s.count(x+p)) {
cout << 2 << endl;
cout << x << " " << x+p << endl;
return 0;
}
}
}
// only single
cout << 1 << endl;
cout << a[0] << endl;
return 0;
}
But note: In the triple detection, we might want to break out of loops after finding. We can use a flag or just return.
Now, is there any risk that we consider the same point as middle but the other two points are same? No.
Now, potential issue: In pair detection, we might detect a pair that is actually the same as a triple? Not needed.
Now, test with input:
6
3 5 4 7 10 12
Our algorithm: unordered_set includes all. Iterate over points. Suppose order is 3,5,4,7,10,12. For 3: check p=1: 2 and4? 2 not,4 exists but need both sides; p=2:1 and5?1 not; p=4:-1 and7? -1 not; p=8: -5 and11? no; ... For 5: p=1:4 and6?6 not; p=2:3 and7 -> both 3 and7 exist! So output 3 5 7? Actually we output x-p, x, x+p: 3,5,7. But sample output is "7 3 5". That's fine. So works.
Now, second example: -1 2 5 8 11. No triple, then pair detection: For -1: p=1 -> 0 not; p=2 -> 1 not; p=4 -> 3 not; p=8 -> 7 not; p=16 -> 15 not; etc. For 2: p=1->3 not; p=2->4 not; p=4->6 not; p=8->10 not; p=16->18 not; 5: p=1->6 not; p=2->7 not; p=4->9 not; p=8->13 not; p=16->21 not; 8: p=1->9 not; p=2->10 not; p=4->12 not; p=8->16 not; p=16->24 not; 11: p=1->12 not; p=2->13 not; p=4->15 not; p=8->19 not; p=16->27 not; So no pair. Then output 1 and first point -1? But sample output is "1\n8". Actually sample output is 1 and 8, not necessarily first. But any single point is allowed. So our algorithm would output -1 (or first point). That's also acceptable. The problem says "If there are multiple answers, print any of them." So outputting -1 is valid. So no problem.
Now, check a case where there are multiple pairs and we output the first found; that's fine.
Now, potential edge: If there is a pair with difference power of two, but our pair detection might output a pair where the larger point is x and the smaller is x+p? Actually if we check x+p, we output x and x+p. That's fine.
Now, is there any scenario where we might output a pair where x and x+p are both present but x+p might be same as x? p>0 ensures distinct.
Now, the triple detection: Could there be a case where we detect a triple but the three points are not distinct because p=0? Not included.
Now, I'll consider if there is any oversight: The condition says "distance between each pair of points in a subset is an integral power of two." That includes the possibility of 0? Distance 0 would mean same point, but points are distinct, so not relevant.
Now, check if there is any scenario where we can have a subset of size 3 that does not have the middle point as one of the points? No.
Now, let's confirm that for any valid triple (a, b, c) sorted, the step d = b-a = c-b is a power of two. That is necessary and sufficient. So algorithm correctly identifies all triples.
Now, what about the case where there are many points and we find a triple quickly? Good.
Now, time complexity: O(n * 31) ~ 6.2e6 operations, each operation is hash set lookup O(1) average, so fast.
Memory: O(n).
Now, one more nuance: The input coordinates are pairwise distinct, but not necessarily sorted. Our unordered_set will contain them.
Now, but there is a subtle case: When checking triple, we might find that x-p and x+p exist, but are they the same as x? p>0 so no.
Now, is there any possibility that the maximum subset is 3 but we might not detect it because the middle point is not in the set? That's impossible because the middle point is one of the three points.
Thus done.
But wait, what about sets of size 3 that are not symmetric because the middle point is not exactly the average? That can't happen as we proved.
Thus final answer.
But let's try to think if there is any trick: Could there be a set of size 4 where the points are such that no triple (a,b,d) forces contradiction because the three points we pick might not be the smallest three? Our proof used the four points sorted a<b<c<d, and considered the triple (a,b,c) and (a,b,d). Both are valid triples because all pairwise distances are powers of two. The triple (a,b,c) yields b-a = c-b = d1. The triple (a,b,d) yields b-a = d-b => d = 2b - a = a + 2(b-a) = a+2d1. Thus d = a+2d1. But since c = a+2d1, we have c=d, contradiction. So no four points.
Thus it's solid.
Now, consider the possibility that there might be a subset of size 3 that is not arithmetic progression if we consider distances that are powers of two but we could have a+b=c? Wait, we already examined.
Thus solution is correct.
Now, I'll produce final answer.
But let's also consider if the answer might be 0? No.
Now, possible pitfalls: In triple detection, we might find a triple where x-p and x+p are both present, but the three points may not satisfy the condition if we misinterpret? Actually they do: differences: (x)-(x-p)=p, (x+p)-(x)=p, (x+p)-(x-p)=2p. Since p is power of two, 2p is also power of two. So valid.
Thus fine.
Now, final code.
But maybe we should use long long to avoid overflow? Let's be safe with long long for x and p. However, set is of ints; if we use long long for computations, we need to cast back to int? But x-p and x+p may exceed int range? As we argued, they are within possible range for lookups; but if we use long long, the values may be outside the int range, but set contains ints, so we can check if the computed value fits within int range before lookup. But we can avoid complexity by just using long long for calculations and then checking if the value is within [-1e9, 1e9] or simply rely on the fact that if it's out of that range, it won't be in the set. However, if we do s.count((long long)value) but s is unordered_set, then there is type mismatch. We could convert to int, but if value is out of int range, conversion could overflow and result in an int that might coincidentally match an element? That would be extremely unlikely and not correct because the actual coordinate is not that value. So to be safe, we could use long long for computations and then check if the value fits in the range of int (since coordinates are within -1e9..1e9, they are within int). So we can do: if (x-p >= -1e9 && x-p <= 1e9) ... But is it necessary? Let's compute max absolute value: x max 1e9, p max 2^30=1073741824, so x-p min = -1e9 - 1073741824 = -2073741824, which is less than -2e9, but still within int range (since int min is -2147483648). So conversion to int would produce some negative number, but that number may not be in the set because the set contains only numbers in the input range. However, if we convert -2073741824 to int, it is valid int. But the set does not contain that number (since inputs are within [-1e9,1e9]), so s.count((int)(x-p)) will return false. That's okay. The problem is that we might have x-p that is a valid int but not in the set, so it's fine. There is no risk of undefined behavior because converting a long long that fits within int range to int is safe if it's within range, but if it's outside int range, the conversion is implementation-defined (usually wraps). But since we are on typical 2's complement 32-bit int, conversion of out-of-range value yields some int, but we don't rely on that value being correct because we only care whether it exists in set; it likely will not match any element because the set contains only numbers within input range, which are all within [-1e9, 1e9]. The converted out-of-range number might coincidentally equal some input if wrap-around occurs? For example, suppose input contains 0, and we compute x-p that is 2^32, which wraps to 0? That would be a false positive. But could that happen? x and p are within reasonable bounds; the maximum absolute we have is about 2.07e9, which is less than 2^31 (2.147e9). So all computed values are within [-2.07e9, 2.07e9]. That's within the range of signed 32-bit int (-2,147,483,648 to 2,147,483,647). So all computed values actually fit in int! Because -2.07e9 > -2.147e9, and 2.07e9 < 2.147e9. So there is no overflow beyond 32-bit signed int. Indeed, the extreme: x = -1e9, p = 1073741824 gives -2073741824, which is > INT_MIN (-2147483648). So fits. x = 1e9, p = 1073741824 gives 2073741824, which < INT_MAX 2147483647. So all fits. So no overflow. So using int is safe.
Thus we can keep int.
Now, finalize.
But wait, what about p = 2^30 = 1073741824, x = 1000000000, sum = 2073741824 < INT_MAX, good.
Thus fine.
Now, consider n up to 200k, we might want to use fast I/O. Use ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
Now, code as above.
Now, let's double-check the problem statement: "the distance between each pair of points in a subset is an integral power of two." That means for every pair (i,j) in the subset, |x_i - x_j| = 2^d for some non-negative integer d (could be different per pair). So our reasoning is correct.
Now, is there any possibility of subset of size 3 where the three points are not collinear? They are on a coordinate line, so collinear trivially. So no.
Thus answer is ready.
Now, we might consider that the answer could be 3 even if there are multiple such triples, but we only need one.
Now, maybe we should output the triple in sorted order? Not required.
Now, produce final answer.
But I'll think about potential alternative scenario: Could there be a subset of size 3 that is not arithmetic but still all differences powers of two if we consider that the differences might be 1, 1, 2? That's arithmetic: {a, a+1, a+2}. So it's arithmetic.
What about {0, 2, 3}? 0-2=2,0-3=3,2-3=1 => 3 not power. So no.
Thus fine.
Now, there is a nuance: The problem says "Note that any subset containing one element satisfies the condition above." So single always works.
Now, confirm that we are not missing any triple where the step d is power of two but we might not detect because we only check for each point as middle. But what if the triple is {a, a+d, a+2d} and we process the point a (the smallest) and check for some p? Could we detect it with a as middle? a is not the middle; we need middle. So we must iterate over all points, including the middle. Since we iterate over all points, we will eventually process the middle.
Now, what if the triple is {a, b, c} where b is not exactly the middle? But in any triple of three distinct numbers, there is a unique middle value. So that point is present. So our loop includes it.
Thus fine.
Now, is there a chance that we might detect a false triple due to integer overflow causing x-p to equal x+p? Not possible.
Thus final.
Now, but wait: There is a nuance: The problem statement says "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 doesn't explicitly say that the exponent must be integer, but "integral power of two" suggests integer exponent. And they clarified "d is some non-negative integer number". So yes.
Now, we can produce solution.
But before finalizing, let's examine if there is any possibility of a larger set using 0? Not.
Now, let's see if there is any situation where we could have a set of size 4 where points are not all distinct? No.
Now, might there be a set of size 4 with property if we allow d to be negative exponent? No.
Thus final.
Now, double-check the problem constraints: n up to 2e5, coordinates distinct, in range [-1e9,1e9].
Thus our algorithm runs in O(n * log(MAX)) ~ 6.2e6, which is fine.
Now, consider using unordered_set with many elements; potential collisions could degrade performance but still likely okay for 2e5. Alternatively we could use a sorted vector and binary search; but unordered_set is simpler.
But be careful: unordered_set may be slow due to hash collisions, but for 2e5 it's fine.
Alternatively, we could use set (tree) with O(log n) lookups, resulting in O(n * 31 * log n) ~ 6.2e6 * log2(2e5) ~ 6.2e6 * 18 = 111 million, which might be borderline but probably still okay in C++? 111 million operations might be okay within 2 seconds? Possibly borderline. So better use unordered_set for O(1). But ensure to call reserve and maybe use custom hash? But default int hash is fine.
We'll use unordered_set.
Now, final code.
But consider one more nuance: The triple detection might incorrectly detect a triple when x-p and x+p exist but one of them equals x? Not possible.
Now, test with negative numbers: Example: points = -5, -3, -1. Middle -3: p=2 gives -5 and -1. Both exist, output triple. Good.
Now, ensure that we output coordinates as integers; may need to separate by spaces.
Now, let's also check if there could be a triple with step d = 0? Not.
Now, we can finalize.
But wait: The problem asks "Among all these subsets, choose a subset with maximum possible size." So we need maximum size. Our algorithm chooses size 3 if exists else 2 if exists else 1. That yields maximum.
Now, what about possibility of size 3 where we have points like {0, 4, 6}? Not valid.
Thus fine.
Now, produce final answer.
One more thought: Could there be a set of size 3 where the step d is not a power of two but the distances are powers of two? Suppose d = 3, then differences: 3,3,6. 3 is not power of two, so fails.
Thus done.
Now, final answer.
But before concluding, I'll search for any tricky test case that might break the "max 3" argument. For instance, consider points: 0, 2, 4, 6. As we argued, triple {0,2,4} works, but set of four fails. So max is 3.
Now, consider points: 0, 1, 4, 5. Check triple {0,1,2}? 2 not present. {0,4,8}?8 not. {1,4,7}?7 not. So no triple? But {0,4,5}? differences:4,5,1 -> 5 not. So no triple. Pairs: 0-1=1 yes. So max=2. So algorithm would find pair (0,1). That's fine.
Now, consider points: 0, 2, 4, 8. Triples: {0,2,4} works. So algorithm finds triple, max=3. The set of four points doesn't satisfy all pairs (e.g., 2-8=6 not), but we only need subset. So answer 3 is correct.
Now, consider points: 0, 2, 6, 8. Check triple: {0,2,4}?4 not. {0,6,12}?12 not. {2,6,10}?10 not. {0,8,16}?16 not. But maybe {2,6,8}? differences:4,2,6? 6 not. {0,2,8}? 2,8,6 -> 6 not. So no triple. Check pairs: 0-2=2 yes. So answer 2. So algorithm works.
Now, consider points: 0, 2, 4, 6, 8. Triples exist: {0,2,4}, {2,4,6}, {4,6,8}. So answer 3.
Now, consider points: -5, -1, 3, 7? Differences: -5 to -1 =4, -1 to3=4, 3 to7=4, but -5 to3=8, -5 to7=12 (not), -1 to7=8? Actually -1 to7=8, but -5 to7=12 not power. So set of four fails. Triples: any three consecutive in this arithmetic progression? -5,-1,3? differences:4,4,8 -> works. So answer 3.
Thus all consistent.
Now, one more nuance: The problem statement says "It is necessary to consider each pair of points, not only adjacent." That just clarifies that we need all pairwise distances to be powers of two, not just consecutive in the subset ordering.
Now, it's clear.
Thus final answer.
But before delivering final answer, I want to double-check if there is any possibility of a subset of size >3 if the points are not on a line? But they are on a coordinate line (1D). So no.
Thus final.
Now, I'll produce final solution code as described.
But let's also think about an alternative: Could there be a subset of size 3 that is not arithmetic if we allow distances like 1,2,3? No, 3 not power. So no.
Thus done.
Now, produce final answer with explanation.
But the user asked for a solution, not just code. The user is ChatGPT; we need to output the final answer with explanation and code. Since it's a "final" channel, we should produce the solution description and code.
Thus final response: Provide reasoning and then code.
Now, include necessary headers.
I'll write:
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (int i=0; i<n; ++i) cin >> a[i];
unordered_set<int> s(a.begin(), a.end());
vector<int> powers;
for (int i=0; i<31; ++i) powers.push_back(1 << i);
// Check for triple
for (int x : a) {
for (int p : powers) {
if (s.count(x-p) && s.count(x+p)) {
cout << "3\n" << x-p << " " << x << " " << x+p << "\n";
return 0;
}
}
}
// Check for pair
for (int x : a) {
for (int p : powers) {
if (s.count(x+p)) {
cout << "2\n" << x << " " << x+p << "\n";
return 0;
}
}
}
// Only one
cout << "1\n" << a[0] << "\n";
return 0;
}
Now, ensure output formatting: newline after size, then coordinates separated by spaces, then newline. That's fine.
Now, we might need to handle the case where we output a pair that is not in order; but that's fine.
Now, finalize.
But wait: There is a subtle bug: In the pair detection, we might output a pair where x+p is present, but what if x+p is the same as x? p>0 ensures not. So fine.
Now, one more nuance: When we check for triple, we might also find a triple where x-p or x+p are the same as x? Not possible.
Thus final answer.
But I'll also consider if there could be a triple with step d = 2^31? As argued no, but we could include it; but then x-p may be out of int range? Not really, but still fine. However, to be safe we can limit powers to those <= 2e9? But we already limited to 30, which is 2^30. 2^30 is 1073741824. That's fine. Actually coordinates range [-1e9,1e9], so the maximum possible power of two difference that can appear between two points is less than or equal to 2e9. The largest power of two <= 2e9 is 2^30 = 1073741824. The next is 2^31=2147483648 >2e9, so cannot appear as a difference between any two points because max diff = 2e9. So we can safely use powers up to 2^30. So we generate for i=0..30 inclusive (31 numbers). Good.
Now, we must be careful about p being int and left shifting 30: 1<<30 is within int if int is at least 32-bit? In C++, 1 is int, shift 30 yields 2^30, which fits in 32-bit signed int (max 2^31-1). So fine.
Now, done.
Now, also note that the triple detection may find a triple even if the three points are not all distinct? No.
Now, final answer.
But the problem might expect that we output the coordinates in any order, but we should not output duplicates. Our triple outputs three distinct coordinates.
Now, test with input that has duplicate points? Not allowed.
Now, final.
I'll also include a brief explanation of why max size is 3 in the solution.
Now, produce final answer. The maximum size of such a subset is at most 3.
For three distinct points with coordinates (a<b<c), the conditions (|a-b|=2^i), (|b-c|=2^j), (|a-c|=2^k) force (i=j) and hence (b-a=c-b=d) with (d) a power of two. Thus any valid triple must be an arithmetic progression with step a power of two.
If we try to take four points (a<b<c<d), then from the triples ((a,b,c)) and ((a,b,d)) we obtain (c = a+2(b-a)) and (d = a+2(b-a)), so (c=d), contradicting distinctness. Therefore a subset of size 4 or more is impossible.
Hence the answer is either 3 (if such a progression exists), else 2 (if any pair has a power-of-two difference), else 1.
We can find a triple by checking for each point (x) whether both (x-p) and (x+p) belong to the set for some power of two (p) (there are only about 31 relevant powers). If none exists, we look for a pair (x, x+p). If still none, output any single point.
The algorithm runs in (O(n \log C)) with (C\approx 2\cdot10^9) and easily handles the constraints.
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
unordered_set<int> s(a.begin(), a.end());
// all powers of two that can appear as a difference (2^0 .. 2^30)
vector<int> powers;
for (int i = 0; i <= 30; ++i) powers.push_back(1 << i);
// check for a triple (x-p, x, x+p)
for (int x : a) {
for (int p : powers) {
if (s.count(x - p) && s.count(x + p)) {
cout << "3\n" << x - p << ' ' << x << ' ' << x + p << '\n';
return 0;
}
}
}
// check for a pair (x, x+p)
for (int x : a) {
for (int p : powers) {
if (s.count(x + p)) {
cout << "2\n" << x << ' ' << x + p << '\n';
return 0;
}
}
}
// only single point possible
cout << "1\n" << a[0] << '\n';
return 0;
}