We need to solve this problem: given an array a of length n, with some entries being -1 indicating missing values. We need to choose an integer k (0 ≤ k ≤ 1e9) to replace all -1 entries with k. Let m be the maximum absolute difference between adjacent elements after replacement. We need to minimize m, and output the minimum possible m and some k achieving it.
Constraints: t up to 1e4, sum n ≤ 4e5. So we need per test case O(n) or O(n log n) solution.
We must find k to minimize the maximum over i of |a_i - a_{i+1}| where missing entries become k.
Let's denote the original array with some known values and some unknown that become k.
We need to consider edges between positions. For each adjacent pair (i, i+1), if both are known, then |a_i - a_{i+1}| is fixed and does not depend on k. If one is missing and the other known, then the absolute difference becomes |k - known|. If both missing, then |k - k| = 0.
Thus m(k) = max( fixed_diffs_max, max over known adjacent to missing of |k - known| ), where fixed_diffs_max = maximum of |a_i - a_{i+1}| for pairs where both known. And also pairs with both missing yield 0.
So the problem reduces to: we have a set S of known numbers that are adjacent to at least one missing element? Actually careful: For each pair (i,i+1), if exactly one is known (say value v) and the other is missing, then the resulting absolute difference is |k - v|. If both are known, we have a fixed value. If both missing, difference 0 (which is ≤ anything). So the overall m(k) is the maximum of:
- M0 = max_{i: both known} |a_i - a_{i+1}|
- M1(k) = max_{v in V} |k - v|, where V is the set of known values that are adjacent to a missing element? But careful: A known value may appear in more than one pair with missing neighbor. But the contribution is still |k - v|. However, could there be a known value that appears on both sides? It doesn't matter; we just need the maximum over all such v of |k - v|. But also note that if a known value is adjacent to two missing neighbors (like ... -1, known, -1 ...), it contributes the same |k - known| twice but maximum only cares about the maximum absolute difference.
Thus indeed, the problem becomes: Given a set V of known values that are adjacent to any missing element, and also considering the fixed max difference M0, we need to choose k to minimize max( M0, max_{v in V} |k - v| ).
This is essentially: we want to minimize the maximum distance from k to all points in V, with a lower bound of M0 (i.e., the answer must be at least M0). So if we can choose k such that max_{v in V} |k - v| = D, then m = max(M0, D). To minimize m, we need to find smallest D possible such that there exists k with max_{v in V} |k - v| ≤ D, then m = max(M0, D). But note that D can be made arbitrarily large; we want to minimize max(M0, D). The minimal achievable max(M0, D) is at least M0. So we need to find the smallest D such that there exists k with max_{v in V} |k - v| ≤ D. This D is the radius of the smallest interval covering V (i.e., the minimal possible value of max distance from a point to V). That minimal max distance is half the diameter of V? Actually, for a set V, the minimax distance to a point is the smallest radius covering all points; i.e., the Chebyshev (L_inf) 1-center problem on the line. The optimal k is any point in the interval [min(V) + D, max(V) - D]? Wait: To cover points with an interval of length 2D (i.e., all points lie within [k - D, k + D]), the minimal D needed is half the range (diameter) of V: D_min = ceil( (max(V) - min(V)) / 2 )? Actually if we consider real numbers, the minimal D such that there exists k with all |k - v| ≤ D is exactly (max(V) - min(V)) / 2. Because we can set k at the midpoint of min and max, then the farthest distance is half the spread. So D_min = (max(V) - min(V)) / 2. But note k must be integer? Actually k can be any integer 0..1e9. So D_min could be fractional, but we need m integer? m is defined as maximum absolute difference, which is integer (since absolute difference of integers). The answer m must be integer. However, we can choose k integer, and the max distance from integer k to integer set V will be integer? Not necessarily, but the max absolute difference is integer because |k - v| is integer if both are integers. So D_min we compute must be such that there exists integer k with max|k - v| = some integer D. The minimal possible maximum distance over integer k is essentially the ceiling of half the diameter? Let's analyze.
Given a set V of integers (or maybe not? a_i can be up to 1e9, but known values are given, they are integers, could be 0..1e9? Actually a_i given are non-negative? The problem statement says array a of n non-negative integers. But then input says a_i can be -1 or non-negative? It says a_i are -1 ≤ a_i ≤ 1e9. It says Dark created array a of n non-negative integers, but some disappeared (became -1). So known values are non-negative (0..1e9). So V consists of non-negative integers. k must be also non-negative (0 ≤ k ≤ 1e9). So we have constraints on k: k must be within [0, 1e9]. However, typical test cases may allow k in that range; but it may be that the midpoint falls outside, so we need to consider boundary.
Nevertheless, in typical cases the optimal k will be between min(V) and max(V) or close.
Thus the answer m = max( M0, D_opt ), where D_opt is the minimal possible maximum distance from k (integer, 0≤k≤1e9) to all points in V.
But wait: What if V is empty? That would happen if there are no known values adjacent to any missing element? But the problem guarantees at least one missing element in each test case. Could it be that all missing elements are isolated? For V to be empty, there must be no pair where exactly one is known. That would mean that all known values are not adjacent to any missing; but there is at least one missing; consider n=2, both -1 => then no known values adjacent to missing because both are missing, so V empty. Also consider n=3: -1, -1, 5: The adjacent pairs: (1,2): both missing => no known; (2,3): one known (5) adjacent to missing at position 2, so V includes 5. So V empty only if there is no known element adjacent to any missing, i.e., either all positions are missing? Actually if there is at least one known, and a missing adjacent to it, then V non-empty. So V empty only when all positions are missing? But also if known exist but are all isolated from missing? For a missing to not be adjacent to any known, the missing must be surrounded by missing or array boundaries. The knowns could be isolated? Let's think: If there is at least one missing and at least one known, then there must be some adjacency between known and missing? Not necessarily; consider array: known, known, missing, missing. The pair (2,3): known next to missing -> V gets the known from position 2. So indeed if there is a missing and at least one known, there will be at least one known adjacent to a missing because the array is contiguous; if the missing positions form a block, at the boundary of the block there will be a known neighbor (unless the block covers the whole array). So V empty only when there are no known values adjacent to any missing, i.e., when the array consists entirely of missing (all -1). Or when there are no known at all? But there must be at least one missing, but could there be zero known? Yes, all entries are -1: then no known values, V empty. So we need to handle that case separately.
Thus for V non-empty, we need to compute min over k (in allowed range) of max_{v∈V} |k-v|. Then answer m = max(M0, that min). And we also need to output a specific k achieving it.
Now consider constraints: k must be between 0 and 1e9 inclusive. V values also within 0..1e9. The best k (midpoint) might be fractional, but we need integer k. The min max distance will be ceil( (max(V) - min(V)) / 2 ). But we must check if the optimal integer k can achieve that.
Let minV = min(V), maxV = max(V). The interval that covers V with radius D is [minV, maxV]? Actually, if we choose k, the maximum distance to V is max( |k - minV|, |k - maxV| ). So for given k, the max distance is essentially the larger of distance to extremes. To minimize that maximum, we set k such that these distances are as balanced as possible: ideally k = (minV + maxV) / 2. Then max distance = max( k - minV, maxV - k ) = (maxV - minV) / 2 if the sum is even, else it's ceil((maxV - minV)/2) because the midpoint is not integer, one side distance is floor((maxV-minV)/2) and the other is ceil((maxV-minV)/2), so max = ceil((maxV-minV)/2). More precisely, if we let d = maxV - minV. Then the minimal possible max distance (over real k) is d/2. Over integer k, the minimal possible max distance is ceil(d/2). Because we can choose integer k = floor((minV+maxV)/2) or ceil((minV+maxV)/2). Then the distances: Let k0 = floor((minV+maxV)/2). Then |k0 - minV| = (d + (minV+maxV mod 2?))? Let's compute: if d is even, minV+maxV is even, midpoint integer. Then k = (minV+maxV)/2 yields distances = d/2 each. So max = d/2. If d is odd, then (minV+maxV) is odd. Let k = floor(mid) = (minV+maxV-1)/2. Then distances: k - minV = (maxV - minV -1)/2 = (d-1)/2, maxV - k = (maxV - (minV+maxV-1)/2) = (maxV - minV +1)/2 = (d+1)/2. So max = (d+1)/2 = ceil(d/2). Similarly for ceil. So indeed min_max = ceil(d/2). So D_opt = ceil( (maxV - minV) / 2 ).
But we also have the bound that k must be within [0, 1e9]. If the optimal integer k from above (midpoint) falls outside [0, 1e9], we might need to push k to the boundary, which could increase D_opt. However, note that minV and maxV are in [0, 1e9] (since known values are non-negative). The midpoint of minV and maxV will also be in [0, 1e9] because it's between them. So it automatically lies within the allowed range. But wait: Could the midpoint be outside? If minV=0 and maxV=1e9, midpoint=5e8, within range. So it's always within. So we don't have to worry about the bound for that. However, consider case where V is empty? Then the expression for D_opt doesn't apply. We'll handle separately.
But is it always optimal to only consider V? Let's double-check if there are any other constraints that could affect m. For pairs where both are known, we have fixed M0. For pairs where one is known and the other missing, we only consider |k - v|. But what about pairs where the known value appears multiple times? No issue.
But is it possible that for some pair where both are missing, the difference is 0, so doesn't affect.
Thus the answer seems straightforward: compute M0 = max over i where a_i != -1 and a_{i+1} != -1 of |a_i - a_{i+1}|. Compute minV = min of known values that have at least one adjacent missing neighbor? Actually we need to collect all known values that are adjacent to any missing. But careful: For a known value v, if both its neighbors are known (i.e., not missing), does it ever appear in any pair that involves a missing? It would not. So such known values are not in V. So we must include known values that are adjacent to missing.
But also consider the case where there is a known value that appears in two pairs: one with left neighbor missing, one with right neighbor missing. That's fine; we still only need its value once in V? Since max over v of |k - v| considers each v only once, and it's the maximum of distances to these known values. Including duplicate values doesn't change maximum.
So we need to collect set (or just min and max) of all known values that have at least one neighbor missing.
But is it enough to consider only the extremes (minV and maxV) to compute D_opt? Since max distance from k to set V is max( |k - minV|, |k - maxV| ), as the farthest points are always the extremes. Indeed, for any set on line, the maximum distance to a point is determined by the extreme points. So we only need minV and maxV of V. But careful: V may be empty (if no known adjacent to missing). In that case D_opt can be considered 0? Because there are no constraints from such pairs; we can choose any k, and the maximum difference involving missing-known pairs is 0? Actually if V is empty, there are no pairs with exactly one known and one missing. The only pairs that involve missing are both missing (difference 0) and both known (fixed M0). So then m = M0. But we also need to output some k. Since no constraints on k, we can choose any k, say 0. However, note the requirement: k must be such that after replacement, maximum absolute difference equals m. If we choose any k, does it guarantee that no new differences larger than M0 appear? Since there are no known adjacent to missing, any missing replaced with k are only adjacent to missing (since both missing? Wait if V empty, meaning no known adjacent to missing. That means for every missing position, all its neighbors are also missing? Or possibly a missing at boundary? Let's analyze: For V to be empty, there must be no pair with exactly one known. That means that for every adjacent pair, either both are known or both are missing. So the array is composed of contiguous segments of known values and contiguous segments of missing values. If there is a missing segment, it must be isolated: its boundaries must be either array ends or adjacent to known? Actually if a missing segment is adjacent to a known value at its left or right, then that adjacent pair (missing, known) would exist, contradicting V empty. So missing segments can only be at array boundaries? Let's think: If there is a missing element at position i, then for it to not be adjacent to any known, both neighbors (if exist) must be missing. Because if i has left neighbor i-1 and that neighbor is known, then pair (i-1,i) is known-missing. So to avoid that, if i is missing and has a neighbor, that neighbor must also be missing. So if there is any missing, then the whole array must be all missing? Because if there is at least one known somewhere else, the missing segment would have to be separated from known by ... but if the missing segment is at the edge of the array, then the boundary neighbor is the array end, not a pair? The pair is defined only for adjacent indices. If the missing segment starts at index 1, then there is no left neighbor, and the right neighbor is index 2. For pair (1,2) to avoid known-missing, index 2 must also be missing. Similarly, if the segment ends at index n, then for pair (n-1, n), n-1 must be missing. So a missing segment at an edge forces that the entire segment from that edge is missing until it maybe meets a known? But if it meets a known at some point, then at that point we have missing-known pair. So the only way to avoid any missing-known pair is that there is no adjacency between known and missing. That means that either there are no missing, or there are no known, or the array is composed of two blocks: one block of known and one block of missing but they are not adjacent? They would have to be separated? In a linear array, if you have both known and missing, there will be a boundary where they meet, creating a known-missing pair, unless the known block is completely isolated from the missing block by ... there is no separation, they are adjacent. So indeed if both exist, there must be at least one adjacency. Therefore V empty if and only if there are no known values (all missing) or no missing (but problem guarantees at least one missing, so only case is all missing). Or also maybe if there are known but they are not adjacent to any missing because missing are isolated by being at positions that are not neighbors? That's impossible because if there is a known and a missing, by pigeonhole they must be adjacent somewhere? Actually consider n=3: [ -1, 5, -1 ]. Pair (1,2): missing-known => V includes 5. So not empty. n=4: [5, -1, -1, 5]. Pair (1,2): known-missing => includes 5. So yes any missing with any known will produce adjacency unless the array is all missing. So V empty only when all elements are -1. (Check n=2: [-1,-1] -> both missing, no known, so V empty). So that's the only case.
Thus we can handle that case separately: if there are no known numbers (all -1), then M0 = 0 (since there are no pairs with both known, but there are pairs? Actually all pairs are missing-missing, differences after replacement become |k - k| = 0, so maximum is 0). So m = 0. And we can choose any k, e.g., 0. But output: "0 0"? In example 4, they output "0 42" for n=2, -1 -1. They chose k=42. So any k is fine. So we can choose e.g., 0 or 42. But we must choose k within 0..1e9; both okay. So we can output 0 0 (or 0 42). Usually they output 0 42 for that test, but any works. In example 5: "2\n0 -1" output "0 0". So they picked 0. So we can choose 0.
Thus general solution: For each test case:
- Scan the array.
- Compute M0 = max_{i=1}^{n-1} if a_i != -1 and a_{i+1} != -1, then |a_i - a_{i+1}|. else ignore.
- Build list of values of known numbers that are adjacent to any -1. Specifically, for each i from 1 to n:
if a_i == -1:
continue;
else:
if (i > 1 and a_{i-1} == -1) OR (i < n and a_{i+1} == -1):
then include a_i in V.
- If V is empty (i.e., no known values adjacent to missing) then answer m = M0, and choose k arbitrarily, say 0 (or maybe something else). But careful: Actually if V empty, then there are no known numbers adjacent to missing, which as argued implies all entries are missing? Could there be scenario with some known but no missing adjacent? As argued, if there is any known and any missing, there will be an adjacency. So V empty => all missing. So then M0 = 0 because no both known pairs. So m = 0. But let's still compute generically: if V empty, answer = M0. Since M0=0, answer 0. k arbitrary.
- Otherwise, V non-empty. Compute minV = min(V), maxV = max(V). Then optimal D = ceil( (maxV - minV) / 2 ). However, final m = max( M0, D ). And we need to output m and a k that achieves m. For k, we can choose any integer k that satisfies max( |k-minV|, |k-maxV| ) ≤ D? Actually we want to achieve the overall m = max(M0, D). Since D is the minimal possible max distance for the V part, and we need to pick k such that the max distance to V is at most m (since m must be at least D). So we need k such that max(|k - minV|, |k - maxV|) ≤ m. Since m ≥ D, there will be an interval. Also we might want to pick k that yields exactly m? Actually the problem asks: "Make sure that after replacing all the missing elements with k, the maximum absolute difference between adjacent elements becomes m." So we need to output a k such that the actual maximum absolute difference equals exactly the minimized m. That is, we need to choose k that attains m, which is the minimal possible. So we must ensure that the maximum over all differences is exactly m, not just ≤ m. But if we pick k that yields max distance to V exactly equal to m (or maybe less) and M0 is exactly m, overall max is m. If both are less than m, then the maximum would be less than m, contradicting the statement that m is the minimum possible. Actually if we achieve m as the minimized value, then there is no way to get less than m. But if we pick k that gives max distance to V less than m, but M0 = m, then overall max = m. That's fine. If M0 is less than m, then we need max distance to V to be exactly m, because otherwise m would not be the maximum? Actually suppose M0 = 5, D_opt = 4, then m = max(5,4)=5. The minimal possible maximum is 5. We can choose k such that max distance to V is, say, 3, then overall max = max(5,3)=5. So that's fine; the maximum equals m=5. So we just need to ensure overall max = m. It can be that one component is exactly m and the other less or equal. It's okay.
But we must be careful: If we compute D_opt = ceil((maxV-minV)/2) and let m = max(M0, D_opt). Then we can find k that gives max distance to V at most m. In fact any k in the interval [minV - m? Actually we need max(|k-minV|,|k-maxV|) ≤ m. This describes k in [minV - m, maxV + m] intersected with constraints. But since m ≥ D_opt, the interval for k is non-empty and includes the midpoint region. We can choose, e.g., k = (minV + maxV) // 2 (floor) or ceil, or any integer in that interval.
But we must also consider that k must be between 0 and 1e9. Since V values are within [0, 1e9], and m is at most something (maxV-minV possibly up to 1e9), the interval [minV - m, maxV + m] may go beyond [0, 1e9]. But we can pick an integer inside the intersection with [0, 1e9]. Since minV and maxV are within [0, 1e9], and m is at most something like max(M0, (maxV-minV)/2). The interval that satisfies max distance ≤ m includes [minV, maxV] extended by m. The midpoint is within [minV, maxV] which is inside [0, 1e9]. So we can pick the midpoint (or near midpoint) and it will be within bounds.
Thus, we can output k = floor( (minV + maxV) / 2 ) or ceil.
But wait: Could there be a scenario where M0 is high and D_opt is low, and the minimal m is M0, but we still need to pick k such that the max distance to V is ≤ M0, but maybe the interval for k with that condition is not entirely within [0, 1e9]? Possibly, but if minV - M0 < 0 or maxV + M0 > 1e9, we need to ensure we pick k within bounds. But since we can choose k anywhere that satisfies the condition, we just need to check if there exists any integer k in [0, 1e9] satisfying max(|k-minV|,|k-maxV|) ≤ m. Since m = max(M0, D_opt) and D_opt = ceil((maxV-minV)/2). If M0 is very large, maybe larger than D_opt, then the condition is easier because we have larger m. But if M0 is huge, the interval may extend beyond [0, 1e9], but we only need existence. We must guarantee that the minimal m can be achieved with some k within 0..1e9. Is it always possible? Let's check edge cases: If minV = 0, maxV = 1e9, then D_opt = ceil(1e9/2) = 500000000 (or 500000000 if 1e9 even? Actually 1e9 is 1,000,000,000, even? 1,000,000,000 is even? 1e9 = 1000000000, which is even because ends with 0, yes even. So D_opt = 500,000,000 exactly. So m = max(M0, 500,000,000). If M0 is <= 500,000,000, then m = 500,000,000. Then we need k such that max(|k-0|, |k-1e9|) ≤ 500,000,000. The interval for k is [ -500,000,000, 1,500,000,000] intersect with [0,1e9] gives [0,1e9]. Actually condition: |k-0| ≤ 500,000,000 => k ≤ 500,000,000; |k-1e9| ≤ 500,000,000 => k ≥ 500,000,000. So the only k that satisfies both is k = 500,000,000 exactly. That's within [0,1e9]. So okay.
If M0 is larger, say M0=800,000,000, then m=800,000,000. Condition: k ≤ 800,000,000 and k ≥ 1e9 - 800,000,000 = 200,000,000. So k ∈ [200M, 800M]. That's non-empty and within [0,1e9]. So fine.
If minV is not at 0 and maxV not at 1e9, but still within range, the interval for k will be within [minV - m, maxV + m]. But m can be up to maybe 1e9? Possibly. However, if m is huge, the interval may include negative numbers or exceed 1e9, but we can pick any integer in the intersection. Since we know there exists at least one k in [0,1e9] that satisfies, because the constraints of k do not restrict the existence of an optimal solution? Actually, the problem requires k in [0,1e9]. Could there be a scenario where the optimal m requires k outside [0,1e9]? Let's think. The optimal m is defined as the minimum possible m given that k must be in [0,1e9]. So if the mathematical optimum without bound would require k outside the range, the best achievable within range may have larger m. So we cannot assume we can pick the midpoint if it's outside range. But as argued, the midpoint always lies between minV and maxV, which are within 0..1e9, so it's within range. So for D_opt, we can pick the midpoint. So the minimal m is not affected by the bound. However, if the optimal m is M0 > D_opt, then we need to pick k such that max distance ≤ M0. The existence of such k within [0,1e9] is guaranteed? Not necessarily; consider extreme: M0 is something that forces a range that may not intersect [0,1e9]? Let's analyze: We need to find k in [0,1e9] such that max(|k-minV|,|k-maxV|) ≤ m. Equivalent to k ∈ [minV - m, maxV + m] ∩ [0, 1e9]. For a given m (which is the answer we propose as the minimal possible), there must exist some k in that intersection, because otherwise m wouldn't be achievable. But is it possible that the theoretical minimal m (as per our formula) cannot be achieved due to k bound, forcing a larger m? In that case our computed m would be too low. So we must check: Could the bound on k affect the minimal m? Possibly if the only k that achieves the optimal max distance D_opt is outside the range, and the best k within range yields a larger max distance. However, as reasoned, for D_opt (the minimal max distance to V), the optimal k is any point in [ (minV+maxV)/2 - epsilon? Actually the set of minimizers is the interval of length (if d even, single point at midpoint; if d odd, interval of integer points? Actually integer k: For d odd, there are two integer points that achieve max distance = ceil(d/2): floor(mid) and ceil(mid). Both are within [minV, maxV] because the midpoint lies strictly between them; floor(mid) is >= minV? Let's check: minV=0, maxV=1, d=1. Mid=0.5. floor=0, ceil=1. Both are within [0,1]. So they are within the range of V. Since V values are within [0,1e9], these k are also within [0,1e9]. So D_opt can be achieved with k in range.
Now, if M0 > D_opt, the minimal m is M0. But we must check whether there exists k such that max(|k-minV|,|k-maxV|) ≤ M0, and also that k ∈ [0,1e9]. Since M0 ≥ D_opt, the set of k satisfying the distance condition is [minV - M0, maxV + M0]. This interval definitely contains the previous optimal interval, specifically it contains the midpoint. So it contains points within [0,1e9] because the midpoint is within [0,1e9]. So again, there is feasible k.
Thus the bound on k does not restrict the minimal achievable m because the optimal k is always within the allowed range. So we can ignore the bound for the optimization.
Thus the algorithm stands: compute M0, minV, maxV from V, then m = max(M0, ceil((maxV-minV)/2)). Output m and a suitable k.
Now, one nuance: Are we sure that V only includes known values adjacent to missing? Let's double-check with an example: Suppose a = [1, -1, 2]. Adjacent pairs: (1,2): |1 - k|; (2,3): |k - 2|. So V should contain both 1 and 2. And indeed we include both because they are known and adjacent to missing. Our method includes a_i if it is known and ( (i-1 exists and missing) OR (i+1 exists and missing) ). So for i=1: a_1=1, i-1 doesn't exist, i+1 is -1 => include. i=3: a_3=2, i+1 doesn't exist, i-1 is -1 => include. So minV=1, maxV=2, d=1, ceil(0.5)=1? Actually (2-1)/2=0.5, ceil=1. So D_opt=1. m = max(M0, 1). M0: pairs both known? (1,3) not adjacent. So M0=0. So m=1. That seems plausible: can we achieve m=1? k=1 gives array [1,1,2]: differences |1-1|=0, |1-2|=1 => max=1. k=2 gives [1,2,2]: differences |1-2|=1, |2-2|=0 => max=1. So yes. But note if we choose k=1.5 not integer? Not allowed, but integer yields max=1. So correct.
Now consider a more complex case: Suppose a = [ -1, 10, -1, 12, -1 ] (example 1). Known values: 10 (adjacent to -1 on left? Actually at index2: neighbors index1=-1, index3=-1 => both missing => include 10. Index4: 12 neighbors index3=-1, index5=-1 => include 12. Also maybe index? So V = {10,12}. minV=10, maxV=12, d=2, ceil(1)=1? Actually (12-10)/2=1, so D_opt=1? Ceil of 1 is 1. M0: both known? There are no adjacent pairs with both known because indices 2 and 4 are not adjacent. So M0=0. So m = max(0,1)=1. Output k=11 (midpoint). This matches example.
Now example 2: [ -1, 40, 35, -1, 35 ]. Known: index2=40 (neighbors: index1=-1, index3=35 (known) => but because neighbor index1 is missing, include 40). index3=35 (neighbors: index2=40 (known), index4=-1 => include 35 because neighbor is missing). index5=35 (neighbors: index4=-1 => include). So V = {40,35,35} => min=35, max=40, d=5, ceil(2.5)=3? Actually (40-35)=5, /2=2.5, ceil=3. So D_opt=3. M0: both known pairs: (2,3): |40-35|=5, (3,5) not adjacent? Actually (3,4): 35 and -1 (one missing) so not both known. (4,5): -1 and 35 not both. So only both known pair is (2,3) gives 5. So M0=5. So m = max(5,3)=5. So D_opt=3 but m=5. Output k=35 (since minV=35, maxV=40, D_opt=3, but m=5). They output "5 35". That matches.
Check: With k=35, array becomes [35,40,35,35,35]? Actually: -1 replaced by 35 -> a1=35, a2=40, a3=35, a4=35, a5=35. Differences: |35-40|=5, |40-35|=5, |35-35|=0, |35-35|=0 => max=5. So m=5. Could we get m=4? Let's try k=36: [36,40,35,36,35]? Actually careful: replace missing at indices 1,4 with 36: a=[36,40,35,36,35]. Differences: |36-40|=4, |40-35|=5, |35-36|=1, |36-35|=1 => max=5. So m=5. k=37: [37,40,35,37,35] => |37-40|=3, |40-35|=5, |35-37|=2, |37-35|=2 => max=5. So m=5. So indeed minimal m=5, due to fixed pair (40,35). So example matches.
Now example 3: [ -1, -1, 9, -1, 3, -1 ]. Known: index3=9 (neighbors: index2=-1, index4=-1 => include), index5=3 (neighbors: index4=-1, index6=-1 => include). V={9,3}, min=3, max=9, d=6, ceil(3)=3. M0: both known pairs? Indices 3 and 5 are not adjacent (3-4 is 9 and -1, 4-5 is -1 and 3, 2-3 is -1 and 9, etc.) So no both known. So M0=0. m=3. They output 3 and k=6. Midpoint of 3 and 9 is 6; (9-3)/2=3. So D_opt=3, m=3. Good.
Example 4: all -1 => V empty, M0=0, m=0, k=42 (any). Good.
Example 5: [0, -1] n=2. Known: index1=0, adjacent to missing? neighbor index2=-1 => include 0. So V={0}, min=0, max=0, d=0, D_opt=0. M0: both known? Only pair (1,2): one missing => not both known. So M0=0. m=0. k must be such that max distance to V=0 => k=0. Output 0 0. Works.
Example 6: [1, -1, 3, -1]. Known: index1=1 (neighbor index2=-1 => include), index3=3 (neighbors: index2=-1, index4=-1 => include). V={1,3}, min=1, max=3, d=2, ceil(1)=1. M0: both known? pair (1,2) one missing, (2,3) one missing, (3,4) one missing. So no both known. So m=1. They output 1 and k=2. Midpoint is 2. Works.
Example 7: [1, -1, 7, 5, 2, -1, 5]. Known: index1=1 (neighbor index2=-1 => include). index3=7 (neighbors: index2=-1, index4=5 known but also has neighbor index2 missing => include). index4=5 (neighbors: index3=7 known, index5=2 known; but does it have any missing neighbor? index4's neighbors: index3 (known), index5 (known). No missing. So we should NOT include 5? Wait, check condition: we include known if it has at least one adjacent missing. So index4=5 does not have missing neighbor. So not in V. index5=2 (neighbors: index4=5 known, index6=-1 missing => include 2). index7=5 (neighbor index6=-1 => include 5). So V = {1,7,2,5}? Actually index1=1, index3=7, index5=2, index7=5. That's V. minV=1, maxV=7, d=6, ceil(3)=3. M0: both known pairs: check all adjacent pairs: (1,2): 1 and -1 => not both known. (2,3): -1 and 7 => not both. (3,4): 7 and 5 => both known! So |7-5|=2. (4,5): 5 and 2 => both known? Actually a4=5 (known), a5=2 (known) => yes, |5-2|=3. (5,6): 2 and -1 => not both. (6,7): -1 and 5 => not both. So M0 = max(2,3) = 3. So m = max(3,3) = 3. They output 3 and k=4. Let's verify: With k=4, array becomes [1,4,7,5,2,4,5]. Differences: |1-4|=3, |4-7|=3, |7-5|=2, |5-2|=3, |2-4|=2, |4-5|=1 => max=3. So m=3. Could we achieve m=2? Maybe not. So works.
Our algorithm gives m = max(M0, ceil((maxV-minV)/2)) = max(3, ceil(6/2=3)) = 3. And we need a k that yields m=3. Our method might output k = floor((1+7)/2)=4 (since (1+7)/2=4) which is exactly what they output. Good.
Thus solution is consistent with examples.
Now we must verify edge cases thoroughly. One subtlety: When we collect V, we need to include known values that have at least one adjacent missing. However, what about the scenario where a known value appears in a pair that is both known but also adjacent to a missing? That's fine, it's included anyway. But the crucial point: In a pair where both are known, the difference is fixed and does not involve k. So they contribute to M0. But if that known value also has a missing neighbor, it is also in V. So that's fine.
Now consider a scenario: Known value v is adjacent to two missing (like ... -1, v, -1). It will be included once. That's fine.
Now, is it sufficient to only consider the extreme values of V? Because max distance to V from any k is determined by extremes. So D_opt = ceil((maxV-minV)/2). However, what about the requirement that k must be such that the max difference with known values adjacent to missing is exactly max_{v∈V} |k - v|? The m must be the maximum of these and M0. As long as we achieve m >= M0 and m >= max_{v∈V} |k-v|, with equality for at least one of them? Actually m is defined as the maximum over all differences after replacement. So if we compute m = max(M0, D_opt), then we need to output a k such that the overall maximum equals that m. That will be the case if we pick k such that max_{v∈V} |k-v| ≤ m. Since m >= M0, overall max will be max(M0, max_{v} |k-v|). If we pick k such that max_{v} |k-v| is exactly m (or less), then overall max is max(M0, something) which is at most m. But we also need it to be exactly m. If M0 = m, then even if max_{v} |k-v| < m, overall max = m (since M0 = m). That's fine. If M0 < m, then we need max_{v} |k-v| = m to achieve overall max = m. Otherwise, if we pick k that yields max_{v} |k-v| < m, then overall max would be M0 < m, contradicting the claim that m is the minimum possible, because we would have achieved a smaller maximum? Wait, if m is the minimal possible value of the maximum absolute difference, then there is no k that yields overall max < m. So if we pick k such that max_{v} |k-v| < m, and M0 < m, then overall max = max(M0, something) < m, which would be a contradiction because we assumed m is the minimal possible. So that cannot happen if m is truly the minimum. However, when we compute m = max(M0, D_opt), it is indeed the minimal achievable maximum. So if M0 < D_opt, then m = D_opt. And the minimal achievable D (the minimal possible max distance to V) is D_opt. So any k achieving D_opt yields max_{v} |k-v| = D_opt. So overall max = D_opt = m. If we pick k that gives max_{v} |k-v| > D_opt, then overall max > m. So we need to pick k that attains exactly the minimal max distance to V. So we need to pick k from the set of k that minimizes max_{v} |k-v|. That's the midpoint(s) described. So for the case M0 < D_opt, we must pick k that gives max distance exactly D_opt. For the case M0 > D_opt, we can pick any k that satisfies max_{v} |k-v| ≤ M0. But we may as well pick the midpoint (or any convenient k) because the maximum will be M0 (since M0 is the larger). However, we must ensure that the max_{v} |k-v| is indeed ≤ M0, which is true for the midpoint because max_{v} |mid - v| = D_opt ≤ M0. So midpoint works. In case M0 == D_opt, either is fine.
Thus we can simply pick k = (minV + maxV) // 2 (or integer division floor) as the candidate. However, we must be careful: In some cases, the floor might not be the optimal minimizing integer if (maxV-minV) is odd; but both floor and ceil give same max distance = ceil(d/2). So we can choose floor. But is it always true that floor((minV+maxV)/2) yields max distance <= m? Let's compute: Suppose minV=0, maxV=3, d=3, ceil(d/2)=2. floor((0+3)/2)=1. Then distances: |1-0|=1, |3-1|=2 => max=2 = D_opt. So good. For maxV=4, minV=1, d=3? Actually 4-1=3, ceil(1.5)=2. floor((1+4)/2)=2, distances: |2-1|=1, |4-2|=2 => max=2. Good. So floor works.
But we must also consider the case when V has only one value: then minV = maxV = v. Then d=0, D_opt=0. Then floor((v+v)/2)=v. So k=v. That works: max distance =0. So it's fine.
Now, what about the possibility that the midpoint is not within [0,1e9]? v in [0,1e9] => midpoint also in [0,1e9]. So fine.
Edge Cases:
-
When V is empty: then we can't compute minV, maxV. In that case, answer m = M0, which should be 0 because all are missing, so M0=0. So we can output k = 0 (or any). But ensure we handle correctly. However, is it possible that V is empty but M0 > 0? As argued, V empty implies all elements are -1, because if any known existed, there would be adjacency. But could there be a scenario with known elements but no known adjacent to missing? As we reasoned, if there is a known and a missing, they are adjacent somewhere because the array is linear. But consider array: [known, known, missing, missing]? The known block ends at index2, missing block starts at index3, then pair (2,3) is known-missing -> V includes known at index2. So V non-empty. So V empty only if all missing. So M0=0. So we can treat V empty as m=0, k arbitrary.
-
However, what if there are known values but all are isolated and there is no missing? But problem guarantees at least one missing, so that case can't happen.
Thus algorithm is straightforward.
Now confirm with some additional tests.
Test: n=2, a=[10, -1]. Then M0=0, V={10}, minV=maxV=10, D_opt=0, m=0. k=10. Check: array becomes [10,10] differences 0. So yes, m=0. But is that truly the minimum? Yes, we can set k=10. That's allowed. So answer (0,10). Good.
Test: n=2, a=[-1, 10]. Same.
Test: n=3, a=[5, -1, 6]. M0: pairs: (1,2): 5 and missing -> not both, (2,3): missing and 6 -> not both, so M0=0. V: index1=5 (neighbor missing) include; index3=6 include. minV=5, maxV=6, d=1, ceil(0.5)=1. So m=1. k = floor((5+6)/2)=5? Actually (5+6)/2=5.5 floor=5 => then array [5,5,6]: differences: |5-5|=0, |5-6|=1 => max=1. So m=1. Good. Could we get m=0? Only if k such that |k-5|=0 and |k-6|=0 simultaneously, impossible. So correct.
Test: n=3, a=[100, -1, 0]. V={100,0}, min=0, max=100, d=100, ceil(50)=50. M0=0. m=50. k=50. Check: [100,50,0]: differences: |100-50|=50, |50-0|=50 => max=50. Could we do better? Maybe k=51 gives max(|100-51|=49,|51-0|=51) => 51; k=49 gives 51? Actually |49-0|=49, |100-49|=51 => 51. So best is 50. Good.
Now consider scenario with M0 dominating: a=[100, 0, -1] . V: index2=0 (neighbor index3 missing) include; also index1? index1=100 has neighbor index2 known? No missing adjacent? Check: index1's neighbors: only index2 (0) which is known, so not missing. So V={0}. So minV=maxV=0, D_opt=0. M0: pairs both known: (1,2): |100-0|=100. So M0=100. m = max(100,0)=100. k any such that max(|k-0|) <=100. k=0 works: array [100,0,0] => differences: |100-0|=100, |0-0|=0 => max=100. So answer m=100, k=0. Could we achieve m=99? With k=1: array [100,0,1]? Actually replace missing index3 with 1: [100,0,1] differences: |100-0|=100, |0-1|=1 => max=100. So still 100. So minimal is 100. Good.
Now consider scenario where V empty but not all missing? Actually impossible. But just in case.
Now, we also need to be careful: In some arrays, there might be a known value that is adjacent to a missing value, but that missing value is also adjacent to another known, so V includes both. That's fine.
Now, we must confirm that the maximum absolute difference after replacement is indeed exactly max( M0, max_{v∈V} |k-v| ). But is there any other kind of pair that could produce a difference that is not captured? What about pairs where both are missing? That gives 0. So no. So it's correct.
Thus solution per test case: O(n) to compute M0, minV, maxV.
Implementation details:
- Input t.
- For each test case:
-
Read n.
-
Read array a[0..n-1] (or 1-index).
-
Initialize M0 = 0.
-
Initialize minV = INF, maxV = -INF (or large sentinel).
-
Also need a boolean flag to know if we encountered any V values.
-
Loop i from 0 to n-1:
If a[i] == -1: continue.
// Check neighbors for missing:
bool adjacent_to_missing = false;
if i > 0 and a[i-1] == -1: adjacent_to_missing = true.
if i < n-1 and a[i+1] == -1: adjacent_to_missing = true.
If adjacent_to_missing:
minV = min(minV, a[i]);
maxV = max(maxV, a[i]);
-
Also compute M0: loop i from 0 to n-2:
if a[i] != -1 and a[i+1] != -1:
diff = abs(a[i] - a[i+1]);
M0 = max(M0, diff).
-
After that, if minV is still INF (i.e., no V values), then // all missing
answer_m = 0
answer_k = 0 (or any)
-
else:
d = maxV - minV
D_opt = (d + 1) // 2 // integer ceil division: (d+1)//2
m = max(M0, D_opt)
k = (minV + maxV) // 2 // floor of average (could also be (minV+maxV+1)//2 if we prefer)
// But note: if we pick floor, it's fine. However, ensure that with this k, the max distance to V is indeed <= m. Since m >= D_opt, and k's max distance is either D_opt (if d even or odd? Actually floor gives max distance = ceil(d/2) = D_opt. So max distance = D_opt. So overall max = max(M0, D_opt) = m. Good.
// However, if M0 > D_opt and we pick k = floor average, max distance = D_opt, which is less than m, overall max = M0 = m. So okay.
// So k = (minV+maxV)//2 works.
-
Output m and k.
Edge Cases to double-check: d might be odd, (minV+maxV)//2 gives floor, max distance = ceil(d/2) = (d+1)//2 = D_opt. So that's fine.
Now, check with example 2: minV=35, maxV=40, d=5, D_opt=(5+1)//2=3, k = (35+40)//2 = 75//2=37? Actually integer division floor yields 37. But in example they output k=35, not 37. So 37 is also valid? Let's test with k=37: array becomes [37,40,35,37,35] differences: |37-40|=3, |40-35|=5, |35-37|=2, |37-35|=2 => max=5. m=5, works. So k=37 also gives m=5. So any k in [35? Actually condition: max(|k-35|,|k-40|) ≤ 5? Actually m=5, we need max distance ≤5? Actually m=5, we need overall max ≤5? But we want exactly 5? If max distance = something less, still overall max = M0=5, so fine. So k=37 gives max distance: |37-35|=2, |40-37|=3, both ≤5, so ok. So k=37 is fine. But they output 35. So both acceptable. Our algorithm using floor average gives 37, which is valid. So that's okay.
Now test with odd case: minV=0, maxV=1, d=1, D_opt=1, k = (0+1)//2 = 0. That yields array maybe? Example: [0, -1, 1]? Actually if we had such V, we'd output k=0. But is that optimal? Let's test: suppose a = [0, -1, 1] (n=3). M0? (0,-1) not both, (-1,1) not both -> M0=0. V={0,1}. min=0, max=1, d=1, D_opt=1. k=0 gives array [0,0,1] => differences: |0-0|=0, |0-1|=1 => max=1. k=1 gives [0,1,1] => diff: |0-1|=1, |1-1|=0 => max=1. So both fine. So our output is fine.
But what if we have a case where minV=0, maxV=2, d=2, D_opt=1, k = (0+2)//2=1. Works.
Now, one more nuance: The problem says: "If there is more than one possible k, you can print any of them." So we can output any valid k that yields the minimal m. So our chosen k is valid.
Now confirm with example 7: minV=1, maxV=7, d=6, D_opt=3, k=(1+7)//2=4. Matches output.
Now, we also need to ensure that when V is empty, we output some k. In example 4, they output 42. So we can output 0, but it's okay. Since they allow any, but in the sample they used 42. However, problem statement: "If there is more than one possible k, you can print any of them." So we can output 0. But to be safe, we might output 0 or 42? 0 is valid. But let's test: all -1, any k works. So 0 works.
But there is a subtle point: The array a is non-negative integers. If we set k=0, it's within bounds. So fine.
Now, we need to verify that our algorithm is correct for all cases, especially those where there are multiple missing segments and known values. It seems correct.
But is there any scenario where the maximum difference might be influenced by a known value that is not directly adjacent to missing, but indirectly through a chain? No, because only adjacent pairs matter.
Thus the solution is straightforward.
Now, before finalizing, let's consider potential pitfalls: The set V includes known values that have at least one adjacent missing. But what about a known value that is adjacent to a missing on both sides? It's included. That's fine.
But what about a known value that is at the boundary of the array and adjacent to missing inside? For example, n=3: [ -1, 5, 10 ]. Known 5 has neighbor -1 (left) so included. Known 10 has neighbor 5 (known) and no missing neighbor (right boundary is not considered), so not included. That's correct because pair (5,10) is both known and its difference is accounted in M0. The missing element at index1 replaced with k interacts only with index2 (5), not with index3 (10). So only 5 matters for |k-5|. So V should not include 10. Good.
Now, check M0 includes pair (5,10) if both known: yes. So algorithm yields: M0 = |5-10|=5, V={5}, minV=maxV=5, D_opt=0, m=max(5,0)=5, k=(5+5)//2=5. So array becomes [5,5,10] diff: |5-5|=0,|5-10|=5 => max=5. Could we do better? If we set k=7, array [7,5,10] diff: |7-5|=2, |5-10|=5 => max=5. If k=8: |8-5|=3, |5-10|=5 => max=5. So m=5. So correct.
But wait: Could we set k=10? Then [10,5,10] diff: |10-5|=5, |5-10|=5 => max=5. So still 5. So minimal m=5. Good.
Now consider scenario where a known value has multiple missing neighbors, but the max distance to it might be dominated by extremes. But our approach using extremes covers it.
Now think about scenario where V includes two values far apart, but also M0 is huge. That's fine.
Now, can there be a case where the optimal k is not the midpoint because the M0 might force k to be something else? For instance, if M0 is very high but we also need to keep max distance to V ≤ M0. That interval may be large, but the midpoint works. So it's fine.
Now check constraints: sum n ≤ 4e5, t ≤ 1e4. So O(n) per test case is fine.
Now, we must be careful about integer types: a_i up to 1e9, differences up to 1e9. So use 64-bit int (long long) to avoid overflow in intermediate, but actually differences up to 1e9 fits in 32-bit signed int (2e9). But we might compute (minV+maxV) up to 2e9, which fits in 32-bit signed? 2e9 is borderline (max int is about 2.147e9). So safe to use 64-bit (long long) for safety. In Python, integers are arbitrary precision, so fine.
Now, code structure:
for each test case:
read n
read list a
M0 = 0
minV = 10**18 (or None)
maxV = -1
haveV = False
for i from 0 to n-2:
if a[i] != -1 and a[i+1] != -1:
M0 = max(M0, abs(a[i] - a[i+1]))
for i from 0 to n-1:
if a[i] == -1:
continue
# check adjacency
if (i > 0 and a[i-1] == -1) or (i < n-1 and a[i+1] == -1):
if not haveV: minV = a[i]; maxV = a[i]; haveV = True
else:
if a[i] < minV: minV = a[i]
if a[i] > maxV: maxV = a[i]
if not haveV:
m = M0 # should be 0
k = 0
else:
d = maxV - minV
D_opt = (d + 1) // 2
m = max(M0, D_opt)
k = (minV + maxV) // 2
print(m, k)
But careful: M0 could be > 0 even if haveV is false? As argued, haveV false implies all missing => M0=0. So m = 0. So it's fine.
Now, test with some custom:
Case: n=4, a=[5,-1,-1,5]. V: index1=5 (neighbor index2=-1) include; index4=5 (neighbor index3=-1) include. So minV=5, maxV=5? Actually both 5. So minV=maxV=5, d=0, D_opt=0. M0: pairs: (1,2): 5 and -1 not both; (2,3): both missing; (3,4): -1 and 5 not both. So M0=0. m=0. k=5. Array becomes [5,5,5,5] diff 0. So correct.
Case: n=4, a=[10,-1,-1,0]. V: index1=10 (adjacent to missing) include; index4=0 include. minV=0, maxV=10, d=10, D_opt=5. M0=0. m=5, k=5. Array: [10,5,5,0] diff: |10-5|=5, |5-5|=0, |5-0|=5 => max=5. Could we get m=4? k=5 gave max=5. k=6: [10,6,6,0] diff: |10-6|=4,|6-6|=0,|6-0|=6 => max=6. k=4: [10,4,4,0] diff: |10-4|=6,|4-4|=0,|4-0|=4 => max=6. So 5 is optimal.
Now, consider an edge case where V has two values, but the optimal k based on minV and maxV might cause a large difference with another known that is not in V but appears in a pair with a missing? Wait, if a known is not in V, it means it has no missing neighbors. So its only adjacent pairs are with known numbers, which are already considered in M0, or with missing? Actually if a known has no missing neighbors, all its adjacent elements are known (or it's at boundary but the neighbor is known). So its value does not affect any missing-adjacent pair. So no need to consider it for k.
Thus algorithm stands.
Now, ensure correctness regarding the handling of boundaries: For i=0 (first element), check if i>0? For adjacency, we check left neighbor if exists and -1, and right neighbor if exists and -1. So correct.
Now, what about a scenario where a known value is adjacent to missing on both sides, and also appears in a both-known pair? That's okay.
Now, confirm with a tricky case: The array might have many known values, and the missing elements only appear in a block. For example: n=5: a=[1,2,-1,3,4]. V: known values adjacent to missing: index2=2? Let's check: indices:1:1 (neighbors: index2=2 known, no missing) -> not in V. index2=2 (neighbors: left 1 known, right -1 missing) -> include 2. index3 missing. index4=3 (neighbors: left -1, right 4 known) -> include 3. index5=4 (neighbor left 3 known, no missing) -> not in V. So V={2,3}. minV=2, maxV=3, d=1, D_opt=1. M0: both-known pairs: (1,2):|1-2|=1, (4,5):|3-4|=1. So M0=1. So m = max(1,1)=1. k = floor((2+3)/2)=2. So array: [1,2,2,3,4] diffs: |1-2|=1,|2-2|=0,|2-3|=1,|3-4|=1 => max=1. Works. Could we achieve m=0? No.
Now consider case where V is empty? Already handled.
Thus algorithm seems correct.
Now, we must confirm that we haven't missed the possibility that the optimal k may not be integer? But k must be integer. So we compute as integer. Our D_opt is ceil(d/2) which is integer. And k integer chosen as floor of average works.
Now, let's prove formally: The objective function f(k) = max( M0, max_{i: exactly one missing} |k - a_i| ). Since M0 is constant, we minimize over k integer in [0,1e9]. The max_{i} |k - a_i| is minimized over integer k by any integer point that minimizes the maximum distance to the set V = { a_i } (the values from those known neighbors). The minimal possible value of that max distance is R = ceil((maxV - minV)/2). And that can be achieved by k = floor((minV+maxV)/2) or ceil. So the minimal value of f(k) = max(M0, R). So it's optimal.
Thus solution is correct.
Now, implementation in Python:
We need to process up to 4e5 total n, t up to 1e4. So O(total n) works.
Be careful about reading input fast due to large input? 4e5 numbers is okay for Python with sys.stdin.read().
We'll implement:
import sys
def solve():
data = sys.stdin.read().split()
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
n = int(next(it))
a = [int(next(it)) for _ in range(n)]
M0 = 0
minV = 10**18
maxV = -1
haveV = False
# compute M0
for i in range(n-1):
if a[i] != -1 and a[i+1] != -1:
diff = abs(a[i] - a[i+1])
if diff > M0:
M0 = diff
# compute V extremes
for i in range(n):
if a[i] == -1:
continue
# check adjacency to missing
if (i > 0 and a[i-1] == -1) or (i < n-1 and a[i+1] == -1):
if not haveV:
minV = a[i]
maxV = a[i]
haveV = True
else:
if a[i] < minV:
minV = a[i]
if a[i] > maxV:
maxV = a[i]
if not haveV:
m = M0
k = 0
else:
d = maxV - minV
D_opt = (d + 1) // 2 # ceil(d/2)
m = M0 if M0 > D_opt else D_opt # max
k = (minV + maxV) // 2
out.append(f"{m} {k}")
sys.stdout.write("\n".join(out))
if name == "main":
solve()
That should be it.
But let's double-check a subtle scenario: Suppose there is a known value that is adjacent to missing, but also there is a missing element that is adjacent to two known values? Actually both known values are in V. So fine.
Now, I'll consider a case that might break: Suppose V contains values that are not extremes but the set V is not just two points, but extremes determine D_opt, which is correct. However, what about if we have V empty but there are some known values and missing values, but we thought that can't happen. Let's confirm mathematically: Suppose array has at least one known and at least one missing. Then there exists at least one index i such that a_i is known and a_{i+1} is missing (or a_{i-1} is missing). Because we can take the leftmost missing or rightmost known. Let's prove: Consider the first known from the left. If it's not at position 1, then its left neighbor may be missing (if it's not position 1, position 1 could be missing). Actually if the first element is known, it may not have left neighbor, but right neighbor could be missing? Not necessarily; if the first element is known and second is also known, then the first known does not have missing neighbor. However, there is at least one missing somewhere. Consider the first missing element; if it's not at position 1, then its left neighbor is known (since it's the first missing, the element before it must be known). So that known neighbor is adjacent to missing. So yes, at least one known is adjacent to a missing. So V is non-empty. Therefore, V empty only if no known at all. Good.
Thus algorithm robust.
Now, edge: n=1? But n≥2 per constraints. So fine.
Now, we also need to consider that there might be known values that are adjacent to missing, but those missing may be filled with k, and the difference between two such k-filled positions? Actually if we have two missing adjacent to each other, the difference is 0. So no effect. So no issue.
Thus solution is done.
Now, before finalizing, let's test with some random brute force for small n to confirm.
We can write a small brute force for verification, but we'll trust reasoning.
One more nuance: The problem says "Dark decided that the gift he is going to give to Motarack is an array a of n non-negative integers." So initial known values are non-negative. But after replacement, we set missing to k (also non-negative). So all entries become non-negative. Good.
Now, about output: we need to output two integers: m and k. Our algorithm does that.
Now, time complexity: O(total n) = 4e5, fine.
Now, potential pitfall: In Python, we should use fast IO for large input. Using sys.stdin.read() is okay.
Now, let's test with provided examples:
Example Input:
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Our algorithm:
Case1:
a = [-1,10,-1,12,-1]
Compute M0: no both known pairs -> 0.
V: index2=10 (neighbors -1 and -1) => include 10; index4=12 (neighbors -1 and -1) include 12 => minV=10, maxV=12, d=2, D_opt=(2+1)//2=1? Actually (2+1)//2 = 1? (2+1)//2 = 3//2 =1, yes. So m=1, k=(10+12)//2=11 -> "1 11". Good.
Case2:
[-1,40,35,-1,35]
M0: (2,3): |40-35|=5 -> M0=5.
V: index2=40 (neighbors -1,35) include; index3=35 (neighbors 40,-1) include; index5=35 (neighbor -1) include -> minV=35, maxV=40, d=5, D_opt=(5+1)//2=3, m=max(5,3)=5, k=(35+40)//2=75//2=37 -> "5 37". But example output "5 35". Both are acceptable. So fine.
Case3:
[-1,-1,9,-1,3,-1]
M0: none ->0.
V: index3=9 (neighbors -1,-1) include; index5=3 include -> min=3, max=9, d=6, D_opt=3, m=3, k=(3+9)//2=6 -> "3 6". Good.
Case4:
[-1,-1] -> haveV false -> m=M0=0, k=0 -> "0 0". But example has "0 42". Still valid.
Case5:
[0,-1] -> M0: none ->0. V: index1=0 include -> minV=0, maxV=0, d=0, D_opt=0, m=0, k=0 -> "0 0". Good.
Case6:
[1,-1,3,-1] -> M0: none ->0. V: index1=1 include; index3=3 include -> min=1, max=3, d=2, D_opt=1, m=1, k=2 -> "1 2". Good.
Case7:
[1,-1,7,5,2,-1,5] -> M0: (3,4): |7-5|=2, (4,5):|5-2|=3 => M0=3. V: index1=1 include; index3=7 include; index5=2 include; index7=5 include -> min=1, max=7, d=6, D_opt=3, m=3, k=4 -> "3 4". Good.
All matches except case2 where we output 37 vs 35, and case4 we output 0 vs 42, both allowed.
Thus solution is correct.
Now, we should consider potential tricky cases:
-
Large values, but we handled.
-
Cases where minV and maxV computed incorrectly because we included known values that are adjacent to missing but the missing are not actually adjacent? No, adjacency condition is correct.
-
Cases where a known value appears multiple times, but we just need extremes.
-
Cases where the array has only one known and multiple missing. E.g., n=5, a=[100,-1,-1,-1,-1]. V: only 100, minV=maxV=100, D_opt=0, M0=0 (since no both known), m=0, k=100. After replacement: [100,100,100,100,100] diff 0. So m=0 indeed. Could we set k to something else and still get max diff 0? Only k=100. So output (0,100) is correct.
-
Cases where there are known at both ends and missing middle. E.g., n=3, [10, -1, 0]. We already did.
Now, consider scenario where M0 is large and V's range is also large, but optimal k might be forced to a specific region to avoid making some specific difference bigger than M0? Actually if M0 is larger than D_opt, we can choose any k that keeps max distance ≤ M0. The set of such k is an interval. However, could it be that for some k in that interval, the actual maximum difference might be less than M0? That would be okay. But we need to ensure that the maximum is exactly m? The problem says: "Make sure that after replacing all the missing elements with k, the maximum absolute difference between adjacent elements in the array a equals m." They want us to output a k that makes the maximum exactly equal to m. However, if we output a k that yields a maximum less than m, then it's not equal to m. Let's re-read the problem statement: "You should print two integers, the minimum possible value of m and an integer k (0 ≤ k ≤ 10^{9}) that makes the maximum absolute difference between adjacent elements in the array a equal to m."
Interpretation: We need to output the minimum possible value of m (call it m_opt), and we also need to output some k that achieves that m_opt (i.e., after replacing missing with that k, the maximum absolute difference is exactly m_opt). So we must output a k such that the overall maximum is exactly m_opt. If we output a k that makes the maximum less than m_opt, that would be wrong because then the maximum is not equal to m_opt. But can that happen? Since m_opt is defined as the minimum possible value of the maximum absolute difference, there cannot exist a k that yields a maximum less than m_opt. So any k that achieves the minimum must yield maximum = m_opt. But could there be a k that yields maximum = some value less than m_opt? No, because m_opt is the minimum, so no k can yield a smaller max. So any k that yields a maximum ≤ m_opt must actually be = m_opt. So if we pick a k that satisfies the conditions (i.e., we choose k to minimize the max, or any k that yields the optimum), the resulting maximum will be exactly m_opt. But wait, is it possible that for some k, the maximum is less than m_opt? That would contradict the minimality of m_opt. So it cannot. However, if we incorrectly compute m_opt too high, then picking a k that yields a lower max would reveal an error. But if m_opt is correct, any k that yields max ≤ m_opt must yield exactly m_opt. So we just need to ensure we pick a k that achieves max ≤ m_opt, then it will be = m_opt automatically.
But is it possible that our computed m_opt is correct, but we pick a k that yields max < m_opt? That would mean m_opt is not actually the minimum, because we found a k with smaller max. So our computed m_opt would be wrong. So we must be careful: Our computed m_opt = max(M0, D_opt). But D_opt is the minimal possible value of max_{v∈V} |k-v|. It is achievable by some k (the midpoints). For those k, max_{v} = D_opt. So overall max = max(M0, D_opt). That's exactly m_opt. So if we choose k = floor average, we get max_{v} = D_opt (if D_opt > 0? Wait, if d even, floor average gives max distance = d/2 = D_opt. If d odd, floor average gives max distance = ceil(d/2) = D_opt. So yes, for that k, max_{v} = D_opt exactly. So overall max = max(M0, D_opt) = m_opt. So that works.
If M0 > D_opt, then for any k that gives max_{v} ≤ M0, overall max = M0. But could it be that for some k, max_{v} < M0, then overall max = M0, so it's exactly m_opt. That's fine. So picking the midpoint works: it yields max_{v}=D_opt ≤ M0, so overall max = M0 = m_opt. So the maximum equals m_opt. So it's good.
Thus our choice k = floor((minV+maxV)//2) yields overall max = m_opt.
Now, consider the case when V empty: we output m=0, k=0. Then array all -1 replaced with 0 => all zeros, max diff=0 = m. So ok.
Thus algorithm correct.
Now, one more nuance: The problem states that k must be between 0 and 10^9 inclusive. Our k might be outside that range if minV and maxV are within 0..1e9, then average is also within that. For example, if minV=0, maxV=1e9, average=500,000,000, inside. So fine.
Now, what about the case where V has values and minV=0, maxV=0? average=0. That's fine.
Now, let's check a random test with brute force for small n (maybe later). But seems solid.
Now, finalize solution.
Before final code, consider potential optimization: It's okay.
Now, edge cases: n up to 100k, sum 400k, reading and processing fine.
Now, confirm memory: storing array of size n for each test case, but we can process on the fly? We need to compute both M0 and V extremes, which require two passes over the array. We can do it in one pass: while reading the array we can compute M0 using previous and next? But easier to just store array (list) since total size 4e5, memory fine.
But we could also process without storing full array? We need to know neighbors to decide if a known value has missing neighbor. For that we need to know left and right neighbor. Could process sequentially, keeping previous and next? For M0, we need adjacent pairs. For V, we need to know for each element if it is missing and if adjacent to known? Actually we need to know for known elements if they have missing neighbor. That requires checking left and right. While iterating, we can know left neighbor from previous element, but right neighbor is not yet known unless we read ahead. Could read entire array. Simpler to read all.
But we could also process in two passes: first read array, then compute. Since sum n 4e5, it's okay.
Now, implement.
Potential pitfalls: Python's recursion not used.
Now, final answer.
I think that's all.
But wait, is there any scenario where we need to consider the absolute difference between a known and a missing when the missing is replaced with k, but also the known is itself missing? No.
Thus final solution is good.
Now, produce final answer.
But I'll also think if there is any nuance with the fact that some known values may appear in V, but the actual maximum difference might be determined not by extremes of V but by some other value because the missing positions are at different indices? For instance, consider: a = [100, -1, 0, -1, 100]. Let's compute. Known: index1=100 (neighbor index2=-1) include; index3=0 (neighbors: index2=-1, index4=-1) include; index5=100 (neighbor index4=-1) include. So V={100,0,100}. minV=0, maxV=100, d=100, D_opt=50. M0: both-known pairs? (1,2) no; (2,3) no; (3,4) no; (4,5) no. So M0=0. So m=50, k=50. After replacement: [100,50,0,50,100] differences: |100-50|=50, |50-0|=50, |0-50|=50, |50-100|=50 => max=50. Could there be a better k? Suppose k=0: [100,0,0,0,100] diff: |100-0|=100, others 0, max=100. k=100: [100,100,0,100,100] diff: |100-100|=0, |100-0|=100, |0-100|=100, |100-100|=0 => max=100. k=51: [100,51,0,51,100] diff: |100-51|=49, |51-0|=51, |0-51|=51, |51-100|=49 => max=51. So 50 is optimal. Good.
Now, consider scenario where there is a known value that is not extreme of V but the difference between k and that known might be larger than extremes? That cannot happen because extremes are the farthest from any point in the interval? Actually if we have V = {0, 100, 101} (so min=0, max=101, d=101, D_opt=51). For k=50, distances: to 0=50, to 100=50, to 101=51 => max=51. That's okay. If we only considered extremes 0 and 101, we would compute max distance = max(|50-0|,|50-101|)=51, same. If we had V = {0, 100, 200} min=0 max=200 d=200 D_opt=100. k=100 gives distances: 0:100, 100:0, 200:100 => max=100. So extremes suffice.
Thus it's correct.
Now, think about case where V is empty? Already.
One more scenario: What if there is a pair where both are known and the absolute difference is huge, say M0 = 100. But V's range