We need to compute for each i, the median of the multiset of N-1 numbers (since N is even, N-1 is odd). The median of odd number l is the (l+1)/2-th largest value, i.e., the element at position (l+1)/2 in sorted order.
Given N up to 200,000, we need to compute each B_i efficiently, ideally O(N log N) or O(N).
We have N even. For each i, we remove X_i. The median of the remaining odd-sized list is the ( (N-1)+1)/2 = N/2-th largest value (since (N-1+1)/2 = N/2). Because N is even, N/2 is an integer. So we need the N/2-th largest element in the multiset after removing one element.
Equivalently, if we sort the original array, we might be able to compute B_i based on whether X_i is less than, equal to, or greater than certain order statistics.
Let's denote the sorted array of all N numbers: Y_1 <= Y_2 <= ... <= Y_N.
We want for each i: median of all X_j for j != i. The size is N-1 odd, median rank = (N-1+1)/2 = N/2. So we need the N/2-th smallest among the N-1 numbers.
If we remove an element, what is the new order statistic?
Consider positions in sorted order. The original sorted array has positions 1..N.
After removing one element, the order of the remaining numbers changes. The N/2-th smallest among the remaining could be either Y_{N/2} or Y_{N/2+1} depending on which element is removed. Because we have N even, the median of the full set (if it existed) would be ambiguous; but here we consider only after removal.
Specifically, the N/2-th smallest among the remaining is:
- If the removed element is at position <= N/2, then we have removed an element from the lower half, so the N/2-th smallest among the remaining will shift to a higher index. More precisely, if we remove Y_k where k <= N/2, then the sorted remaining list consists of all Y_j for j != k. The N/2-th element (1-indexed) in that list: originally the N/2-th element in full list is Y_{N/2}. But after removal, if k <= N/2, then Y_{N/2} might shift down? Let's think:
We have positions 1..N. After removing index k, we re-index. The element that originally was at index m becomes at index m' = m if m < k, and m' = m-1 if m > k. We want the element at index N/2 in the new list (size N-1). So we need to find original index m such that after removal, its new index = N/2.
That is: if m < k, then new index = m. So condition m = N/2 and m < k => k > N/2. But if k > N/2, then m = N/2 is less than k, so unchanged. So if k > N/2, then the element at position N/2 remains Y_{N/2}.
If m > k, new index = m-1 = N/2 => m = N/2+1. So if the removed index k is <= N/2, then the element at original index N/2+1 becomes the new index N/2 (because we removed one before it). So in that case, the median becomes Y_{N/2+1}.
What about the case k = N/2? That is <= N/2, so median = Y_{N/2+1}.
If k = N/2+1? That is > N/2, so median = Y_{N/2}.
What about equality when there are duplicate values? The sorted list Y is non-decreasing. The index positions matter based on sorting with stable order? But we are removing a specific occurrence (by original index i). However, if there are duplicates, which occurrence of the value we remove matters: the removal of a particular instance could affect the ranking if there are multiple equal values. Because when we sort, duplicate values occupy multiple consecutive positions. Removing one of them might affect which position of the median is taken. The above analysis based purely on sorted order positions works if we consider the sorted multiset and we remove one occurrence of a value, but we need to know which sorted index corresponds to the removed element. However, if there are duplicates, multiple indices could have same value. So we need to determine for each original element X_i, its rank in sorted order (with ties broken by some order, but we need to know which position it occupies). Because different occurrences of same value might be considered distinct; removing one of them might shift median differently if the removed occurrence lies on different side of the median threshold.
Thus, we need to compute for each i, the sorted index (position) of X_i in the full sorted array. However, with duplicates, there is an ordering: we can assign positions arbitrarily among equal values, but it must be consistent for all i. Typically, we can sort the pairs (value, original index) and assign ranks 1..N. Then for each i, we know its rank p_i (1-indexed). Then, as argued, the median B_i = Y_{N/2} if p_i > N/2, else B_i = Y_{N/2+1} if p_i <= N/2. But is that always correct? Let's test with examples.
Example 1: N=4, values: 2,4,4,3. Sorted Y: [2,3,4,4] positions 1,2,3,4. N/2 = 2. So Y_2 = 3, Y_3 = 4. According to rule: if p_i > 2 then B_i = Y_2 = 3; if p_i <= 2 then B_i = Y_3 = 4. Compute p_i: sort pairs:
- (2,1) -> rank1
- (3,4) -> rank2 (original index 4)
- (4,2) -> rank3
- (4,3) -> rank4
So:
i=1 p=1 <=2 => B1 = Y_3 = 4 (output 4) correct.
i=2 p=3 >2 => B2 = Y_2 = 3 (output 3) correct.
i=3 p=4 >2 => B3 = Y_2 = 3 (output 3) correct.
i=4 p=2 <=2? p=2 exactly <=2 => B4 = Y_3 = 4 (output 4) correct.
Example 2: N=2, values: 1,2. Sorted: [1,2], N/2=1. Y1=1, Y2=2. Rule: if p_i > 1 then B_i = Y1; if p_i <=1 then B_i = Y2. Compute p_i: (1,1)->1, (2,2)->2. So i=1 p=1 <=1 => B1 = Y2=2 output 2 correct. i=2 p=2>1 => B2 = Y1=1 output 1 correct.
Example 3: N=6, values: 5,5,4,4,3,3. Sorted: [3,3,4,4,5,5]. N/2=3. Y3=4, Y4=4. Both are 4. So irrespective of p_i, B_i will be 4. So rule gives 4 for all.
Now test a case where duplicates straddle the median threshold and removal of one might cause median to be either of the two values, but maybe both are same? Consider N=4, values: 2,2,3,4. Sorted: [2,2,3,4]. N/2=2, Y2=2, Y3=3. Compute p_i: (2,1)->1, (2,2)->2, (3,3)->3, (4,4)->4. Rule:
i=1 p=1<=2 => B1=Y3=3
i=2 p=2<=2 => B2=Y3=3
i=3 p=3>2 => B3=Y2=2
i=4 p=4>2 => B4=Y2=2
Now compute actual:
Remove i=1: remaining [2,3,4] sorted [2,3,4] median index 2 => 3. OK.
Remove i=2: similar =>3.
Remove i=3: remaining [2,2,4] sorted [2,2,4] median index 2 =>2. OK.
Remove i=4: [2,2,3] sorted [2,2,3] median=2. OK.
Now test a more subtle case: N=4, values: 2,2,2,3. Sorted: [2,2,2,3]. Y2=2, Y3=2. Both same, so all medians 2. Rule: N/2=2, Y2=2, Y3=2. So any p_i yields 2. Indeed, remove any one, the remaining three sorted median is the second smallest: For removal of a 2, remaining [2,2,3] median 2; removal of 3, remaining [2,2,2] median 2.
Now test a case where duplicates might cause an issue if the removed element's rank is exactly N/2 and there are duplicates of the same value at positions N/2 and N/2+1. For example, N=4, values: 1,2,2,3. Sorted: [1,2,2,3]. Y2=2, Y3=2. Rule: if p_i <=2 => B_i=Y3=2; if p_i>2 => B_i=Y2=2. So all outputs 2. Let's verify manually:
Remove i=1 (value 1): remaining [2,2,3] median (2nd) = 2.
Remove i=2 (value 2 at position 2 maybe? Actually which occurrence? Suppose we have indices: i=1:1, i=2:2, i=3:2, i=4:3. Sorted with stable: (1,1)->1; (2,2)->2; (2,3)->3; (3,4)->4. Then:
Remove i=2 (p=2 <=2): B=Y3=2. Actual: remove one 2, remaining [1,2,3] sorted [1,2,3] median 2 -> OK.
Remove i=3 (p=3 >2): B=Y2=2. Actual: remove the other 2, remaining [1,2,3] median 2 -> OK.
So fine.
Now consider an edge case: What if the sorted array has Y_{N/2} and Y_{N/2+1} are distinct, and we remove an element whose rank is exactly N/2? According to rule, p_i <= N/2 => B_i = Y_{N/2+1}. If p_i = N/2, that's <=, so B_i = Y_{N/2+1}. Check: N=4, values: 1,2,3,4. Sorted: [1,2,3,4]. N/2=2. Y2=2, Y3=3. p_i for element 2? Suppose we have i where X_i=2, and it gets rank2. Then after removing that 2, remaining [1,3,4] sorted median (2nd) = 3. So B_i =3 = Y_3 correct. If we remove element with rank3 (value3), p_i=3>2 => B_i = Y2=2, and actual: remove 3, remaining [1,2,4] median 2. Works.
Now what if there are duplicate values that cause the position of the removed element to be ambiguous? But we assign a specific rank to each occurrence via sorting with tie-breaking (like original index). That's correct because the actual multiset removal treats that specific occurrence. When we sort the multiset, we have to consider that the elements are distinct objects; if we remove one particular object, its position in the sorted order is determined by its value and by some ordering among equals. In a multiset, if we sort with a stable tie-breaking (like original index), then the sorted order is well-defined. So our rule based on rank p_i should be accurate.
But wait: is the median of the remaining numbers defined based on values only, independent of tie-breaking? The median is the (N/2)-th smallest value among the multiset of N-1 numbers. The multiset doesn't have any ordering besides values. When we have duplicate values, the median is just that value, irrespective of which occurrence we removed. But the process of deciding which element is the N/2-th smallest when there are duplicates: we count multiplicity. For example, if we have values [1,2,2,3], the 2nd smallest is 2, because there are two 2's. The precise index doesn't depend on which "2" we consider as first or second. So the median is the value at that position counting duplicates. So to compute B_i, we need to know: after removing X_i, what is the (N/2)-th smallest value? This can be determined by looking at the sorted list of all values and seeing where the removed value sits relative to the two central positions.
Our simple rule: B_i = Y_{N/2} if p_i > N/2, else B_i = Y_{N/2+1}. This seems plausible. But we must confirm it holds in all cases, especially when p_i <= N/2 but the removed element's value is greater than Y_{N/2}? Wait, if p_i <= N/2, that means the removed element is among the first N/2 in sorted order. Then the N/2-th smallest among remaining will be Y_{N/2+1} (the next one). This holds regardless of the value. Similarly, if p_i > N/2, then median is Y_{N/2}. That seems straightforward from index shift.
But what about the case when there are many duplicates and the removed element's rank is say p_i, but Y_{N/2} and Y_{N/2+1} might be equal or not. The index shift logic still holds: we need to find the original element that ends up at position N/2 after removal. This is exactly: if we remove an element at position <= N/2, then the element originally at N/2+1 becomes the new N/2; if we remove an element at position > N/2, then the element originally at N/2 remains N/2.
But does this rely on the concept of "position" being unique for each element? Yes. Since elements are distinct objects, even if they have equal values, we can assign them distinct positions in sorted order by any stable tie-breaking. The median value is determined by the sorted list of values, which is not unique in ordering of equal values. However, the value at position N/2 in any sorted order (non-decreasing) is well-defined because if there are duplicates, any permutation of equal values yields the same multiset of values at each position? Actually, if there are duplicates, the sorted sequence is not unique in terms of which occurrence goes where, but the multiset of values is sorted non-decreasing, so the value at index k is well-defined as the k-th smallest value, which is the same regardless of tie-breaking because if there are duplicates, the exact positions of equal values might swap, but the value at index k is determined by the order statistics: it's the k-th order statistic. For a multiset, the k-th order statistic is unique value, unless there are ties? Actually, if there are duplicates, the k-th smallest value is a value such that the count of numbers less than that value is < k and count of numbers less than or equal is >= k. That value is uniquely defined (though there could be multiple occurrences of that value, the "value" itself is unique). So when we talk about Y_{k}, we mean the k-th smallest value. That is unique. In our sorted list Y we have a specific ordering of the elements; but if we reorder equal-valued elements among themselves, the value at index k might change if there are duplicates? Let's examine: Suppose we have values [2,2,3]. The 2nd smallest value is 2. In any sorted ordering, the second element will be 2 because there are two 2's. So Y_2 = 2. Even if we permute the two 2's, the second position still holds 2. So Y_k is well-defined as the k-th order statistic value. So we don't need to worry about tie-breaking for the value Y_k; it's just the k-th smallest value.
Now consider the removal of a specific element with value v. In the sorted multiset, there may be multiple copies of v. Which rank p_i should we assign? The rank of that specific occurrence might be any index among the range of indices where v appears. But the median after removal B_i depends only on the value v and how many copies are less/greater? Let's analyze more thoroughly.
We have a multiset S of N numbers, all integer values. For each element x_i, we consider S \ {x_i}. We need the N/2-th smallest value of S{x_i}. Since N is even, N/2 is integer.
Let the sorted values of S be v_1 <= v_2 <= ... <= v_N, where these are values (with multiplicities). Because of duplicates, there may be repeated values. The order statistics: For any k, v_k is the k-th smallest value. This is uniquely defined.
Now, remove one occurrence of value x_i. How does the order statistics change? Let L = number of elements strictly less than x_i in S. Let E = number of elements equal to x_i in S (including the one we remove). So L + E = count of elements <= x_i, and L < N/2? Possibly.
After removing that one occurrence, the multiset S' has size N-1. For the N/2-th order statistic (value) in S', it could be:
- If L >= N/2: that means there are at least N/2 elements strictly less than x_i. Then even after removal, the N/2-th smallest value remains the same as the original v_{N/2}? Not necessarily: if we remove an element that is > v_{N/2}, it might not affect. But if we remove an element that is less than or equal? Let's formalize.
Better approach: The N/2-th smallest in S' will be either v_{N/2} or v_{N/2+1}, depending on which side of the removal relative to the median. But is it always one of those two? Since we remove just one element, the sorted sequence S' is nearly the same as S, except one element missing. For any index, the value at that index in S' equals either the value at same index in S or the value at next index if the removed element was at or before that index.
Specifically, define p as the position of the removed occurrence in the sorted order if we break ties arbitrarily? However, the "position" of a specific occurrence is not uniquely defined when there are duplicates. But we can think of the multiset as a list of occurrences with some ordering. To determine the new order statistic, we can think in terms of counts.
Let’s define the counts. Let the value of the N/2-th order statistic in the original S be m = v_{N/2}. Let also next = v_{N/2+1}. After removal, we need to find the new N/2-th order statistic.
Consider the number of elements in S' that are <= m. In original S, count of elements <= m is at least N/2 (since v_{N/2}=m, there are at least N/2 elements <= m). It could be more if m appears multiple times; let C = count of elements with value <= m in S. Then C >= N/2. Similarly, count of elements < m is less than N/2 (since v_{N/2}=m, there are at most N/2-1 elements < m). Let c_less = count of elements strictly less than m; then c_less < N/2.
Now, after removing x_i:
Case 1: x_i < m. Then c_less decreases by 1. So the number of elements strictly less than m becomes c_less-1, which is <= N/2-2? Actually, c_less < N/2, so c_less-1 < N/2-1. The count of elements <= m becomes C-1 (since x_i < m, it was counted in <= m). Since originally C >= N/2, now C-1 >= N/2-1. The N/2-th order statistic in S' will be the smallest value such that cumulative count >= N/2. Since after removal, there are N-1 elements. The N/2-th order statistic: we need the value v such that number of elements <= v >= N/2 and number of elements < v < N/2. Consider v = m: number of elements in S' <= m = C-1. Is C-1 >= N/2? It might be or might not be. Since C could be exactly N/2 or more. If C > N/2, then C-1 >= N/2, so the count condition for v=m is satisfied. But also need count of elements < m = c_less-1. Is c_less-1 < N/2? Yes, because c_less < N/2 => c_less-1 < N/2-1 < N/2. So v=m is indeed the N/2-th order statistic. So B_i = m. However, what if C = N/2 exactly? That means there are exactly N/2 elements <= m, and since v_{N/2}=m, the elements with value m are those that fill up to position N/2; there are no elements with value m beyond N/2? Actually if C = N/2, then exactly N/2 elements have value <= m, and the next values are > m. Since v_{N/2}=m, it implies that the N/2-th element is m, and all elements at indices > N/2 are > m. So then c_less = number of elements < m = maybe < N/2, but C = c_less + count(m) = N/2. So count(m) = N/2 - c_less.
Now if we remove an element x_i < m, then C-1 = N/2 -1, which is less than N/2. So the number of elements <= m in S' is N/2 -1, which is insufficient to reach N/2. Thus the N/2-th order statistic must be > m. In that case, the next value is v_{N/2+1} = next (since next > m). So B_i = next. But is that possible? Let's test with example: S = [1,2,3,4], N=4, m=v2=2, next=v3=3. If we remove x_i=1 (< m), then S' = [2,3,4]. The N/2=2nd smallest is 3? Actually S' sorted: [2,3,4]; second smallest is 3, which is next. Indeed C = count <=2 = 2 (elements 1 and 2) = N/2? Actually N/2=2, C=2 exactly. After removal of 1, C-1=1 <2, so median becomes next=3. So B_i=next. That matches our earlier rule? Our earlier rule used rank p_i: if p_i <= N/2 then B_i = Y_{N/2+1} else B_i = Y_{N/2}. Here x_i=1 has rank p=1 <=2, so B_i = Y_3=3, which matches this case. So indeed when x_i < m and C = N/2, B_i = next. If C > N/2, then B_i = m.
Now, what about when x_i = m? Then we remove one of the elements equal to the median value. Then the effect depends on how many m's there are and their positions.
Let count_m = number of elements with value m.
In original S, the positions of m range from L+1 to L+count_m, where L = count of elements < m. Since v_{N/2} = m, we know that L < N/2 <= L+count_m.
Case 2: x_i = m.
After removal, count_m' = count_m - 1. The number of elements <= m becomes C' = L + count_m' = (L + count_m) - 1 = C - 1.
We need to determine whether the N/2-th order statistic remains m or becomes next.
If C > N/2, then C' = C - 1 >= N/2, so still at least N/2 elements <= m. And number of elements < m = L remains unchanged (since we removed an m). Since L < N/2 (because L < N/2 by definition, as N/2 is <= L+count_m and L < N/2), L < N/2. So the N/2-th order statistic is still m. So B_i = m.
If C = N/2, then originally there are exactly N/2 elements <= m, i.e., L + count_m = N/2. Since L < N/2, count_m = N/2 - L >=1. After removal of one m, C' = N/2 - 1 < N/2, so there are fewer than N/2 elements <= m. Hence the N/2-th order statistic must be > m, i.e., next. So B_i = next.
Thus when x_i = m, B_i = m if C > N/2; else B_i = next.
Now, what about x_i > m? Then we remove an element greater than m. Then counts <= m unchanged. So number of elements <= m remains C. Since C >= N/2 (because at least N/2 elements <= m), after removal, we still have at least N/2 elements <= m? But careful: C might be exactly N/2. If C = N/2, then after removal of a > m element, C remains N/2. So there are exactly N/2 elements <= m. The number of elements < m = L < N/2. So the N/2-th order statistic is m (since cumulative count at m is exactly N/2, and for any value less than m it's < N/2). So B_i = m. If C > N/2, also B_i = m. So for x_i > m, B_i is always m.
Thus we have three cases based on comparison of x_i with m (the N/2-th smallest value) and also based on whether C (count of <= m) is > N/2 or = N/2.
But our simple rank-based rule gave: B_i = Y_{N/2} if p_i > N/2 else B_i = Y_{N/2+1}. That rule uses p_i, which is the rank of that specific occurrence in sorted order, with ties broken arbitrarily but consistently. However, this rule might produce a different answer than the above count-based analysis in some borderline cases? Let's compare.
In the count-based analysis, for x_i = m, the result depends on whether C > N/2 or C = N/2. In the rank-based rule, p_i could be any index from L+1 to L+count_m. Among these, some might be <= N/2, some > N/2. Specifically, if L+1 <= N/2 <= L+count_m, then some of the m's have p_i <= N/2 and some have p_i > N/2. But in count-based analysis, if C > N/2, meaning L+count_m > N/2, then there are more m's than needed to fill up to N/2. In that scenario, some m's have p_i <= N/2 and some have p_i > N/2. But count-based says for any removal of an m, B_i = m (since C > N/2). Our rank-based rule would give B_i = Y_{N/2+1} for those with p_i <= N/2, and B_i = Y_{N/2} for those with p_i > N/2. If C > N/2, then Y_{N/2} = m and Y_{N/2+1} might also be m if there are enough m's? Actually, if C > N/2, then L+count_m > N/2. The (N/2+1)-th smallest value could be m if count_m is at least 2 and N/2+1 <= L+count_m, i.e., if L+count_m >= N/2+1. Since C > N/2, it's possible that Y_{N/2+1} = m as well, if there are at least two m's covering positions N/2 and N/2+1. But if C = N/2+1? Actually, if C > N/2, it's possible that Y_{N/2+1} = m as long as N/2+1 <= L+count_m, i.e., if count_m >= (N/2+1 - L). Since L+count_m = C > N/2, it's possible that N/2+1 <= L+count_m, but not guaranteed. For example, suppose L = N/2 - 1, count_m = 2, then L+count_m = N/2+1, so positions: L+1 = N/2, L+2 = N/2+1, both m. So Y_{N/2}=m, Y_{N/2+1}=m. Then rank-based rule: if p_i <= N/2, B_i = Y_{N/2+1} = m; if p_i > N/2, B_i = Y_{N/2}=m. So both give m. Good.
But consider scenario where C > N/2, but Y_{N/2+1} is not m; that would require that L+count_m = N/2? Wait, if C > N/2, then L+count_m >= N/2+1. So the (N/2+1)-th position is still within the block of m's if count_m is large enough to cover that index. If count_m is exactly 1? But if count_m=1, then L+count_m = L+1. For C > N/2, we need L+1 > N/2, i.e., L >= N/2. But L is count of elements less than m; if L >= N/2, then the N/2-th smallest would be less than m, contradiction because m is the N/2-th smallest. So count_m cannot be 1 if C > N/2? Let's analyze: For m = v_{N/2}, we have L < N/2 <= L+count_m. If count_m = 1, then L+1 >= N/2 => L >= N/2-1, and the upper bound L+count_m = L+1. Since we also need L < N/2, the only possibility is L = N/2-1, then L+1 = N/2, so C = N/2 exactly, not > N/2. So if count_m = 1, then C = N/2 exactly. Therefore, if C > N/2, count_m must be at least 2. And with count_m >=2, and L+count_m >= N/2+1, it's possible that N/2+1 is still within the m block if L+count_m >= N/2+1. Since count_m >=2, it's likely that L+count_m >= L+2. Since L could be as low as? L < N/2. The worst case for covering N/2+1 is L = N/2 - 1, count_m = 2, then L+2 = N/2+1, so indeed N/2+1 is within m block. If count_m > 2, also. So in any scenario with C > N/2, we have L+count_m >= N/2+1, meaning Y_{N/2+1} = m as well. Because the (N/2+1)-th smallest value is still m because there are enough m's to occupy that position. Therefore, both Y_{N/2} and Y_{N/2+1} are m. So rank-based rule yields m regardless.
Now consider the other borderline: C = N/2. Then L+count_m = N/2. That implies that Y_{N/2}=m, and Y_{N/2+1} = next (> m). Here, L = count of < m, and count_m = N/2 - L. The positions of m are from L+1 to L+count_m = N/2. So all m's occupy positions up to N/2 exactly. That means any occurrence of m has p_i <= N/2 (specifically, p_i = L+1,...,N/2). So for all x_i = m, p_i <= N/2. Then rank-based rule gives B_i = Y_{N/2+1} = next. Count-based rule also gives B_i = next because C = N/2. For x_i < m, p_i <= L (which is <= N/2-1), so p_i <= N/2, rank-based gives next; count-based gave: if C = N/2, then for x_i < m, we have C-1 = N/2-1 < N/2 => B_i = next. So consistent. For x_i > m, p_i > N/2, rank-based gives Y_{N/2}=m; count-based gives m. So consistent.
Thus the rank-based rule is indeed equivalent to the count-based rule. The key is that we need to compute p_i correctly: the rank (position) of each element in sorted order, but with ties broken in a way that the set of positions for equal elements aligns with the definition of order statistics? Actually, as we reasoned, Y_k is the k-th smallest value, not necessarily tied to a specific occurrence. However, to decide whether p_i > N/2 or <= N/2, we need to assign each occurrence a distinct index. But for elements with value v that appears multiple times, we need to decide which of them are considered to have p_i <= N/2. But because Y_{N/2} and Y_{N/2+1} could be equal or different, the rule "if p_i > N/2 => B_i = Y_{N/2}, else B_i = Y_{N/2+1}" will give the correct result if we assign ranks consistently with the natural ordering of values, but with ties broken arbitrarily. However, is it always safe? Let's test a tricky scenario where C > N/2, Y_{N/2}=m, Y_{N/2+1}=m as we argued, so both are m, so any assignment of p_i among equal m's yields B_i = m regardless of whether p_i <= N/2 or > N/2. So no problem. In scenario where C = N/2, Y_{N/2}=m, Y_{N/2+1}=next > m, and all m's have positions <= N/2 (since they occupy exactly up to N/2). So if we assign p_i to m's arbitrarily, we might inadvertently assign a p_i > N/2 to some m? That would be inconsistent because positions > N/2 must correspond to values > m (since Y_{N/2+1}=next > m). In a sorted list, after sorting by value and breaking ties arbitrarily, can an m appear after a next? No, because all m's are less than next. So any stable sorting will place all m's before any value greater than m. Therefore, all m's will occupy positions 1..N/2? Not necessarily; they occupy positions from L+1 to L+count_m = N/2. Since L could be less than N/2, the range of positions for m is within 1..N/2. Indeed, L+1 could be as low as 1, but N/2 is the maximum. So all m's are at positions <= N/2. So no m gets position > N/2. Similarly, any value less than m gets positions <= L <= N/2-1, so also <= N/2. And values greater than m get positions >= N/2+1. So indeed, p_i <= N/2 exactly when the value is <= m, and p_i > N/2 exactly when the value > m. But wait, in the C > N/2 case, some m's may have positions > N/2 because there are more m's than needed to fill up to N/2. For example, L=3, count_m=4, N/2=5, then positions of m: 4,5,6,7. So some m's (positions 6,7) are > N/2. That's okay because then Y_{N/2}=m, Y_{N/2+1}=m. In this case, the rule for those m's with p_i>N/2 gives B_i=Y_{N/2}=m; for those with p_i<=N/2 gives B_i=Y_{N/2+1}=m. Both m.
So the rule works as long as we assign ranks consistently with the sorted order: all values sorted non-decreasing, and equal values can be in any order but must be contiguous. Then p_i > N/2 exactly means the element is not among the first N/2 in the sorted multiset. That is equivalent to: after sorting, the element is in the second half (positions N/2+1..N). But careful: due to duplicates, some elements with value equal to the median could be in both halves. But then Y_{N/2}=Y_{N/2+1}=m, so the rule still gives m regardless.
Thus we can compute B_i as:
- Sort the array to get sorted values. Let A = sorted list.
- Let med_low = A[N/2 - 1] if using 0-index? Let's define indices: we need the N/2-th smallest (1-indexed) = A[N/2] if using 1-indexed array. In 0-indexed, positions: 0..N-1. The 1-indexed position k corresponds to index k-1. So N/2-th smallest (1-indexed) corresponds to index N/2 - 1? Wait, N/2 is integer. Example N=4, N/2=2, second smallest is index 1 in 0-indexed. So A[1] = N/2-th smallest. Similarly, (N/2+1)-th smallest is index N/2. So we can define:
Let low = A[N/2 - 1] (0-indexed, the N/2-th element)
Let high = A[N/2] (the (N/2+1)-th element)
Then for each original X_i, we need to determine if, when removing X_i, the median is low or high.
From earlier rank rule: if the element is in the "first half" (i.e., its rank <= N/2), median = high; else median = low. But rank is 1-indexed. In 0-indexed terms: rank = position in sorted order (1-indexed). So we can compute for each i its sorted index (1-indexed). Then:
if rank_i <= N/2: B_i = high
else: B_i = low
Now, we need to compute rank_i efficiently. Sorting the pairs (value, index) yields sorted order and we can assign each element its 1-indexed position in that sorted order. That's O(N log N). Then output accordingly.
But we must be careful: when there are duplicates, which rank is assigned to which element? As argued, any consistent ordering among equal values works because for equal values the output B_i is either low or high, and if low != high, then all elements of that value will be on same side? Actually, if low != high, then low < high (since sorted). That means the median values are distinct. In that case, the N/2-th element is some value m, and the (N/2+1)-th element is a larger value. So all elements with value <= m occupy the first N/2 positions? Not exactly: if there are multiple m's, they could occupy positions up to some index > N/2? Let's see: if low != high, then low = m, high = M > m. Since low is the N/2-th smallest, that means there are exactly N/2 elements that are <= m? Actually, the N/2-th smallest is m. Since the next is M > m, that implies that the number of elements <= m is exactly N/2? Because if there were more than N/2 elements <= m, then the (N/2+1)-th smallest would also be m (since m appears at least up to N/2+1). But we have high = M > m, so there are exactly N/2 elements <= m. Therefore, count of elements with value <= m is N/2. Thus all elements with value <= m occupy exactly the first N/2 positions (though there might be elements with value m, but they fill the positions up to N/2). So any element with value m has rank <= N/2. Elements with value > m have rank > N/2. So in this case, the rule "if rank_i <= N/2 => B_i = high, else B_i = low" yields:
- For elements with value <= m (including m): B_i = high = M.
- For elements with value > m: B_i = low = m.
Check with earlier example: [1,2,3,4] -> low = 2, high = 3. Elements <=2: 1,2 -> ranks <=2 => B=3. Elements >2: 3,4 -> ranks >2 => B=2. Works.
If low == high, then median is same regardless, all B_i = low = high. So rule works as well.
Thus the simple approach works.
Now we need to implement for N up to 200k. We'll sort values with indices. Then compute two values: low = sorted[N/2 - 1] (0-indexed) and high = sorted[N/2] (0-indexed). For each element based on its rank in sorted order, we output either high or low.
But careful: N/2 is integer, N is even. So N/2 and N/2+1 are distinct positions. Using 0-index: indices 0..N-1. The element at index N/2 - 1 is the N/2-th smallest (since index 0 is 1st). The element at index N/2 is the (N/2+1)-th smallest.
Implementation steps:
- Read N, array X of length N.
- Create list of pairs (value, original_index).
- Sort list by value. For ties, we can keep the original order, but it doesn't matter. The sorting algorithm will likely be stable if we don't specify, but we can just sort by value; if values equal, the order of indices may be arbitrary but it's okay.
- Create an array rank of size N (or maybe we can compute answer on the fly). We'll need for each original index i, its position (1-indexed) in sorted order. So after sorting, iterate over sorted list with position j from 0 to N-1, and set rank[original_index] = j (or j+1 for 1-indexed). Actually we need to know if rank <= N/2 or > N/2. We can store bool directly: if j < N/2 (0-indexed) then it's among first N/2 positions? Wait, careful: rank in 1-indexed: the first element has rank 1. The condition rank_i <= N/2 means position <= N/2. In 0-indexed, that corresponds to index <= N/2 - 1. So we can compute: if sorted_position (0-index) < N/2 then rank <= N/2? Actually if sorted_position is from 0 to N-1, then 1-indexed rank = sorted_position + 1. Condition rank <= N/2 => sorted_position + 1 <= N/2 => sorted_position <= N/2 - 1. So yes, sorted_position < N/2 (since N/2 is integer, N/2 - 1 is the largest index with sorted_position <= N/2-1). So we can use: if sorted_index < N/2 then output high else output low.
But recall we defined low = sorted[N/2 - 1] (0-index), high = sorted[N/2] (0-index). So:
if sorted_index < N/2: # belongs to first half (including exactly N/2-th? sorted_index = N/2 -1 is the last of first half) then output high.
else: output low.
Let's test: N=4, N/2=2, sorted_index: 0,1,2,3. If sorted_index <2 => indices 0,1 output high (index2? Wait high = sorted[2]? Actually N/2 = 2, so high = sorted[2]? But we said high is the (N/2+1)-th smallest, index N/2 (0-index) because N/2+1 => index N/2. For N=4, N/2=2, high index = 2, which is the third element? That seems off. Let's recompute with indices carefully.
We have N even. The sorted array A of length N (0-indexed). Define:
- low: the N/2-th smallest (1-indexed). That is the element at position N/2 in 1-indexed => index = N/2 - 1 in 0-indexed.
- high: the (N/2+1)-th smallest (1-indexed) => index = N/2 in 0-indexed.
For N=4: N/2=2, low index=1 (A[1]), high index=2 (A[2]). Example sorted [1,2,3,4] -> low=2, high=3. Good.
Now condition: if element is among the first N/2 in sorted order (i.e., its rank <= N/2), then after removal median = high. The first N/2 elements are indices 0..N/2-1 (0-index). Those are exactly indices < N/2. So if sorted_index < N/2, output high; else output low.
Test: For element with sorted_index 0 (rank1): output high=3, correct? In example [1,2,3,4], removal of 1 yields median 3, correct.
sorted_index 1 (rank2): output high=3, removal of 2 yields median 3, correct.
sorted_index 2 (rank3): output low=2, removal of 3 yields median 2, correct.
sorted_index 3 (rank4): output low=2, removal of 4 yields median 2, correct.
So rule works.
Now, duplicates: In case low == high, both equal, so output always same. The condition also works but not needed.
Thus algorithm:
- Input N, array X.
- Create vector of pairs (value, index).
- Sort by value. For ties, the order among equal values can be arbitrary.
- After sorting, we have array sorted_vals (or we can just extract low and high values: low_val = sorted[N/2 - 1].value, high_val = sorted[N/2].value).
- Create answer array ans of size N.
- For each position j from 0 to N-1:
original_index = sorted[j].second
if j < N/2:
ans[original_index] = high_val
else:
ans[original_index] = low_val
- Output ans line by line.
Complexities: O(N log N) sorting, O(N) extra.
Edge Cases: N=2: sorted: [a,b] (a<=b). N/2=1, low = sorted[0] = a, high = sorted[1] = b. For j=0 (<1) => ans[ index of a ] = high = b; j=1 (>=1) => ans[ index of b ] = low = a. Works.
Now test with given examples.
Example1: N=4, X=[2,4,4,3]. Sort pairs: values sorted: [2,3,4,4] with indices (1,4,2,3) as earlier. N/2=2, low = A[1]=3, high = A[2]=4. j=0 (<2): ans[ index1 ]=4; j=1 (<2): ans[ index4 ]=4; j=2 (>=2): ans[ index2 ]=3; j=3: ans[ index3 ]=3. Output order: 4,3,3,4. matches.
Example2: N=2, X=[1,2] => sorted [1,2] low=1, high=2. j=0 (<1): ans[ index of 1]=2; j=1: ans[ index of 2]=1. Output 2,1.
Example3: N=6, X=[5,5,4,4,3,3]. Sorted [3,3,4,4,5,5]. N/2=3, low=A[2]=4, high=A[3]=4. Both 4. So ans all 4.
All good.
Now, ensure constraints: N up to 200k, values up to 1e9. Sorting O(N log N) is fine.
Potential pitfalls: We must be careful about 0-index vs 1-index. Use integer division.
Implementation details: read input, store in vector X(N). Then create vector<pair<int,int>> data(N). For i in 0..N-1: data[i] = {X[i], i}. Sort data by first (value). Then low_val = data[N/2 - 1].first; high_val = data[N/2].first. Then ans vector of size N. For j in 0..N-1: idx = data[j].second; ans[idx] = (j < N/2 ? high_val : low_val). Then print ans lines.
That's it.
But is there any scenario where our rule might fail due to tie-breaking among equal values? Let's think more deeply. The sorted order we create might assign some elements with value equal to low_val to positions >= N/2, and some to < N/2. But we argued that if low_val != high_val, then count of <= low_val is exactly N/2, so all low_val elements must be in the first N/2 positions? Actually, if low_val != high_val, then the number of elements <= low_val is exactly N/2, because if there were more than N/2, then the (N/2+1)-th would also be low_val. So indeed, the count of low_val elements is exactly N/2 - count_less, where count_less = number of elements < low_val. All low_val elements occupy positions from count_less+1 to N/2. So all low_val elements have sorted index <= N/2-1 (since 0-index indices from count_less to N/2-1). So all low_val elements satisfy j < N/2. So they get high_val as output. That's correct because for an element with value = low_val (which is the median low), if low_val != high_val, then removal of such element leads to median = high_val (since there are exactly N/2 elements <= low_val, and removing one reduces count to N/2-1, so median becomes high_val). So our condition yields high_val for those low_val elements. Good.
What about elements with value high_val? If low_val != high_val, then high_val > low_val. Then the count of elements <= high_val is at least N/2+1 (since there is at least one high_val). So some high_val elements may be in positions N/2 or later. In particular, the first high_val appears at index N/2 (since that's the first element > low_val). So all high_val elements have j >= N/2. So they get low_val output. That's correct: removing a high_val element (which is > low_val) leaves median low_val because there are still N/2 elements <= low_val. So condition works.
If low_val == high_val, then all elements are either less than, equal to, or greater? Actually if low_val == high_val, then the N/2-th and (N/2+1)-th values are the same, meaning there are at least two occurrences of that value such that they cover those positions. In that case, the output is the same regardless, so any assignment works.
Thus, the algorithm is robust.
Now, we need to produce output lines. That's straightforward.
But wait, could there be a scenario where low_val and high_val are the same but due to tie-breaking, we might incorrectly assign some element with that value to j < N/2 and some to j >= N/2? That doesn't matter because both branches output the same value (since low_val == high_val). So no problem.
Therefore, the solution is correct.
Double-check: N is even, N>=2, okay.
Now, verify with some random tests via mental or small code? Let's do some mental tests.
Test: N=6, values: [1,2,3,4,5,6]. Sorted: [1,2,3,4,5,6]. N/2=3, low = A[2]=3, high = A[3]=4. Then:
indices: 0 (<3) -> high=4; 1 (<3) -> 4; 2 (<3) -> 4? Actually j=2 is <3, so output high=4. So removal of 1,2,3 all yield median 4? Let's verify: remove 1 -> remaining [2,3,4,5,6] sorted median (3rd) =4. remove 2 -> [1,3,4,5,6] median =4? Sorted: 1,3,4,5,6 -> 3rd=4. remove 3 -> [1,2,4,5,6] -> 3rd=4. Yes. For j=3,4,5 (>=3) -> output low=3. removal of 4 -> [1,2,3,5,6] sorted: 1,2,3,5,6 -> 3rd=3. removal of 5 -> [1,2,3,4,6] -> 3rd=3? Sorted:1,2,3,4,6 -> 3rd=3. removal of 6 -> [1,2,3,4,5] -> 3rd=3. So works.
Test with duplicates across boundary: N=6, values: [1,2,2,2,3,4]. Sorted: [1,2,2,2,3,4]. N/2=3, low = A[2]=2, high = A[3]=2? Actually A[2] (0-index) = 2, A[3] = 2, so low=2, high=2. So all outputs 2. Let's verify manually: remove 1: remaining [2,2,2,3,4] sorted median 3rd=2; remove a 2: remaining size5, median 3rd: if we remove one 2, we still have at least two 2's, and maybe other numbers, median likely 2; remove 3: [1,2,2,2,4] sorted median 3rd=2; remove 4: [1,2,2,2,3] median=2. So all 2. Good.
Test with low != high but many duplicates of low and high: N=6, values: [1,2,2,2,2,3]. Sorted: [1,2,2,2,2,3]. N/2=3, low = A[2]=2, high = A[3]=2? Actually A[2]=2, A[3]=2, so low=high=2, all outputs 2. Check: removal of 1: [2,2,2,2,3] median 3rd=2; removal of 2: [1,2,2,2,3] median 3rd=2; removal of 3: [1,2,2,2,2] median 3rd=2. All good.
Test with low != high and duplicate low across the boundary? Actually if low != high, count low exactly N/2? Example: N=6, values: [1,2,2,3,4,5]. Sorted: [1,2,2,3,4,5]. N/2=3, low = A[2]=2, high = A[3]=3. Count of <=2? Elements <=2: 1,2,2 => three, exactly N/2. So all low (2) are in first 3 positions. Our sorting: indices for 2: j=1 and j=2. Both <3 => output high=3. So removal of a 2 yields median 3. Let's check: remove a 2: remaining [1,2,3,4,5] median 3rd = 3; correct. Removal of 1: [2,2,3,4,5] median 3rd = 3; removal of 3: [1,2,2,4,5] median 3rd = 2; j for 3? 3 is at index 3 (>=3) output low=2. Good.
Now consider if there are elements with value between low and high? Not possible because low and high are consecutive order statistics, so there is no value between them.
Thus, solution stands.
Potential alternative: We could compute low and high, then for each X_i, if X_i <= low then output high else output low. But careful: when low == high, this would give high always, which is fine. But if low != high, is "X_i <= low" correct? Let's test: In example [1,2,3,4], low=2, high=3. X_i <=2 => output high=3, correct. X_i >2 => output low=2, correct. In example with many low's: N=6 [1,2,2,3,4,5] low=2, high=3. X_i <=2 -> output 3. That includes both 1 and 2. Check removal of 1 gives 3, removal of 2 gives 3, correct. X_i >2 -> output 2. Check removal of 3,4,5 gives 2. So seems that rule "if X_i <= low then output high else output low" works when low != high? But we must be careful: what about elements with value = low but located after N/2? That cannot happen when low != high because count of <= low = N/2 exactly, so all low are in first half, thus X_i <= low implies rank <= N/2. However, if there are elements with value less than low, they also satisfy X_i <= low, and they are in first half. So indeed, condition X_i <= low is equivalent to rank <= N/2 when low != high. But is it always? Consider the scenario where low != high, we have count of <= low = N/2. So any element with value <= low will be among the first N/2 elements. Conversely, any element among the first N/2 elements has value <= low? Since the (N/2)-th element is low, all elements before that are <= low, but could there be an element with value > low among first N/2? No, because the (N/2)-th is low, and all previous are <= low (since sorted). So yes, first N/2 elements are exactly those with value <= low. So condition X_i <= low is equivalent to rank <= N/2. So we can avoid computing ranks and simply compare X_i to low. But careful: if there are duplicates of low, X_i = low qualifies as <= low, and they are all in first half. So condition holds.
But what about when low == high? Then condition X_i <= low would be true for all elements? Actually low == high, so low is the median value. In that case, both low and high are same, so output that value regardless. However, if we used the rule "if X_i <= low then output high else output low", we would output high (which equals low) for all i, since all X_i are <= low? Not necessarily: if low == high, that means the N/2-th and (N/2+1)-th values are equal, but that doesn't imply that all values are <= that value; there could be values greater than that value. For example: N=6, values: [1,2,3,3,4,5]. Sorted: [1,2,3,3,4,5]. N/2=3, low = A[2]=3, high = A[3]=3. So low == high = 3. But there are values > 3 (4,5). X_i <= low? For 4 and 5, 4 <= 3 is false. So if we used the rule, for X_i=4, we would output low (3), which is correct because median after removal is 3? Let's verify: remove 4: remaining [1,2,3,3,5] sorted: 1,2,3,3,5 -> median 3rd = 3, correct. For X_i=5, similarly median 3. So for those > low, we would output low, which is correct. For X_i=1,2,3, they satisfy <= low, so output high (which is 3). So overall, the rule would output 3 for all. So it works even when low == high, because the else branch outputs low (which equals high) as well. So the rule "if X_i <= low then output high else output low" actually works in all cases, regardless of whether low == high or not. But we need to be careful: is it always true that for elements with value <= low, the median after removal is high? Let's reexamine the count-based analysis.
General case: Let low = v_{N/2}, high = v_{N/2+1}. For any x_i:
- If x_i <= low, then after removal, is median = high? Not always; consider when there are more than N/2 elements <= low (i.e., low == high). In that case, median after removal might still be low (which equals high). So it's still high (since high == low). So it's consistent.
- If x_i > low, then median = low. That holds even if low == high? If low == high, then x_i > low implies median = low (since low == high). So again consistent.
But is there any scenario where x_i <= low but median after removal is low (instead of high) while low != high? Let's check. Suppose low != high, so count of <= low = N/2. If we remove an element x_i <= low, then number of elements <= low becomes at most N/2 - 1. So median becomes > low, i.e., high. So yes. So the rule is sound.
Thus we can compute B_i without even sorting to get ranks: just compute low and high from the sorted array, and then for each X_i, if X_i <= low => print high; else print low. This is even simpler.
But caution: In the case low != high, we rely on the fact that any element with value <= low is exactly those in the first half. However, if there are elements with value equal to low but also some with value less than low, all are <= low. So condition X_i <= low correctly identifies those that are in first half. For x_i > low, they are in second half. So it works.
But wait, what about an element with value = low but appears after N/2? As argued, if low != high, count of <= low is exactly N/2, so all low-valued elements occupy exactly the first N/2 positions? Not necessarily: Suppose we have values: low = 5, and there are elements: 1,5,5,5,6,7, with N=6? Let's try: N=6, sorted: [1,5,5,5,6,7]. Then N/2=3, low = A[2]=5, high = A[3]=5? Actually A[2]=5, A[3]=5, so low=high=5. So low != high is false. So not relevant.
To have low != high, we need that after sorting, the N/2-th and (N/2+1)-th are different. That implies that the number of elements <= low is exactly N/2. So all elements with value low are exactly the ones that fill the positions from (count_less+1) to N/2. So indeed all low elements are within first N/2 positions. So no low element can be in the second half when low != high.
Thus condition X_i <= low correctly captures the first half. Good.
So the algorithm can be simplified:
- Sort the array X to get sorted copy A.
- Let low = A[N/2 - 1], high = A[N/2].
- For each original X_i:
if X_i <= low: print high
else: print low
That's O(N log N) for sorting, O(N) for output.
Test with earlier examples:
Ex1: X=[2,4,4,3], sorted=[2,3,4,4], low=3, high=4. For each:
2 <=3 -> print 4
4 <=3? false -> print 3
4 <=3? false -> print 3
3 <=3 -> print 4. Output matches.
Ex2: [1,2], sorted=[1,2], low=1, high=2. 1<=1 ->2; 2<=1? false ->1.
Ex3: [5,5,4,4,3,3] sorted=[3,3,4,4,5,5], low=4, high=4. All <=4? 5<=4 false -> print 4? Actually for 5 it prints low=4, for 3,4 it prints high=4. So all 4.
Thus works.
But wait: In ex3, there are values > low (5) that output low=4, correct. Values <= low (3,4) output high=4. So all 4.
Now, is there any case where an element X_i equals low but is not <= low? That's impossible.
Thus the condition is sound.
Edge Cases: When N=2, low = min, high = max. Then rule: if X_i <= min, print max; else print min. For (1,2): min=1, max=2. X_i=1 <=1 ->2; X_i=2 >1 ->1. Works.
Now, confirm with a random test where low != high but there are many duplicates of high? No issue.
Thus we have a very simple solution.
But we need to be cautious: The condition X_i <= low works because we compare value, not rank. However, consider a scenario where there are multiple values equal to low, but also there are values less than low. All those are <= low. That's fine.
But is it possible that there exists an element with value < low but appears after N/2? That would contradict sorting because if it's less than low, it would come before low in sorted order, and since low is at position N/2, any value < low must be at position < N/2. So that's fine.
Thus the rule holds.
Now, is there any scenario where an element with value > low but still could be in first half? That would require that element > low but appears before some low? But sorted order ensures all elements > low come after all elements <= low. Since low is at position N/2, all positions after N/2 are > low. So no.
So rule stands.
Thus we can implement.
We must ensure we use 0-index correctly: low = sorted[N/2 - 1]; high = sorted[N/2].
Now, is there any case where we need to consider stability of sorting? Not needed.
Thus solution is trivial.
But before finalizing, let's double-check with more complex cases using reasoning about counts.
Suppose N=8, values: [1,2,3,4,5,6,7,8]. Sorted, low = A[3] = 4, high = A[4] = 5. Then rule: X_i <=4 -> print 5; else print 4. Check: removal of 1,2,3,4 -> median of 7 numbers is 4th? Actually N/2=4, median of 7 numbers is 4th smallest. After removal of <=4, median should be 5? Let's test removal of 4: remaining [1,2,3,5,6,7,8] sorted: 1,2,3,5,6,7,8 -> 4th = 5. Good. Removal of 5: X_i=5 >4 -> print 4. Actual: remove 5, remaining [1,2,3,4,6,7,8] sorted: 1,2,3,4,6,7,8 -> 4th = 4. Works.
Now with duplicates: N=8, values: [1,2,2,2,3,3,4,5]. Sorted: [1,2,2,2,3,3,4,5]. N/2=4, low = A[3] = 2, high = A[4] = 3. Condition: X_i <=2 -> print 3; else print 2. Check removal of 2 (value 2): should print 3? Let's see: if we remove a 2, there are still two 2's? Actually original count of 2 is 3. After removing one 2, count of <=2 becomes: count of 1 (1) + count of 2 (2) = 3, which is less than 4 (N/2). So median becomes the next, 3. So output 3 correct. Removal of 3 (value >2): output 2. After removal of 3, count of <=2 remains 4 (1+3) so median 2. Good.
Now scenario low == high: N=8, values: [1,2,2,3,3,3,4,5]. Sorted: [1,2,2,3,3,3,4,5]. N/2=4, low = A[3] = 3? A[3] is 0-index: positions:0:1,1:2,2:2,3:3,4:3,5:3,6:4,7:5 -> low = A[3] = 3, high = A[4] = 3. So low=high=3. Rule: X_i <=3 -> print high=3; X_i >3 -> print low=3. So all 3. Check: removal of 4: remaining 7 numbers: [1,2,2,3,3,3,5] sorted median 4th = 3. removal of 2: [1,2,3,3,3,4,5] median 4th = 3. removal of 1: [2,2,3,3,3,4,5] median 4th = 3. Works.
Thus solution is consistent.
Now, we might think about potential integer overflow? Not an issue.
Implementation details in C++:
- Use vector x(N);
- vector sorted = x; sort(sorted.begin(), sorted.end());
- int low = sorted[N/2 - 1]; int high = sorted[N/2];
- For each i: if (x[i] <= low) cout << high << '\n'; else cout << low << '\n';
But need to be careful: N is even, N/2 is integer division; for N=2, N/2-1 = 0, okay.
Test with N=200000, values within int range, all fine.
Now, is there any scenario where this fails due to stable sorting? Not at all, because we only use sorted values to extract low and high, not ranks.
But wait: The condition X_i <= low might be ambiguous if there are multiple occurrences of low and we rely on the exact value. But it's just a numeric comparison, so no ambiguity.
But consider this: Suppose low != high. The element with value low, when removed, yields median high. So condition X_i <= low includes low, so it prints high, correct. Element with value high, which is > low, prints low. So all good.
Now, what about elements with value less than low? They also print high. That's correct.
Thus the solution is incredibly simple.
But is there a catch? Let's double-check the problem statement: "the median of l numbers a_1, a_2, ..., a_l is the (l+1)/2-th largest value among a_1, a_2, ..., a_l." They say "largest" but they mean "largest" as in order from smallest to largest? Actually "(\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l" could be ambiguous: Usually "largest" means descending order: the largest is the maximum, second largest is second maximum, etc. But here they say "largest value", which is weird because median is usually defined as the middle value when sorted in ascending order. However, "k-th largest" typically means the k-th when sorted descending (i.e., the k-th biggest). For an odd l, the median in ascending order is the (l+1)/2-th smallest, but they say largest. Let's parse: "the median of l numbers a_1, a_2, ..., a_l is the (\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l." That means if we sort the numbers in descending order (from largest to smallest), the median is the element at position (l+1)/2 in that order. That is equivalent to the (l+1)/2-th smallest? Let's see: In a list sorted descending, the first is largest, second is second largest, ..., the (l+1)/2-th largest is actually the ((l+1)/2)-th largest, which in ascending order would be the (l+1)/2-th smallest? Actually, if we have l numbers, the (l+1)/2-th largest is the same as the (l+1)/2-th smallest because the positions from both ends are symmetric: the k-th largest is the (l - k + 1)-th smallest. Here k = (l+1)/2 => l - (l+1)/2 + 1 = (2l - (l+1) + 2)/2 = (l+1)/2. So it's the same! For odd l, (l+1)/2-th largest equals (l+1)/2-th smallest. So it's consistent. For even l, median is not defined in the problem because l is odd only. So our interpretation of median as the (N/2)-th smallest (1-indexed) for N-1 odd is correct.
Now, the problem: "When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the (\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l." So they explicitly define median for odd l as the middle element when sorted in either order. So we are fine.
Now, the output examples confirm our interpretation: for input 4: 2 4 4 3 -> outputs 4,3,3,4. Using our method we got that.
So solution is good.
Thus we can produce code.
Time complexity: O(N log N). Memory: O(N).
We can also do O(N) using selection algorithms to find the two order statistics (N/2-th and (N/2+1)-th) without full sorting, but N log N is fine for 200k.
But is there any edge case where low and high are computed incorrectly due to sorting? No.
Now, let's ensure the code is correct for C++.
Potential pitfalls: Use long long? Values up to 1e9, but int can hold up to around 2e9, so int is fine (if 32-bit signed). In C++, int is typically 32-bit, max 2,147,483,647, so 1e9 is fine. So int okay.
But to be safe, we could use long long, but not needed.
Now, for the output, we should print each B_i on separate line.
Now, consider the input format: N on first line, then N numbers on second line (or possibly multiple lines). We'll just read all.
Thus final solution.
But before finalizing, let's test a custom case where low != high but there are many values equal to high, and some values equal to low. Example: N=8, values: [1,2,3,4,5,6,7,8] already done. Another: [1,1,1,2,3,4,5,6]. Sorted: [1,1,1,2,3,4,5,6]. N/2=4, low = A[3] = 2, high = A[4] = 3. Condition: X_i <=2 -> print 3; else print 2. Let's compute manually: Remove a 1: remaining 7 numbers, median 4th smallest? Actually N/2=4? Wait N=8, N/2=4, median of 7 is 4th smallest. After removing a 1, sorted remaining: [1,1,2,3,4,5,6] -> 4th = 3. So B=3. Good. Remove a 2: [1,1,1,3,4,5,6] -> 4th = 3? Sorted: 1,1,1,3,4,5,6 -> 4th=3, B=3. But condition says X_i <= low (2) includes 2, so prints 3, correct. Remove a 3: [1,1,1,2,4,5,6] -> sorted: 1,1,1,2,4,5,6 -> 4th = 2. So B=2. Condition: X_i=3 >2 -> prints 2, correct. Remove a 6: [1,1,1,2,3,4,5] -> sorted: 1,1,1,2,3,4,5 -> 4th = 2, B=2. Condition: X_i=6 >2 -> prints 2. Good.
Now, consider case where low == high but there are values both below and above. Example earlier: [1,2,3,3,4,5], low=3, high=3. Condition: X_i <=3 -> 3; else 3. All good.
Thus solution holds.
Now, about the correctness proof: we already reasoned.
Thus final answer: sort, get low and high, then for each element output high if element <= low else low.
Now, potential pitfalls: The condition is X_i <= low. But if low == high, both branches output same, so it's fine. If low != high, then condition correctly partitions.
Edge case: What if low is the maximum value? Could low be the maximum? That would require that the N/2-th smallest is the maximum, meaning at least N/2 numbers are maximum? But if maximum is M, and it's the N/2-th smallest, then there are at least N/2 numbers <= M (obviously all numbers <= M). But for low to be maximum, all numbers must be equal to M? Because if there is any number less than M, then that number would be <= M but not equal; but low is the N/2-th smallest, so if there is any number less than M, then N/2-th smallest might be less than M. Actually, could we have low = M but high > M? Not possible because high is (N/2+1)-th smallest, which would be >= low. If low = M (the maximum), then there cannot be any number > M, so high = M also. So low == high. So the case low != high cannot have low being the maximum. So safe.
Similarly, could high be the minimum? Not possible unless all equal.
Thus condition covers all.
Now, let's see if we could solve in O(N) using nth_element to get low and high without full sort. But N log N is acceptable.
Implementation in Python would be similar.
Now, confirm the problem constraints: N up to 200000, even. So O(N log N) is fine.
Now, we need to produce output as per examples. We'll code accordingly.
But before final code, one more test: N=200000 with all distinct random numbers. Sorting fine.
Now, I'll produce the solution in C++.
Pseudo:
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector X(N);
for (int i = 0; i < N; ++i) cin >> X[i];
vector S = X;
sort(S.begin(), S.end());
int low = S[N/2 - 1];
int high = S[N/2];
for (int i = 0; i < N; ++i) {
if (X[i] <= low) cout << high << '\n';
else cout << low << '\n';
}
return 0;
}
Now, ensure that N/2 - 1 is valid for N>=2. Yes.
Now, we should consider the possibility of integer type: int is fine.
Now, potential bug: For large N, the vector copying is fine.
Now, perhaps we can avoid copying by sorting a copy or sorting X itself and then using it? But we need original X to compare. So we need to keep original unsorted. So copy is fine.
Alternatively, we can sort indices or store original separately. That's what we do.
Now, memory: O(N) for X and S, fine.
Now, let's test with provided examples.
Example1: N=4, X=[2,4,4,3], S sorted=[2,3,4,4], low=3, high=4. Loop:
2<=3 ->4
4<=3? false ->3
4<=3? false ->3
3<=3 ->4. Output matches.
Example2: N=2, X=[1,2], S=[1,2], low=1, high=2. 1<=1 ->2, 2<=1? false ->1. Output matches.
Example3: N=6, X=[5,5,4,4,3,3], S=[3,3,4,4,5,5], low=4, high=4. All output 4.
Now, I want to test with a case where X_i <= low but X_i is actually > low? Impossible. So fine.
Now, ensure we use fast I/O because N=200k, input output large. Use ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);.
Now, final code.
But wait, is there any scenario where this fails because we use value comparison instead of rank? Let's think of a scenario where low != high, but there exists an element with value = low that is actually in the second half due to some weird sorting? But as argued, if low != high, count of elements <= low = N/2. So exactly N/2 elements are <= low. Since there are N elements, the first N/2 positions in sorted order must be occupied by exactly those N/2 elements. So all elements with value <= low are in the first N/2 positions; none with value <= low can be in the second half. So any element with value <= low indeed has rank <= N/2. So condition X_i <= low is equivalent to rank <= N/2. Good.
Now, what about element with value > low? Could it be in first half? No, because first half is fully occupied by <= low elements, so any element > low must be in second half. So condition X_i > low is equivalent to rank > N/2. So correct.
Thus algorithm is valid.
Now, we might consider the scenario where there are multiple values equal to low and some values less than low. The count of <= low is N/2, so exactly N/2 elements satisfy <= low. So condition partitions correctly.
Now, I think we've covered everything.
Thus final answer: implement.
Now, reflect if there is any need to handle input where values are repeated? Already covered.
Now, one more check: The problem states "When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the ( (l+1)/2 )-th largest value among a_1, a_2, ..., a_l." So for each i, we compute median of the remaining N-1 numbers. N-1 is odd because N is even. So median is the (N/2)-th largest? Let's compute: l = N-1, odd. (l+1)/2 = (N-1+1)/2 = N/2. So the N/2-th largest. Since "largest" means descending order, it's the same as N/2-th smallest? As argued, for odd l, the k-th largest equals the k-th smallest. So it's fine. But we need to be careful: In descending order, the median is the N/2-th largest. For a set of numbers, that is the element that would be at position N/2 from the top. That is the same as the element at position (l - N/2 + 1) = (N-1 - N/2 + 1) = N - N/2? Actually let's compute: l = N-1. The N/2-th largest in descending order corresponds to the (l - N/2 + 1)-th smallest. l - N/2 + 1 = (N-1) - N/2 + 1 = N - N/2 = N/2. So indeed it's the N/2-th smallest. So consistent.
Now, the problem examples confirm.
Thus solution ready.
Now, I'll produce final answer with explanation.
But wait, there is a twist: The input says "All values in input are integers." But it doesn't specify that X_i are distinct. So duplicates allowed, and we handled.
Now, double-check the condition for N=2: low = first element, high = second element. Condition: if X_i <= low then high else low. For (2,1) unsorted? But we sort to get low and high. Example: X=[2,1], sorted = [1,2], low=1, high=2. For X_i=2: 2<=1? false -> output low=1. For X_i=1: 1<=1 true -> output high=2. So output 1 and 2? But the example for N=2 gave input 1 2 and output 2 1. In our example, if input is 2 1, what would be output? Let's compute manually: For X=[2,1], remove first (2): remaining [1], median? Wait N=2, N-1=1, median is the only number, so 1. Remove second (1): remaining [2], median 2. So outputs should be 1 and 2? Actually i=1 remove X1=2 -> B1=1, i=2 remove X2=1 -> B2=2. So output order: 1,2? But the example input 1 2 gave output 2 1. That is symmetric. So for input 2 1, output should be 1,2. Our algorithm for input 2 1: sorted low=1, high=2, then for X1=2 <=1? false -> low=1; for X2=1 <=1 true -> high=2. So outputs 1,2. That matches manual. So it's consistent regardless of order.
Now, check the example: Input 2: 1 2 -> output 2 1. Our algorithm gives: sorted low=1, high=2, X1=1 <=1 -> 2; X2=2 <=1? false ->1. So 2,1. Works.
Thus all good.
Now, we might think about the possibility of using nth_element twice to find low and high in O(N) average. But not necessary.
Thus final solution is as described.
Now, we must write solution in the answer. Since the user asked "When l is an odd number... You are given N numbers... Find B_i" and we need to provide solution. They might expect explanation of reasoning and then code.
Thus we'll produce a clear explanation and code.
Now, I'll write the final answer. We are given an even number ( N ) and a list ( X_1, \dots, X_N ). For each ( i ) we must compute the median of all numbers except ( X_i ). Because ( N ) is even, ( N-1 ) is odd, and the median of an odd-sized list is the element at position (\frac{(N-1)+1}{2} = \frac{N}{2}) when the numbers are sorted in non‑decreasing order (1‑indexed).
Let ( A ) be the sorted version of the whole list.
Define
[
\text{low} = A_{N/2} \quad (\text{the } \frac{N}{2}\text{-th smallest}), \qquad
\text{high} = A_{N/2+1} \quad (\text{the } \frac{N}{2}+1\text{-th smallest}).
]
When we remove one element, the new median can only be either low or high, depending on which side the removed element lies:
- If the removed element is ≤ low, then it belongs to the first half of the sorted list. After its removal there are fewer than ( N/2 ) elements ≤ low, so the median becomes high.
- Otherwise (the removed element is > low), it belongs to the second half. The count of elements ≤ low stays at least ( N/2 ), so the median remains low.
Note that when ( \text{low} = \text{high} ) (which happens when the two middle values coincide) both cases give the same answer, so the rule is still correct.
Therefore, after sorting the entire list to obtain low and high, we can answer each ( i ) by checking whether ( X_i \le \text{low} ).
Complexity: ( O(N \log N) ) for sorting, ( O(N) ) additional time and memory.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<int> X(N);
for (int i = 0; i < N; ++i) cin >> X[i];
vector<int> S = X;
sort(S.begin(), S.end());
int low = S[N/2 - 1]; // 0‑based index: N/2‑th smallest
int high = S[N/2]; // 0‑based index: (N/2+1)‑th smallest
for (int i = 0; i < N; ++i) {
if (X[i] <= low)
cout << high << '\n';
else
cout << low << '\n';
}
return 0;
}