We need to solve multiple queries: given sorted distinct points a_i (ascending), we need to find integer x minimizing the (k+1)-th smallest distance from x to the points. That is, we sort distances |a_i - x| and take the (k+1)-th smallest (index starting from 1). Equivalent to: we want to minimize the (k+1)-th order statistic of distances. For k from 0 to n-1. For k=0, it's the minimum distance; we want x to be as close as possible to some point; basically minimize min_i |a_i - x|; that is minimized by any point between two points (or at a point) giving min distance; but integer point; we can find a_i itself; the minimum distance would be 0 if we choose x = a_i, so f_0(x)=0. So answer could be any a_i. For k=0, any a_i yields 0. But they said we can print any.
For k= n-1, we want to minimize the maximum distance, i.e., the furthest point; this is classic 1D minimax: minimize max_i |a_i - x|; optimal x is median of extreme points: any point between min and max inclusive gives minimax value = (a_n - a_1)/2? Actually minimax distance minimized by midpoint; for integer, choose floor or ceil of midpoint, whichever yields minimal max distance; that value = ceil((a_n - a_1)/2). So answer is around that.
But for general k, we need to minimize the (k+1)-th smallest distance. This looks like we want to choose x such that there are at most k points very close, and then the next one is as far as possible? Actually minimize the (k+1)-th smallest distance means we care about the distance to the (k+1)-th nearest point. We want that distance to be as small as possible. So we want to be close to at least k+1 points? But minimizing the (k+1)-th nearest distance means we want to be near to many points; but careful: If we pick x at a cluster, we can have several points close, then the (k+1)-th is small. So we want to place x in a region where there are at least k+1 points within some radius R, and we want to minimize R. Essentially we want to find the smallest R such that there exists an integer x with at least k+1 points within distance R (i.e., number of points in interval [x-R, x+R] >= k+1). Then f_k(x) is that minimal R achievable? But f_k(x) is the distance to the (k+1)-th nearest point, which is exactly the smallest R such that the ball of radius R centered at x contains at least k+1 points. So f_k(x) = min{R: count of points in [x-R, x+R] >= k+1}. That is indeed the distance to the (k+1)-th nearest neighbor. Then we want to minimize f_k(x) over integer x. So we want the minimum possible radius R* such that there exists some integer x with at least k+1 points within radius R*. Then answer x can be any that achieves it. Equivalent to: we want to find the minimal integer (or real) R for which there exists an integer x with interval length 2R covering at least k+1 points. But distances are absolute values; we can consider x as integer, points are integers? a_i are integers, x integer. So distances are integers? Actually |a_i-x| are integers if both integer, yes. So f_k(x) is integer. So we want to find integer x minimizing that integer.
Observation: f_k(x) is the distance to the (k+1)-th nearest point; that is essentially the radius of the smallest ball centered at x covering k+1 points. So we want to minimize that radius.
Interpret as: we want to find a set of k+1 points that can be covered by an interval of length 2R with center at integer x, minimizing R. Since the interval is [x-R, x+R], it must be symmetric about integer x, but the points are arbitrary integer positions. However, we can choose x anywhere integer. So we need to cover k+1 points with an interval of length 2R (so span max-min ≤ 2R) and such that the center is integer; the minimal R for covering those points with integer center is something like ceil((max - min)/2) maybe, but also the center must be integer, so we need to shift the interval such that its center integer and covers the points. Given a set of points, the minimal R needed to cover them with an integer center is the minimal R such that there exists integer c with all points in [c-R, c+R]. That's equivalent to: the points must lie in an interval of length 2R, and the interval's center is integer. Another way: we need to have max_i a_i - min_i a_i ≤ 2R, and also the midpoint (min+max)/2 must be within 0.5 of an integer? Actually condition: There exists integer c such that for all points, |a_i - c| ≤ R. That is equivalent to: max a_i - min a_i ≤ 2R, and also (max+min) parity condition? Because we need c such that c ≥ max - R and c ≤ min + R? Wait: For all points, we require a_i ∈ [c-R, c+R]. This implies c must satisfy: c ≤ a_i + R for all i => c ≤ min_i (a_i + R) = min a_i + R. Also c ≥ a_i - R for all i => c ≥ max_i (a_i - R) = max a_i - R. So there exists integer c iff max a_i - R ≤ min a_i + R, i.e., max - min ≤ 2R, and also there is an integer in the interval [max - R, min + R]. Since max - R ≤ min + R (if condition holds), the interval length is (min+R) - (max-R) = (min - max) + 2R = 2R - (max-min) ≥ 0. So any integer c in that interval works. Since interval length ≥ 0, it may contain at least one integer. But is it guaranteed? If the interval length is at least 1, definitely contains an integer. If length is 0 (i.e., max-min = 2R exactly), then the interval is a single point: c = max - R = min + R. That point might not be integer? Actually c = max - R = min + R. Since max and min are integers, and R may be integer? R = f_k(x) is integer because distances are integer. For our minimization, R will be integer. So if max-min = 2R, then c = max - R = min + R, which is integer because max and min are integers and R integer, so c integer. If max-min < 2R, the interval length >0, but could be less than 1, e.g., length = 0.5? But R integer, max-min integer, so 2R - (max-min) is integer. So length is integer. Since R integer, 2R is even integer, max-min integer, difference is integer. So length is integer >=0. If length >=1, there are integers. If length=0, the only point is integer. So condition reduces to simply max - min ≤ 2R. No parity constraints because integer c always exists if the interval length is integer and contains at least one integer? Let's check: interval [max - R, min + R] endpoints are integers? max - R is integer, min + R is integer. So interval between two integers inclusive. So if max - R ≤ min + R, there is at least one integer (the endpoints themselves are integers). So yes, there is integer c if and only if max - min ≤ 2R. So condition is just that.
Thus, for a given set of k+1 points, the minimal R needed to cover them with an integer center is ceil((max - min)/2). Because we need 2R >= max-min, minimal integer R = ceil((max-min)/2). And such R yields c = any integer between max - R and min + R inclusive; since (max - R) and (min + R) are both integers and (max - R) ≤ (min + R) because R >= ceil((max-min)/2) ensures that. So minimal R = ceil((max-min)/2). And then any integer c in that range works.
Therefore, the problem reduces to: given sorted points a[1..n], we need to choose a subset of exactly k+1 points (consecutive? The minimal covering interval length for a set of points is determined by its min and max. To minimize the required radius ceil((max-min)/2) we would want the subset to be as "tight" as possible, i.e., consecutive points in sorted order, because any non-consecutive set would have larger span. So the optimal subset of size k+1 to cover will be a contiguous block of k+1 points in sorted order. Because if we skip a point, the min and max remain same or farther? Actually consider we choose any k+1 points; the min and max are some a_i and a_j with j-i+1 ≥ k+1, because there are points in between. The span (a_j - a_i) is at least as large as the span of the contiguous block of size k+1 that starts at i and ends at i+k (i.e., the smallest possible span for a given number of points). So to minimize span, we should take consecutive points. So the optimal R is min over i from 1 to n-k of ceil( (a[i+k] - a[i]) / 2 ). Because the block of k+1 points from i to i+k inclusive. Then we need to find an integer center x that achieves this minimal R. Among those blocks achieving minimal R, we can output any x that works: e.g., the midpoint rounded? Since R = ceil((span)/2). For a given block with min = L, max = R (I'll use L and H), the minimal radius r = ceil((H - L)/2). Then any integer c in [H - r, L + r] works. Since we need to output any optimal x, we can just compute the block with minimal r, then output some integer c in that interval. Typically we can output the floor of the average? For even span, r = (H-L)/2 exactly, then H - r = L + r = (H+L)/2, which is integer if H+L even? Actually H+L even: if H-L even, then H+L even because parity: (H-L) even => H and L same parity => H+L even. So (H+L)/2 integer. So optimal x = (H+L)/2. For odd span, r = (H-L+1)/2, then H - r = H - (H-L+1)/2 = (2H - (H-L+1))/2 = (H+L-1)/2, and L + r = L + (H-L+1)/2 = (2L + H - L +1)/2 = (H+L+1)/2. So interval [ (H+L-1)/2 , (H+L+1)/2 ] which are two consecutive integers. So any of them works. So we can choose floor((H+L)/2) or ceil? Actually (H+L)/2 is half-integer for odd sum; floor((H+L)/2) = (H+L-1)/2, ceil = (H+L+1)/2. Both are valid. So we can output (L + H) // 2 (integer division floors) for simplicity; that will give one of the centers that yields r = ceil((H-L)/2). Let's verify: For even span, floor((L+H)/2) = (L+H)/2, which works. For odd span, floor((L+H)/2) = (L+H-1)/2, which is within [ (L+H-1)/2 , (L+H+1)/2 ] and indeed yields r = ceil((H-L)/2). Because if H-L is odd, say 5, then L+H maybe odd; floor((L+H)/2) gives integer; check distances: For x = (L+H)//2, compute distances to L and H: x - L = (H-L)//2? Actually if H-L=5, L=0, H=5, floor((5)/2)=2, distances: |0-2|=2, |5-2|=3, so max distance =3 = ceil(5/2)=3, so r=3. And the (k+1)-th nearest distance? It would be at most the farthest among the k+1 points; but we need to ensure that with x, the (k+1)-th nearest distance is exactly that r? Actually if we cover the block with radius r, then there are at least k+1 points within distance r, so f_k(x) ≤ r. But could it be less? f_k(x) is the (k+1)-th smallest distance; if there are exactly k+1 points in the block and maybe others outside also within that radius? Possibly there could be more points within that radius, but f_k(x) is the (k+1)-th smallest distance, which is ≤ r, but could be smaller if we have more points closer. However, we are minimizing f_k(x); if we achieve f_k(x) ≤ r, and we know that for any x, f_k(x) must be at least the minimal possible r over blocks (i.e., at least the minimal value of ceil((a[i+k]-a[i])/2) ), because to have at least k+1 points within distance f_k(x), the block of those k+1 points must have span ≤ 2 f_k(x). So f_k(x) >= min_i ceil((a[i+k]-a[i])/2). So the minimal possible f_k(x) is exactly that minimum. So if we can achieve equality, then that's optimal. And we can achieve equality by selecting x as described from a block achieving that min. But is it guaranteed that for that x, the (k+1)-th smallest distance is exactly that r? It could be smaller if there are more than k+1 points within radius r. But then f_k(x) would be the (k+1)-th smallest, which could be less than r. That would be even better, contradicting the lower bound? Wait: The lower bound says that for any x, f_k(x) >= min_i ceil((a[i+k]-a[i])/2). So if we get f_k(x) less than that lower bound, it's impossible. So if we choose x from the optimal block, we must have f_k(x) >= that min. But if there are more points within distance r, it could be that the (k+1)-th smallest distance is actually less than r, because we might have k+1 points that are even closer. But that would mean that there exists some other block of k+1 points with smaller span? Not necessarily; because the distances are measured from x; if x is close to a cluster of points, the (k+1)-th nearest distance might be small even if the block spanning those points is large? Let's think: The (k+1)-th nearest distance is the radius such that ball of that radius contains at least k+1 points, and ball of radius slightly smaller contains at most k points. That radius is determined by distances to points. The points that are within that ball are not necessarily consecutive in sorted order? They could be from two clusters far apart? But if they are far apart, the ball might need to be large to include both clusters. However, the ball is centered at x; if x is placed such that it's close to many points from both sides, it's possible that the set of k+1 nearest points includes points that are not contiguous? Actually the nearest points are determined by distance; they could be from anywhere. However, to have at least k+1 points within radius R, the set of points that lie within the interval [x-R, x+R] must have size ≥ k+1. This set will be contiguous in the sorted order because it's an interval on the line. So indeed the k+1 nearest points are the k+1 points with smallest distances; they form some subset, but they are exactly the points in [x-R, x+R] that are the closest, but they are not necessarily the entire set of points within the interval if there are more than k+1; but we consider the (k+1)-th smallest distance, which is the distance to the (k+1)-th nearest point. That distance is the smallest R such that the interval [x-R, x+R] contains at least k+1 points. So it's exactly the covering radius needed to include k+1 points. And the set of the k+1 nearest points is a subset of points within that interval; but the minimal R such that the interval contains ≥ k+1 points is determined by the distance to the (k+1)-th nearest point. So if we have a block of points, the required R to cover that block is exactly max distance from x to the farthest point in that block? Not exactly: The block we choose might not be exactly the k+1 nearest points; if there are additional points within that radius, then the (k+1)-th nearest point might be closer than the farthest point of that block because we might have more than k+1 points within that radius; but the (k+1)-th nearest distance is the distance to the (k+1)-th closest point, which could be less than the distance to the farthest point of a specific block. However, the covering radius needed to include at least k+1 points is the same as the distance to the (k+1)-th nearest point. So if we take x from the optimal block, and if there are other points that are even closer than some points in the block, then the (k+1)-th nearest might be determined by those closer points, potentially smaller than the radius needed to cover the block's extremes. But note: The block's extremes define the interval that contains at least those k+1 points. The covering radius needed to include at least k+1 points is the smallest R such that the interval contains k+1 points. That R could be smaller than the radius needed to cover the block's extremes if the block is not the actual k+1 nearest set. For example, suppose we have points at positions 0, 10, 11, 12. k=2 (so we need 3 nearest). If we take block [10,11,12] span=2 => r=1. If we choose x=11, distances: |0-11|=11, |10-11|=1, |11-11|=0, |12-11|=1. Sorted: 0,1,1,11. The 3rd smallest is 1. So f_2(11)=1, equal to r=1. If we choose x=5, distances: 5,5,6,7? Actually points:0->5,10->5,11->6,12->7. Sorted:5,5,6,7 => f_2=6. So that's larger. So the block method seems to produce equality. But consider points: 0, 1, 100, 101. k=2 (need 3 nearest). Block [0,1,100] span=100, r=50; block [1,100,101] span=100, r=50; block [0,1,101]? Not consecutive; but consecutive blocks: [0,1,100] span=100, [1,100,101] span=100. So minimal r=50. Choose x =? For block [0,1,100], optimal center integer: ceil((100-0)/2)=50, so x=50? That yields distances: 50,49,50,51? Actually |0-50|=50, |1-50|=49, |100-50|=50, |101-50|=51. Sorted:49,50,50,51 => f_2=50. So equal. If we choose x=51? distances: 51,50,49,50? Actually compute: 0->51,1->50,100->49,101->50; sorted:49,50,50,51 => f_2=50. So still 50.
But could there be a case where the minimal f_k(x) is less than min_i ceil((a[i+k]-a[i])/2)? No, because for any x, take the set of its k+1 nearest points; they are within some interval of length at most 2 f_k(x) (since all within distance f_k(x) of x, so they lie in [x - f_k(x), x + f_k(x)], thus max-min ≤ 2 f_k(x)). So the minimal possible f_k(x) is at least the minimal possible half-span of any k+1 points, which is min_i (a[i+k] - a[i])/2 ceiling. So lower bound holds. So the minimal achievable f_k(x) is at least that. And we can achieve exactly that by selecting x appropriately from a block achieving minimal span. But we must ensure that with that x, the (k+1)-th nearest distance is indeed exactly that radius, not smaller. If it's smaller, that would contradict the lower bound? Actually it's possible that the (k+1)-th nearest distance is smaller than the half-span of that block if there are other points even closer that cause the k+1 nearest to be a different set with smaller span? But then the minimal half-span of any k+1 points would be even smaller, contradicting the minimality of the block we chose. Since we are taking the block with minimal span among all k+1 points, any other set of k+1 points has span >= that minimal. So if for our chosen x, the actual set of k+1 nearest points (which are the k+1 points with smallest distances) has a span S, then f_k(x) must be at least ceil(S/2). But we are constructing x such that the radius we target is r = ceil(min_span/2). If there exists a different set of k+1 points that are even closer (i.e., distances less than r for all of them), then that set's span S' must satisfy ceil(S'/2) <= (some distance) < r, implying min_span <= S'? Wait careful: If the k+1 nearest points to x all have distances <= d (with d < r), then the set of those points is contained in [x-d, x+d], so span S' ≤ 2d < 2r. Hence ceil(S'/2) ≤ d < r. But the minimal span among all k+1 points is L = min_i (a[i+k]-a[i]). Then ceil(L/2) is our r. If there exists a set with span S' < L, that would contradict L being minimal. So S' must be at least L. But if d < r, then S' ≤ 2d < 2r = 2 * ceil(L/2). Could 2d be less than L? Possibly if L is odd? For example, L=5, r=3, 2d < 6 => could be 5. So S' could be 5, which equals L. But then d must be >= ceil(S'/2) = ceil(5/2)=3, so d >=3. So d cannot be <3. So if d < r, then 2d < 2r = 2*ceil(L/2). But if L is even, say L=4, r=2, 2d <4 => d<2, so S' <= something <4, but minimal span is 4, so impossible. If L is odd, say L=5, r=3, 2d <6 => d<3, so possible d=2.5? But distances are integer, so d could be 2. Then S' ≤ 4. But minimal span is 5, so impossible. So indeed it's impossible to have f_k(x) < r if r = ceil(L/2) where L = min_i (a[i+k]-a[i]). Because any set of k+1 points has span at least L, so the (k+1)-th nearest distance must be at least ceil(L/2). So equality can be achieved by selecting x appropriately. Therefore the optimal value is exactly min_i ceil((a[i+k] - a[i]) / 2). And we need to output any integer x that achieves this.
Thus, the solution is straightforward: For each query, compute over i=1..n-k: compute span = a[i+k] - a[i]; compute r = (span + 1)//2 (integer ceil). Keep track of minimal r. Then find an index i where r is minimal (the first or any). Then output an integer x that gives f_k(x) = that r. As derived, we can output floor((a[i] + a[i+k]) / 2) (i.e., (a[i] + a[i+k]) // 2). That works because:
- If span even: (a[i] + a[i+k]) // 2 = (a[i] + a[i+k])/2, and r = span/2.
- If span odd: (a[i] + a[i+k]) // 2 = floor((a[i]+a[i+k])/2) = (a[i]+a[i+k]-1)/2; then r = (span+1)/2 = (a[i+k]-a[i]+1)/2. Check that distance to farthest point in block: distance to a[i+k] is |a[i+k] - x|. x = floor(mid) = (L+H-1)/2. Then H - x = H - (L+H-1)/2 = (2H - L - H +1)/2 = (H - L +1)/2 = r. Similarly x - L = (L+H-1)/2 - L = (H - L -1)/2 = r -1? Actually compute: (L+H-1)/2 - L = (H - L -1)/2. For odd span, say L=0, H=5, x=2, distances: to L=2, to H=3. So max distance =3 = r, min distance among these two extremes? The farthest is H with distance 3, which equals r. The other extreme L has distance 2 which is less than r. But we require covering both L and H within radius r: yes, both distances <= r. So it's fine. The (k+1)-th nearest distance? With x=2, distances to the block points: besides L and H, there are the intermediate points (if any). Since we are considering block of size k+1, there are exactly k+1 points from L to H inclusive. Their distances to x will be at most r, with the farthest being r. So there are at least k+1 points within distance r. Could there be more points (outside block) within distance <= r? Possibly if there are points just outside the interval that are within r of x. That would increase the count, but then the (k+1)-th nearest might be even smaller, which would be good but might lower f_k(x) below r? Actually if there are more than k+1 points within distance r, then the (k+1)-th nearest distance is <= r. But could it be strictly less than r? For that to happen, the distance to the (k+1)-th nearest point must be less than r. That would require that at least k+1 points have distance < r. That would imply there exists a set of k+1 points with maximum distance < r. That set's span S would satisfy S <= 2*(r-1) = 2r-2. But r = ceil(L/2). If L is even, L=2r, then 2r-2 < L, so S < L, contradiction. If L is odd, L=2r-1, then 2r-2 = L-1 < L, so S ≤ L-1, again contradiction because minimal span is L. So impossible. Therefore even if there are extra points within distance r, the (k+1)-th nearest distance cannot be less than r because the k+1 points with smallest distances might include some from outside but still the set of the k+1 nearest points would have a span that is at most the span of those k+1 points; but if they all have distances < r, their span < L? Not necessarily: they could be spread out more? Actually if we have points outside the block, they might be farther than the block extremes? But if they are within distance < r, they must lie in [x-(r-1), x+(r-1)]. This interval length is 2r-2. For any set of points lying within that interval, its span ≤ 2r-2. Since L = minimal span of any k+1 points, we have L ≤ any such span? Actually L is the minimal span among all k+1 points. So if there exists any set of k+1 points with span ≤ 2r-2, then L ≤ 2r-2. But we have L = minimal span. Our r = ceil(L/2). So we need to check consistency. Suppose L is even: L=2r. Then 2r-2 < L, so any set with span ≤ 2r-2 would have span less than L, contradicting minimality (since L is the minimal span). So no such set exists. If L is odd: L=2r-1, then 2r-2 = L-1 < L, again contradiction. Therefore there cannot be k+1 points all with distance < r. So the (k+1)-th nearest distance is exactly r. Good.
Thus algorithm: For each test case, read n,k, read a array. Compute best = INF, best_i = 0. For i in 0..n-k-1 (0-indexed), compute span = a[i+k] - a[i]; r = (span + 1) // 2; if r < best, update best=r and best_i=i. Then output x = (a[best_i] + a[best_i + k]) // 2.
But need to verify for edge cases: k = 0: n >=1, k=0 => we need block of size 1? Actually k+1 = 1. Then we consider i from 0 to n-1 (since n-k = n), span = a[i] - a[i] = 0, r = 0. So best=0, best_i any i, e.g., i=0. Then x = (a[i] + a[i]) // 2 = a[i]. So output a point a[i], which is fine (f_0(x)=0). So it matches.
k = n-1: block size n. Only i=0, span = a[n-1] - a[0], r = ceil(span/2). Output floor((a[0]+a[n-1])/2) which is the integer minimax center. That's correct.
Thus solution is O(n) per test case, total O(∑ n) ≤ 2e5, which is fine.
But wait: We assumed that the optimal x is integer. And we found that for a block, the minimal radius is ceil(span/2), and an integer center exists achieving that radius. But we need to ensure that the integer center we output actually yields f_k(x) = that r. As argued, it does. So answer is correct.
Let's test with given sample:
3
3 2
1 2 5
Block size 3? k=2, k+1=3, n=3 so only i=0: span=5-1=4, r= (4+1)//2? Actually ceil(4/2)=2, so r=2. x = (1+5)//2 = 3. Output 3 matches.
2 1
1 1000000000
k=1, k+1=2, n=2 => i=0: span=1e9-1=999999999? Actually 1e9 - 1 = 999,999,999? Wait points: 1 and 1e9, so span = 1e9 - 1 = 999,999,999? Actually 1e9 = 1000000000, difference = 999,999,999. Ceil half = (999,999,999+1)//2 = 500,000,000. x = (1 + 1e9)//2 = 500,000,000 (since 1000000001//2 = 500,000,000? 1000000001/2=500,000,000.5 floor = 500,000,000). Output 500000000 matches.
1 0
4
n=1, k=0 => block size 1: span=0, r=0, x = (4+4)//2 = 4. Output 4 matches.
So sample works.
Thus solution is straightforward.
But wait, we need to consider that x must be integer and points a_i are up to 1e9, sum of n up to 2e5, so no overflow issues if using 64-bit.
Potential pitfalls: When computing (span+1)//2 for large numbers, it's fine. For best_i, we can store index.
Edge Cases: n can be as large as 2e5, but per test case sum ≤ 2e5, so O(n) works.
Thus the answer: For each test case, compute min over i of ceil((a[i+k] - a[i])/2) and output the midpoint (floor of average) for that i.
But is it always true that the optimal x is from a block of k+1 consecutive points? Let's formalize: For any x, the set of points within distance f_k(x) is an interval [x - f, x + f] that contains at least k+1 points. Among those points, consider the leftmost and rightmost; the span between them is at most 2f. And those are among the original points; they are not necessarily consecutive indices? They could be the extreme points of the set, but there might be missing points between them? Actually all points between the leftmost and rightmost are inside the interval because the interval covers the whole segment between them? Wait: The points are on a line. If the leftmost and rightmost are within [x-f, x+f], then the whole segment between them is also within that interval? Not necessarily: The interval [x-f, x+f] is contiguous. If the leftmost point is at position L and rightmost at position R, and both are within [x-f, x+f], then for any point between L and R, its position is between L and R, and since L >= x-f and R <= x+f, any point between is also between x-f and x+f. However, there might be points that are not present (we only have discrete points). But we are concerned about the set of points we have. The leftmost and rightmost points we consider are the extreme among those that lie in the interval. Since the interval is contiguous, any point that lies between L and R is also within the interval, but we don't know if such points exist in our set. However, the span of the set of points that are within the interval is determined by its extremes; there might be gaps, but that doesn't affect the span. But the minimal span of k+1 points could be achieved by a non-consecutive set? Suppose points: 0, 10, 11, 12, 20. k=2 (need 3 points). The consecutive block [10,11,12] has span 2. Non-consecutive block [0,11,12] has span 12. So consecutive gives smaller span. But could there be a case where the minimal span among subsets of size k+1 is not achieved by a consecutive block? In general, for points on a line, the subset of given size with minimal diameter (max-min) is indeed a set of consecutive points after sorting. Because if you have a subset with min index i and max index j, and there is a point between them not included, you could replace the max or min with that interior point to reduce the span while keeping size same? Actually to minimize span, you want the points to be as close together as possible. If you have a subset that skips some points, you could replace the extreme point with a skipped point that is inside, possibly reducing span. More formally, consider sorted points a1 < a2 < ... < an. For any subset S of size m, let i = min index in S, j = max index in S. Then j - i + 1 ≥ m because there might be missing indices. The span is a_j - a_i. If there is any index t between i and j not in S, then we could consider replacing either i or j with t to possibly reduce span. But to guarantee that there is a consecutive block of size m with span ≤ a_j - a_i, note that the consecutive block from i to i+m-1 (if i+m-1 ≤ j) has span a_{i+m-1} - a_i. Since i+m-1 ≤ j (because the number of indices from i to j inclusive is at least m, so j ≥ i+m-1), we have a_{i+m-1} ≤ a_j. Thus a_{i+m-1} - a_i ≤ a_j - a_i. So the consecutive block starting at i has span no larger. Actually careful: Suppose S has indices {i, j, and some others}. The number of points from i to j inclusive is at least m, but could be more than m if there are many points between. The smallest possible span for a block of size m starting at i is a_{i+m-1} - a_i. Since j ≥ i+m-1, a_j ≥ a_{i+m-1}, so a_j - a_i ≥ a_{i+m-1} - a_i. So indeed the consecutive block of size m starting at i has span ≤ that of S. But S might have min index i' > i? Actually i is min index in S. So the consecutive block starting at i of size m exists because i+m-1 ≤ n? Since S has m points, and max index is j, the total indices from i to j inclusive is at least m, but could be larger; but i+m-1 might be > j? No, if there are exactly m points between i and j inclusive, then j = i+m-1 exactly. If there are more than m points, then j > i+m-1. So i+m-1 is within [i, j], so it's a valid index. So a_{i+m-1} is defined as long as i+m-1 ≤ n. Since S has m points, the maximum index j is at least i+m-1 because you need at least m distinct indices from i to j. So i+m-1 ≤ j ≤ n. So indeed the consecutive block from i to i+m-1 is a subset of size m (though maybe not exactly S, but it exists). Its span is ≤ the span of S. So the minimal span over all subsets of size m is achieved by some consecutive block. So correct.
Hence the solution is proven.
But wait: The problem statement says we need to find integer point x that minimizes f_k(x). Our approach yields a candidate x. But could there be an x that yields the same minimal f_k(x) but not derived from a block of consecutive points? Possibly, but we just need any. So it's fine.
But we must be cautious: The block we choose yields r = ceil((a[i+k]-a[i])/2). Is it always true that with x = floor((a[i]+a[i+k])/2), f_k(x) = r? As argued, yes. But let's test more thoroughly with an example that could have extra points inside the interval [x-r, x+r] beyond the block. Could that cause f_k(x) to be less than r? We argued impossible because that would require k+1 points with all distances < r, implying a set of k+1 points with span < 2r =? But suppose there are points inside the interval that are very close to x, but the block we considered includes the extremes which are at distance r. The k+1 nearest points might exclude the farthest extreme and include a closer point from outside the block? But then the set of k+1 nearest would be some set of points that are all within distance < r. To see if that's possible, we need to examine if there exists a set of k+1 points whose maximum distance to x is < r. That set would be a subset of points that lie within (x - (r-1), x + (r-1)). That interval length is 2r-2. For such a set to exist, we need that there are at least k+1 points in that narrower interval. Is that possible while still the minimal span over all k+1 points is L? Possibly if L is small, but our r = ceil(L/2). Could there be more than k+1 points in that narrower interval? If yes, then we would have a subset of size k+1 all within distance < r. That subset's span S would be ≤ 2r-2. But we defined L = min_{blocks} (a[i+k] - a[i]). Could it be that L ≤ 2r-2? Since r = ceil(L/2). If L is even, L=2r, then 2r-2 = L-2 < L. So L > 2r-2, so any interval of length 2r-2 cannot contain k+1 points because if it did, the span of those points would be at most 2r-2, which would be less than L, contradicting L being minimal. So not possible. If L is odd, L=2r-1, then 2r-2 = L-1 < L, same contradiction. So indeed impossible. So safe.
Thus algorithm is correct.
But we need to consider also that x must be integer, and our computed x is integer. Good.
Now implement.
Potential issues: The input constraint says sum of n ≤ 2e5, T up to 2e5, so many small test cases. So we need fast I/O.
Implementation steps:
- Read T.
- Loop over T:
- read n, k.
- read array a of n ints.
- Initialize best_r = large, best_x = 0.
- Loop i from 0 to n-k-1:
- span = a[i+k] - a[i]
- r = (span + 1) // 2 (ceil division)
- if r < best_r:
best_r = r
best_x = (a[i] + a[i+k]) // 2 // we can compute x later from best_i
// Or store best_i and compute at end.
- Print best_x.
But careful: If multiple i have same r, we can choose any. The problem says any.
Edge case: n-k could be 0? Actually k < n, so n-k >=1. So loop works.
Double-check for n=1,k=0: n-k=1, i=0, span=0, r=0, best_x = (a[0]+a[0])//2 = a[0]. Good.
For n=2,k=1: i=0, same.
For n large.
Test some more:
Example: n=5, points: [1,2,3,100,101], k=2 (need 3 points). Consecutive blocks:
i=0: [1,2,3] span=2, r=1, x=(1+3)//2=2.
i=1: [2,3,100] span=98, r=49, x=51.
i=2: [3,100,101] span=98, r=49, x=52.
So best r=1, x=2. f_2(2): distances: 1,0,1,98,99 -> sorted:0,1,1,98,99 => third smallest =1. So indeed f=1.
Now consider k=3 (need 4 points). Blocks:
i=0: [1,2,3,100] span=99, r=50, x=50 or 51? (1+100)//2=50. distances: 49,48,47,50,51? Actually compute: 1->49,2->48,3->47,100->50,101->51; sorted:47,48,49,50,51 => fourth smallest =50. So f=50.
i=1: [2,3,100,101] span=99, r=50, x=(2+101)//2=51. distances: 2->49,3->48,100->49,101->50,1->50? Actually 1->50? distances: |1-51|=50, |2-51|=49, |3-51|=48, |100-51|=49, |101-51|=50 => sorted:48,49,49,50,50 => fourth =50. So f=50. So best r=50. Could there be x giving f < 50? Possibly x=50 gave 50, x=51 gave 50. Could any give 49? To have 4 points within distance 49, need interval length 98, and need 4 points with span ≤98. The minimal span for 4 points is min_i (a[i+3]-a[i]) = min(99,99)=99, which >98, so impossible. So correct.
So solution stands.
Now we should double-check the reasoning for general k. Actually we used that the minimal f_k(x) equals min_i ceil((a[i+k] - a[i])/2). But is it always achievable? For a block with minimal span L = a[i+k] - a[i], we set r = ceil(L/2). We need to show there exists integer x such that all points in that block are within distance r. That's true as argued. However, we also need to ensure that f_k(x) = r, not less. As argued, f_k(x) cannot be less because otherwise there would be a set of k+1 points with span < L. So it's exactly r. So good.
One nuance: The points are sorted, but we must consider that when we choose x from the optimal block, there might be other points also within distance r, potentially making the (k+1)-th nearest distance smaller? But as argued, that would imply existence of k+1 points with all distances < r, which contradicts minimal span. However, what if there are exactly k+1 points within distance r, but some of them are not from the block? Then the set of those k+1 points might have a span that is not necessarily less than L? Actually if they are all within distance r, they lie in [x-r, x+r]. The span of this set is at most 2r. Could it be exactly L? Possibly if 2r = L or larger? Let's check. If there exists a set of k+1 points within distance r, their span S ≤ 2r. Could S be less than L? If 2r < L, then S ≤ 2r < L, contradiction. But we have r = ceil(L/2). For L even, 2r = L, so S could be ≤ L. It could be exactly L (if the set includes extremes). But if the set is different from the block, it could still have span S = L or less. If S < L, that's a problem. But can S be less than L? If the set of k+1 points within distance r has S < L, then that would contradict L being the minimal span. So we need to ensure that any set of k+1 points within distance r must have span at least L. But is it possible that due to x placement, we get a set of k+1 points whose span is less than L? That would mean there exists a set of k+1 points with span less than L. But L is defined as the minimal span among all k+1 points. So such a set cannot exist. Therefore any set of k+1 points has span >= L. So if all points in some set are within distance r, then we must have 2r >= S >= L. Since r = ceil(L/2), we have 2r >= L, but could be > L. So the condition 2r >= L is satisfied. However, could there be a set of k+1 points within distance r but with S < L? That would contradict the definition of L. So no. So the (k+1)-th nearest distance cannot be less than r, because that would mean there exists a set of k+1 points all with distance < r, implying S <= 2(r-1) < 2r, but not necessarily < L? Actually if r = ceil(L/2), then 2(r-1) = 2r-2. If L is even, L=2r, then 2r-2 = L-2 < L, so S <= L-2 < L, contradiction. If L is odd, L=2r-1, then 2r-2 = L-1 < L, also contradiction. So indeed no set of k+1 points can have all distances < r. Therefore the (k+1)-th smallest distance must be at least r. And since we have at least k+1 points within distance r (the block itself), the (k+1)-th smallest is exactly r (since there are at least k+1 points with distance ≤ r, and the (k+1)-th smallest is the smallest value v such that at least k+1 distances ≤ v; because the distances are integers, and we have at least k+1 distances ≤ r, but not all k+1 distances could be ≤ r-1, because that would require a set of k+1 points with distances ≤ r-1, which we proved impossible). So indeed f_k(x)=r. Good.
Thus solution stands.
Now we must write code accordingly.
Complexities: O(total n) time, O(n) memory per test? We can read array of size n for each test, but total sum ≤ 2e5, so okay.
Now implement in C++:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while(T--) {
int n, k;
cin >> n >> k;
vector<int> a(n);
for(int i=0; i<n; i++) cin >> a[i];
int best_r = INT_MAX;
int best_x = 0;
for(int i=0; i <= n - k - 1; i++) {
int span = a[i+k] - a[i];
int r = (span + 1) / 2; // ceil
if(r < best_r) {
best_r = r;
best_x = (a[i] + a[i+k]) / 2; // floor of average
}
}
cout << best_x << "\n";
}
return 0;
}
But careful: i <= n - k - 1 because i from 0 to n-k-1 inclusive. Actually number of blocks = n - k. So loop i=0; i < n - k; i++. Use i < n - k. Since n - k >=1.
Also best_r initial could be 2e9, but span up to 1e9, r up to 5e8, so int (32-bit) might hold? a[i] up to 1e9, sum up to 2e9, but (a[i] + a[i+k]) could be up to 2e9, which fits in 32-bit signed int (max ~2.147e9). Actually 2e9 is safe. But to be safe, use long long for sum? 2e9 fits in int. However, if a[i] = 1e9, a[i+k] = 1e9, sum = 2e9 which is within signed 32-bit (2,147,483,647). So int is fine. But r = (span+1)/2 may be up to 5e8, also fits. So int ok.
But sometimes we might have n up to 2e5, many test cases, but fine.
Potential off-by-one: For k = n-1, n-k = 1, i=0, works.
Thus answer.
But wait, I recall that some problems with similar description might require to consider x not necessarily integer? But here x must be integer. Our derivation used integer centers. The condition for existence of integer center was simply max-min ≤ 2R, because endpoints are integers and interval length is integer. That is correct because R is integer. But we must ensure that the radius we compute is integer, and we output integer x. Good.
Now check sample again.
All good.
Now think about if there is any nuance: The function f_k(x) sorts distances and takes d_{k+1}. Since we use 0-index? They said non-descending list d_1,...,d_n and take d_{k+1} as result. So index starting from 1. So our interpretation is correct.
Now just to be absolutely rigorous: The minimization problem is min_x f_k(x) where x ∈ Z. Our algorithm gives a candidate x that achieves min over all integer x. Is it possible that the minimal f_k(x) over integer x is strictly larger than min_i ceil((a[i+k] - a[i])/2)? Because maybe the integer constraint could increase the minimal possible radius for covering a given block. But we already argued that given block of points with span L, the minimal integer radius R needed to cover them with integer center is ceil(L/2). Because condition is max-min ≤ 2R, and we can always pick integer c in [max-R, min+R] because endpoints are integers and interval is non-empty when R >= ceil(L/2). But is it always possible to pick an integer c? The interval is [max-R, min+R]. Since max-R and min+R are integers (max,min,R integers), the interval contains at least one integer: either endpoints or any integer between them. Since max-R ≤ min+R when R ≥ ceil(L/2). So yes, there exists integer c. So the integer constraint does not increase the required radius. So the lower bound is achievable.
Thus algorithm is correct.
Now we might also consider multiple blocks with same r, we output the first one's midpoint. That's fine.
Now we can produce final answer.
But wait: The problem statement says: "You are given n points a_1, a_2, ..., a_n on the OX axis. Now you are asked to find such an integer point x on OX axis that f_k(x) is minimal possible." And we are printing integer x.
Our algorithm prints x as integer.
Now consider potential pitfalls: The function f_k(x) for x integer yields integer distances. The minimal possible value of f_k(x) is indeed integer. Our computed r is integer.
Now we can implement.
Now let's test with some random small cases by brute force to ensure correctness.
We'll mentally test small n.
Case: n=3, points [1,4,6]. k=1 (need 2 nearest). Compute blocks: i=0: [1,4] span=3, r=ceil(3/2)=2, x=(1+4)//2=2. i=1: [4,6] span=2, r=1, x=5. So best r=1, x=5. Now compute f_1(5): distances: |1-5|=4, |4-5|=1, |6-5|=1 => sorted:1,1,4 => second smallest =1. So f=1. Could there be x giving f=0? To have second smallest distance 0, need at least two points at distance 0, i.e., x equal to at least two points, but points are distinct, so impossible. So minimal is 1. Good. What about x=4? f_1(4): distances: 3,0,2 -> sorted 0,2,3 => second =2. So worse. So our x=5 is optimal.
Check x=3? distances:2,1,3 => sorted 1,2,3 => second=2. So 5 is best.
Now check if any other integer yields f=1? Possibly x=6 gives distances:5,2,0 -> sorted 0,2,5 => second=2. x=2 gives distances:1,2,4 -> 1,2,4 => second=2. So only x around 5? Actually maybe x=4.5? Not integer. So okay.
Now case: n=4, points [1,2,3,10]. k=2 (need 3 nearest). Blocks:
i=0: [1,2,3] span=2, r=1, x=(1+3)//2=2.
i=1: [2,3,10] span=8, r=4, x=6.
So best r=1, x=2. f_2(2): distances:1,0,1,8 -> sorted 0,1,1,8 => third=1. Good.
Could x=1 give f= ? distances:0,1,2,9 -> sorted 0,1,2,9 => third=2. So not better.
Thus correct.
Now case: n=4, points [1,3,4,7]. k=2. Blocks:
i=0: [1,3,4] span=3, r=2, x= (1+4)//2=2.
i=1: [3,4,7] span=4, r=2, x= (3+7)//2=5.
Both r=2. Our algorithm picks first i=0, x=2. Check f_2(2): distances:1,1,2,5 -> sorted 1,1,2,5 => third=2. f_2(5): distances:4,2,1,2 -> sorted 1,2,2,4 => third=2. Both give 2. So any works.
Now case: points [1,2,5,6]. k=2. Blocks:
i=0: [1,2,5] span=4, r=2, x=(1+5)//2=3.
i=1: [2,5,6] span=4, r=2, x=(2+6)//2=4.
Both r=2. Check x=3: distances:2,1,2,3 -> sorted 1,2,2,3 => third=2. x=4: distances:3,2,1,2 -> sorted 1,2,2,3 => third=2. Good.
Now case: points [1,4,5,9]. k=2. Blocks:
i=0: [1,4,5] span=4, r=2, x=3.
i=1: [4,5,9] span=5, r=3, x=6.
So best r=2, x=3. Check x=3: distances:2,1,2,6 -> sorted 1,2,2,6 => third=2. Good. Could there be x=4? distances:3,0,1,5 -> sorted 0,1,3,5 => third=3. Worse.
Now test case where the minimal span block yields x that might cause extra points within radius r from outside that could reduce f_k(x) below r? We argued impossible. But let's attempt to find a counterexample: Suppose points are [0, 5, 6, 10]. k=2 (need 3). Blocks:
i=0: [0,5,6] span=6, r=3, x=(0+6)//2=3.
i=1: [5,6,10] span=5, r=3, x=(5+10)//2=7 (floor 7.5 =7).
So minimal r=3 from both? Actually i=0 span=6 => r=3; i=1 span=5 => ceil(5/2)=3. So r=3 both. For i=0, x=3: distances: 3,2,3,7 -> sorted 2,3,3,7 => third=3. For i=1, x=7: distances:7,2,1,3 -> sorted 1,2,3,7 => third=3. Both f=3.
Now is there an x that gives f=2? To have three points within distance 2, the interval length would be 4, need three points with span ≤4. Our points: any three? [0,5,6] span=6>4; [5,6,10] span=5>4; [0,5,10] span=10>4; [0,6,10] span=10>4. So none. So impossible. So correct.
Now consider points [0,1,2,100]. k=2. Blocks:
i=0: [0,1,2] span=2, r=1, x=1.
i=1: [1,2,100] span=99, r=50, x=50.
Best r=1, x=1. Check x=1: distances:1,0,1,99 -> sorted 0,1,1,99 => third=1. Good.
Now think about case with many points, but minimal span block is not unique; but our selection is fine.
Now consider edge case where the optimal block yields x that is not integer? But our calculation yields integer because we use floor division. However, note: For block with even span, the true optimal centers are exactly one integer: the midpoint. For odd span, there are two integer centers: floor and ceil of midpoint. Our floor gives one of them. So correct.
Thus the solution is straightforward.
Now before finalizing, let's read the problem statement again to ensure no misinterpretation: "form a list of distances d_i = |a_i - x|; sort list d in non-descending order; take d_{k+1} as a result." So it's exactly the (k+1)-th smallest distance. Our reasoning is correct.
One nuance: They said "You are given n points a_1, a_2, ..., a_n on the OX axis." The points are given in ascending order: 1 ≤ a_1 < a_2 < ... < a_n ≤ 10^9. So they are distinct and sorted. Good.
Now confirm constraints: ∑n ≤ 2e5, T ≤ 2e5. So we must ensure we handle many small test cases. Our loop is O(n) per test, fine.
Now potential issues: In the loop, we compute (a[i]+a[i+k])//2; but what if a[i] + a[i+k] overflows 32-bit? Maximum sum is 2e9, which fits in 32-bit signed int (2,147,483,647). So okay. But to be safe, use long long? It's fine either way.
Now I think we have final solution.
But wait: I recall that Codeforces problem "Minimum Median Distance" or similar? Actually this looks like problem from Codeforces Round #... There is a problem "1472B? No, maybe "1730B"? Not exactly. But anyway.
Now double-check: The input format: first line T, then for each query, two lines: first line n,k; second line a1..an. They guarantee sum of n ≤ 2e5. So we can read.
Now we produce the output.
Now finalize.
Now let's test with a custom case: T=1, n=5, k=2, a = [1,3,5,7,9]. Blocks:
i=0: [1,3,5] span=4 r=2 x=3
i=1: [3,5,7] span=4 r=2 x=5
i=2: [5,7,9] span=4 r=2 x=7
All r=2. Choose first x=3. f_2(3): distances:2,0,2,4,6 -> sorted 0,2,2,4,6 => third=2. Good.
Now maybe a case where the minimal r is not from the block with minimal span? Actually r = ceil(span/2) is monotonic in span, so minimal r corresponds to minimal span. So it's equivalent.
Thus solution is essentially find the minimal length of a segment covering k+1 points (i.e., min_i (a[i+k] - a[i])), and then answer is ceil(that/2) and the center is the midpoint (floor average) of that segment.
Now we should consider that the optimal x might not be exactly that midpoint? Could there be an x that yields the same r but not from that block? Possibly, but we don't care.
Now we are ready.
But wait, the problem wants x such that f_k(x) is minimal possible. Our x from the block achieves that minimal. So answer is correct.
Now there is a subtlety: The function f_k(x) depends on all points, not just the block. But as argued, the value is r. Good.
Now implement.
Edge Cases: k = n-1, block covers all points. Compute span = a[n-1] - a[0], r = ceil(span/2), x = (a[0]+a[n-1])//2. That is integer minimax center. Works.
Now let's also test with extreme values: n=2e5, all points random. Our loop will be O(n) which is okay.
Now we can write final code.
But I want to consider if there's any scenario where using floor((L+H)/2) might not be optimal if there are multiple blocks with same r? It's fine.
Now also check the problem's example:
Input:
3
3 2
1 2 5
2 1
1 1000000000
1 0
4
Our algorithm gives 3, 500000000, 4.
Matches.
Thus final answer.
Now possible alternative approach: binary search on answer. But our direct method is simpler.
Now we should also consider if the answer might be something else due to parity? Already covered.
Now finalize solution.
Now I'll produce final answer with explanation.
We'll write the solution in C++.
Potential improvements: Use scanf/printf for speed. But with ios_base sync, cin is okay.
Now produce final answer.
Let's also consider memory: We can read array per test using vector.
Now code:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
int best_r = INT_MAX;
int ans = -1;
for (int i = 0; i < n - k; ++i) {
int span = a[i + k] - a[i];
int r = (span + 1) / 2;
if (r < best_r) {
best_r = r;
ans = (a[i] + a[i + k]) / 2;
}
}
cout << ans << '\n';
}
return 0;
}
That should pass.
Now what about the case when best_r remains INT_MAX? That would happen only if n-k = 0, but k=1, so loop runs at least once. So ans set.
Thus final answer.
Now we can write solution explanation accordingly.
We should include reasoning: f_k(x) is the distance to the (k+1)-th nearest point. For any x, we need at least k+1 points within distance f_k(x). Thus the set of k+1 points must have span ≤ 2 f_k(x). The minimal possible f_k(x) is at least ceil(D/2) where D is the minimal span of any k+1 points. And that is achievable by taking the block of k+1 consecutive points with minimal span and placing x at the midpoint (or integer near). So answer: compute min_i ceil((a[i+k] - a[i])/2) and output floor((a[i]+a[i+k])/2) for that i.
Thus done. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
int best_r = INT_MAX;
int ans = -1;
// examine every block of k+1 consecutive points
for (int i = 0; i < n - k; ++i) {
int span = a[i + k] - a[i];
int r = (span + 1) / 2; // ceil(span/2)
if (r < best_r) {
best_r = r;
ans = (a[i] + a[i + k]) / 2; // floor of average
}
}
cout << ans << '\n';
}
return 0;
}