We need to compute the minimal number of elements to add (any integers from 1 to 10^5) to an array of size n so that after sorting the resulting array (original + added), the median equals x. The median is defined as the element at position floor((m+1)/2) after sorting, where m = n + k (total elements after adding k elements). Actually careful: "a median in an array with the length of n is an element which occupies position number floor((n+1)/2) after we sort the elements in the non-decreasing order". So median is the element at position ceil(n/2) (since floor((n+1)/2)). That's the typical lower median (since for even length, they take the lower one). So given total size N (after additions), the median is the element at index = floor((N+1)/2) (1-indexed). So we want that element to equal x.
We can add any numbers (including x) any number of times. The goal is minimize number of additions.
Observation: The median being x means that after sorting the final array, the element at position p = floor((n+k+1)/2) is exactly x. Also we cannot change initial elements. So we can add numbers that are ≤ x or > x to influence the ranking.
We want minimal k.
Let’s denote original array A of size n. Sort A. We'll consider counts of numbers less than x, equal to x, greater than x.
Let L = count of numbers < x, E = count of numbers = x, G = count of numbers > x.
We can add any numbers, but the optimal additions should be either x (to increase count of x) or numbers less than x (to shift median upward?) Actually we need the median to be x. The median position depends on total count.
Think: After adding some numbers, we have new array with total size N = n + k. Let L' = number of elements < x in final array, E' = number equal to x, G' = number > x. Obviously L' + E' + G' = N.
For the median to be x, the element at position p = floor((N+1)/2) must be x. In sorted order, all elements less than x come first, then all x's, then all greater than x. So x is at positions from L'+1 to L'+E' inclusive. We need p to be within that range.
Thus we need L' < p ≤ L' + E'. Since p is integer, condition: L' < p ≤ L' + E'.
Equivalently: p must be greater than L' and less than or equal to L' + E'.
So we need to achieve this by adding numbers (which affect L', E', G' and N) with minimal k.
We can add numbers:
- numbers less than x → increase L' (and N)
- numbers equal to x → increase E' (and N)
- numbers greater than x → increase G' (and N)
But adding numbers > x increases G', which may affect median if they shift the position of x? Actually they increase N and p, but also increase G' which might affect the condition? Condition only involves L' and E'. Adding > x numbers increases N, thus p = floor((N+1)/2) also increases. Also L' and E' unchanged. So adding > x numbers makes it harder to satisfy condition because p moves rightwards while L' and E' stay same. So typically we would not add > x numbers unless needed? Possibly if we need to increase N to adjust p relative to L' and E', but adding > x also increases N without increasing L' or E', which could move p beyond L'+E' if p becomes too large. So adding > x is generally detrimental to achieving median = x, unless we also add many x's to increase E'? But adding > x could help if we need to increase p to be within E'? Let's think: The condition is p ≤ L'+E'. If we add > x, N increases, p increases, L'+E' unchanged, so RHS unchanged; thus the inequality p ≤ L'+E' becomes harder. Also L' < p: adding > x increases p, but L' unchanged, so L' < p becomes easier (since p grows). So the lower bound is easier, upper bound harder. So if p is too low (p ≤ L')? Actually condition requires p > L'. If originally p is ≤ L', we need to increase p (or decrease L') to satisfy L' < p. Adding > x increases p, helpful. But if p > L' already, adding > x might break the upper bound. So maybe we need a combination.
Alternatively, adding numbers < x increases L' and N, so both L' and p change. The effect on p and L' relative? Let's compute.
We have initial L, E, G, n.
We can choose to add a numbers: a_L of < x, a_E of = x, a_G of > x, with a_L + a_E + a_G = k.
Then final: L' = L + a_L, E' = E + a_E, G' = G + a_G, N = n + k.
p = floor((N+1)/2) = floor((n + k + 1)/2).
We need: L' < p ≤ L' + E'.
Goal: minimize k.
We can think of selecting k and possibly the a's to satisfy condition. Since we can choose any numbers, we are free to allocate adds arbitrarily, but the minimal k will be achieved by a combination that meets the inequality.
We can consider that adding numbers equal to x is always beneficial: it increases E', making the upper bound larger; also increases N and p (which might be okay). Adding numbers less than x increases L', which makes the lower bound harder (since we need p > L'), but also increases p (since N increases). The net effect on p - L'? Let's analyze.
Define D = p - L'. Need D ≥ 1 (since p > L'). Also need p - (L' + E') ≤ 0 (i.e., p ≤ L'+E').
Rewrite second: E' ≥ p - L'.
Thus both conditions essentially: 1 ≤ p - L' ≤ E'.
So the difference p - L' must be at least 1 and at most E'.
We can control L' and E' and p (via N). So we need to achieve that the offset between median index and count of < x falls within the count of x.
Initially, compute N0 = n, p0 = floor((n+1)/2). L0 = L, E0 = E, G0 = G.
Compute d0 = p0 - L0. This is the offset. Initially, if 1 ≤ d0 ≤ E0, then median already x? Not exactly, because the median might be x only if the element at position p0 is x, which is equivalent to d0 being between 1 and E0 inclusive. But need also that the numbers at position p0 are exactly x, which requires that L0 < p0 ≤ L0+E0, same condition. So if condition holds, answer is 0? Check: if original median equals x, then answer 0. But in sample 1, original array (10,20,30): n=3, p=2. L=0 (numbers <10?), Wait numbers: 10,20,30. x=10. Numbers <10 =0, E=1 (10), G=2. p=2. Check: L=0 < 2? yes. p ≤ L+E? 2 ≤ 0+1=1? No, 2 ≤ 1 false. So median not x. So condition fails.
So if condition holds initially, k=0.
Otherwise, we need to add elements.
Approach: We can treat k as variable, and we can choose to add only numbers equal to x? Or only numbers less than x? Or a mix.
Let's consider possibilities.
Goal: after additions, we need p - L' in [1, E'].
We can vary L' and E' by adding numbers. Adding numbers < x increases L' and also N (=> p changes). Adding numbers = x increases E' and N (p changes). Adding numbers > x increases only N (p changes) and G' (unused). So any addition changes N and p.
We might try to minimize k, so we consider whether it's possible to achieve condition with a given k.
Observation: Since we can only add numbers, we can increase L', E', and N. We cannot decrease L or G. So L' ≥ L, E' ≥ E, N = n + k, p is determined by N.
Thus we have constraints.
Let's denote after additions, the condition:
(1) p > L' => L' ≤ p - 1.
(2) p ≤ L' + E' => L' ≥ p - E'.
But L' is integer. Combine: p - E' ≤ L' ≤ p - 1.
Also L' = L + a_L, E' = E + a_E, with a_L, a_E ≥ 0 integers. And we also have G' = G + a_G, but a_G doesn't appear directly except through N: k = a_L + a_E + a_G.
We can choose a_G freely (including 0). Adding a_G only increases N, p, and increases k. Since we want minimize k, we likely set a_G = 0, because adding > x does not help with the condition (makes L' ≤ p-1 easier but p ≤ L'+E' harder). However, maybe there is scenario where we need to increase p to be larger than L'? But we can increase p by adding any numbers (including =x or <x) anyway, and adding >x only increases p without helping L' or E'. Actually adding >x increases p and also adds to G' which does not help; but if we need to increase p relative to L'? Since L' also may increase with other adds. But adding >x only increases p, not L' or E', so it could help achieve L' < p if p is too low. But we could also increase p by adding numbers = x, which simultaneously increase E' and p, which is better because it helps the upper bound. So adding > x seems never beneficial over adding x, because adding x gives same increase to p and N, plus it increases E', which helps the upper bound. Similarly, if we need to increase p but keep L' from increasing too much? Adding < x increases L' as well, which might be undesirable if L' is already high. So adding > x might be used if we need to increase p without increasing L' and without increasing E'? But why wouldn't we want to increase E'? It always helps the upper bound. So it's never harmful to increase E' (since it makes the upper bound larger). However, if we already have enough E' to satisfy the upper bound, we may not need to add more x. But adding x still increases p, potentially making the upper bound harder? Let's examine: condition p ≤ L'+E'. If we add x, L' unchanged, E' increases by 1, N increases by 1, p might change (increase by either 0 or 1 depending on parity). Adding x could increase p by either 0 or 1 (since floor((N+1)/2)). Actually p = floor((N+1)/2). When N increments by 1, p either stays same or increases by 1. Let's compute delta: For integer N, floor((N+1)/2) increments by 0 when N is even? Let's check: N=1: p=1; N=2: p=1; so +0. N=2: p=1; N=3: p=2; +1. So pattern: p increases by 1 when N becomes odd? Actually floor((N+1)/2) = ceil(N/2). So p = ceil(N/2). When N increases by 1, ceil(N/2) increases by 0 if N goes from even to odd? Let's test: N=2 => ceil(1)=1? Wait careful: ceil(N/2): N=2 => 1, N=3 => 2 => +1. N=3 => 2, N=4 => 2 => +0. So when N is odd to even, p stays same; even to odd, p increases by 1. So adding one element might or might not increase p.
Thus adding x may increase p or not. If p stays same, then L' unchanged, E' increases, so condition p ≤ L'+E' becomes easier (since RHS increases). If p increases, then condition becomes both sides change: p increases by 1, E' increases by 1, L' unchanged. So p - L' increases by 1, and E' increases by 1, so the upper bound condition p ≤ L'+E' becomes? LHS p becomes p+1, RHS becomes (L' + E' +1). So if previously p ≤ L'+E', then after both increase, inequality still holds (since both sides increased by 1). Actually if initially p <= L'+E', then adding 1 to both sides preserves inequality. So adding x never harms the upper bound; it either improves or keeps same. And lower bound: we need p > L'. If p increases by 1, then p - L' increases by 1, so lower bound becomes easier; if p doesn't increase, then p - L' unchanged, but L' unchanged, so lower bound unchanged. So adding x is always beneficial or neutral for both inequalities. So if we need to satisfy both, adding x is always good.
Similarly, adding < x: L' increases by 1, E' unchanged, N increases by 1, p may increase or stay. Let's analyze effect on conditions.
Let delta p = dp (0 or 1). Then new L' = L'+1. New p = p + dp.
Condition lower: need new p > new L' => p+dp > L'+1 => (p - L') + dp > 1. Initially we have some gap. If dp=0, then new condition requires (p - L') > 1; if dp=1, requires (p - L') +1 > 1 => (p - L') > 0, i.e., p - L' >=1? Actually >0 means at least 1? Since integers, p - L' >=1. So lower bound might become stricter if we add < x when dp=0, because we need larger gap.
Upper condition: need p+dp ≤ (L'+1) + E' = L' + E' +1 => p - L' + dp ≤ E' +1? Actually rearrange: p+dp ≤ L'+E'+1 => (p - L') + dp ≤ E' +1? Wait subtract L': p - L' + dp ≤ E' +1? Let's do: p+dp ≤ L'+E'+1 => p - L' + dp ≤ E' +1. Not straightforward. But compare to original condition: originally we needed p ≤ L'+E'. Adding < x increases L', which may help upper bound? Because now RHS becomes L'+E'+1, but LHS increases by dp. If original condition held, after addition it may still hold? Let's test: Suppose original p ≤ L'+E' holds. After adding < x: new p' = p+dp, new L'' = L'+1, new E' unchanged. Need p+dp ≤ (L'+1)+E' = L'+E'+1. Since p ≤ L'+E', the worst case is p = L'+E'. Then we need L'+E' + dp ≤ L'+E'+1 => dp ≤ 1, which is true. So if original condition held, it will still hold after adding < x. But if original condition didn't hold, adding < x might help? Possibly, because RHS increased by 1, LHS increased by dp (0 or 1). So net RHS increase at least 0? Actually RHS increased by 1, LHS increased by dp ≤ 1. So it could make condition hold if the deficit was exactly 1 and dp=0. But if dp=1, then both increase by 1, no net change. So adding < x could help upper bound if dp=0, because RHS increase > LHS increase. However, adding < x also increases L', which might harm lower bound.
Adding > x: L' unchanged, E' unchanged, N increases, p changes (dp 0 or 1). Lower condition: need p+dp > L' => p - L' + dp > 0. If original p > L', then remains true if dp=0 or 1? If original p > L' holds, then after adding > x, p+dp > L' if original p > L' or p = L'+ something. Actually if original p > L', then p - L' >=1. If dp=0, new p = p, so still > L'. If dp=1, p+1 > L' certainly. So lower condition remains satisfied if originally satisfied. If original p <= L', then we need to check. Upper condition: need p+dp ≤ L'+E'. Original RHS unchanged, LHS increases by dp. So upper condition becomes harder. So adding > x can only help lower condition (by increasing p) but hurts upper condition. Since we can also help lower condition by adding x (which helps upper condition), it's generally better to add x rather than > x. So for minimizing k, we likely never add > x.
Similarly, adding < x may be necessary to adjust L' and p in some balance, but we need to see if we can always achieve condition by only adding x? Let's test.
Suppose we only add x's. Then L' = L, E' = E + k, N = n + k. Need: L < p(k) ≤ L + (E + k), where p(k) = ceil((n+k)/2). So condition becomes:
(1) L < p(k)
(2) p(k) ≤ L + E + k.
Let's denote p(k) = ceil((n+k)/2). We need to find smallest k >=0 integer such that both hold.
Inequality (1): L < ceil((n+k)/2). Since L is integer, that means ceil((n+k)/2) ≥ L+1.
Inequality (2): ceil((n+k)/2) ≤ L + E + k.
Now we can attempt to solve for k.
Define f(k) = ceil((n+k)/2) = floor((n+k+1)/2).
We need to find minimal k ≥ 0 s.t.
f(k) ≥ L+1 (i.e., f(k) > L)
and f(k) ≤ L+E+k.
If such k exists, answer is k (since we only add x). If not, maybe need to add some numbers < x to adjust.
Check if always possible with only x? Let's examine borderline cases.
We have L+E+G = n.
Inequality (2) can be rewritten: f(k) - k ≤ L+E.
Note f(k) = ceil((n+k)/2) = (n+k + 1)//2 (integer division rounding up). Let's compute f(k) - k = ceil((n+k)/2) - k.
We can consider two parity cases.
Let t = n+k.
If t is even: t = 2m, then f(k) = m? Actually ceil(t/2) = t/2 = m if t even? Since t/2 is integer, ceil = t/2 = m. So f(k)= (n+k)/2.
If t odd: t = 2m+1, then f(k) = (t+1)/2 = m+1. So f(k)= (n+k+1)/2.
So:
- If (n+k) even: f(k) = (n+k)/2.
- If (n+k) odd: f(k) = (n+k+1)/2.
Then f(k) - k = (n - k)/2 for even case? Let's compute:
Case even: n+k even => f = (n+k)/2, so f - k = (n+k)/2 - k = (n+k - 2k)/2 = (n - k)/2.
Case odd: n+k odd => f = (n+k+1)/2, so f - k = (n+k+1)/2 - k = (n+k+1 - 2k)/2 = (n - k + 1)/2.
So f(k) - k = floor((n - k + 1)/2)? Actually both expressions can be combined as: f(k) - k = ceil((n - k)/2). Because (n - k)/2 ceil? Let's check: For even n+k, n - k may be even? Not necessarily. But we can note: f(k) = ceil((n+k)/2). Then f(k) - k = ceil((n+k)/2) - k = ceil((n - k)/2)? Since ceil((n+k)/2) - k = ceil((n - k)/2)? Let's verify: ceil((n+k)/2) - k = ceil((n - k)/2)? For integer k, ceil((n+k)/2) - k = ceil((n - k)/2) indeed? Consider A = ceil((n+k)/2). Multiply (n - k)/2 + k = (n+k)/2, not straightforward. But we can use algebra: Let u = n - k. Then n+k = n - k + 2k = u + 2k. Then ceil((u+2k)/2) = ceil(u/2 + k) = k + ceil(u/2) because ceil(a + integer) = integer + ceil(a). So indeed f(k) = k + ceil((n - k)/2). So f(k) - k = ceil((n - k)/2). Good.
Thus inequality (2): f(k) ≤ L + E + k <=> k + ceil((n - k)/2) ≤ L + E + k <=> ceil((n - k)/2) ≤ L + E.
Since k cancels, we get a condition independent of k! Actually interesting: ceil((n - k)/2) ≤ L + E. This must hold for some k. But note that L+E is the count of numbers ≤ x in original array (i.e., numbers less than or equal to x). Denote LE = L + E.
The inequality simplifies to: ceil((n - k)/2) ≤ LE.
But note k is variable, and appears on left inside ceil. As k increases, (n - k) decreases, so ceil((n - k)/2) also decreases (or stays same). For large k, (n - k) becomes negative? But k cannot exceed n? Actually k can be any nonnegative integer. But (n - k) could become negative. However ceil of a negative number? Ceil of negative is less than or equal to that number? For example, if n - k = -1, then (-1)/2 = -0.5, ceil is 0? Actually ceil(-0.5) = 0 (since ceil rounds up to the greater integer: -0.5 → 0). So for negative arguments, ceil may be 0 or positive. Let's treat properly.
We require inequality to hold. Since left side is nonnegative? Actually ceil((n - k)/2) can be negative? If (n - k) is very negative, say -5, then (-5)/2 = -2.5, ceil = -2. That would be ≤ LE? Since LE is nonnegative, but -2 ≤ LE always true. So for sufficiently large k, the left side becomes negative (or zero) and automatically ≤ LE. So inequality (2) is automatically satisfied for large enough k? Wait, is it always true that for large k, left side becomes negative? But note that as k increases, n - k becomes negative; the ceil of a negative number is still negative (or zero). For example, n - k = -1 => ceil(-0.5) = 0. n - k = -2 => (-1) => ceil(-1)= -1. So yes, it can become negative. So there will always be some k making (2) true. However, we also have inequality (1) to satisfy: L < f(k) = k + ceil((n - k)/2).
Now inequality (1): L < k + ceil((n - k)/2). This can be rewritten as L - ceil((n - k)/2) < k. Not as simple.
Alternatively, from f(k) = k + ceil((n - k)/2). So condition (1) becomes:
k + ceil((n - k)/2) > L.
Since ceil((n - k)/2) may be negative for large k, then the left side could be less than k. For large k, ceil((n - k)/2) might be negative, so k + negative = k - something. Still grows as k grows? For large k, n - k is negative large, ceil((n - k)/2) ≈ (n - k)/2 (since ceil for half-integers? Actually if n - k is negative integer, say -m, then ceil(-m/2) = - floor(m/2)? Let's compute: for m even, -m/2 is integer, ceil = -m/2. For m odd, -m/2 = -(m-1)/2 - 0.5, ceil = -(m-1)/2. So roughly -k/2. So k + ceil((n - k)/2) ≈ k - k/2 = k/2, which still grows with k. So (1) will eventually be satisfied as k increases. But we need minimal k.
So overall, by adding only x's, we need to find minimal k≥0 such that:
(A) k + ceil((n - k)/2) > L
(B) ceil((n - k)/2) ≤ L+E (which may already hold for some k; note that as k increases, left side decreases, so if it's not satisfied for k=0, it will become satisfied at some k, possibly after a few increments).
But we also have the possibility that (B) might be violated for small k? Let's test with sample 2: n=3, x=4, array [1,2,3]. Then L = count <4 = 3? Actually numbers: 1,2,3 all <4, so L=3, E=0, G=0. LE = L+E = 3. Condition (B): ceil((n - k)/2) ≤ 3. For k=0: ceil(3/2)=2 ≤3 true. For k=1: ceil(2/2)=1 ≤3 true. For any k, ceil((3-k)/2) ≤ something. So (B) is fine. Condition (A): need k + ceil((3 - k)/2) > L =3. Compute:
k=0: 0+ceil(3/2)=0+2=2 >3? false.
k=1: 1+ceil(2/2)=1+1=2 >3? false.
k=2: 2+ceil(1/2)=2+1=3 >3? false (needs >, not ≥). Actually > L, so need >3, 3 is not >3. So false.
k=3: 3+ceil(0/2)=3+0=3 >3? false.
k=4: 4+ceil(-1/2)=4+0? ceil(-0.5)=0 =>4 >3 true. So minimal k=4 if only adding x's. But sample answer is 4, and indeed they added numbers 4,5,5,5? Actually they added 4 (x) and three 5's (>x). But our calculation using only x gives k=4 as minimal? But we added only x's? For k=4, we would be adding four 4's. That would give array: [1,2,3,4,4,4,4]? Actually original n=3, add 4 x's => total 7 elements. Sort: 1,2,3,4,4,4,4. Median position p = ceil(7/2)=4. The 4th element is 3? Actually positions: 1:1,2:2,3:3,4:4,5:4,... So median is 4? Wait 4th element is 4. So median =4, yes! So with only adding four 4's, we get median 4. But sample solution added 4,5,5,5? They said: "you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4." That's also 4 additions but includes > x numbers. But adding only four 4's also works, and k=4, same minimal. So sample answer 4 is consistent with only adding x. But they used > x because maybe they thought only adding 4's? Actually they added one 4 and three 5's. But we can add four 4's, which is allowed? Yes, any integers from 1 to 10^5, including x, can be added multiple times. So adding four 4's is valid and yields median 4. So minimal k could be 4 indeed. So only x works for that sample.
Now sample 1: n=3, x=10, array [10,20,30]. L=0, E=1, G=2. LE=1. Condition (B): ceil((3 - k)/2) ≤ 1. For k=0: ceil(3/2)=2 ≤1? false. For k=1: ceil(2/2)=1 ≤1 true. So (B) satisfied for k>=1? Check k=1: (3-1)=2, ceil(2/2)=1 ≤1 ok. Condition (A): need k + ceil((3 - k)/2) > L=0. For k=0: 0+2=2 >0 true, but (B) fails. For k=1: 1+ceil(2/2)=1+1=2 >0 true. So minimal k=1 using only x. Indeed adding one 10? Let's test: original [10,20,30]; add one 10 => [10,10,20,30]; sorted: 10,10,20,30; total N=4, p=ceil(4/2)=2? Actually ceil(4/2)=2, median is 2nd element = 10. So works. So k=1 matches sample answer.
Thus seems adding only x might always achieve the minimal? Let's test more scenarios.
Consider case where original array already has median x: then condition holds for k=0, so answer 0.
But is there a case where adding only x cannot achieve condition with a smaller k than adding some < x numbers? Let's try to construct a counterexample.
We need to find a situation where adding x's alone would require a large k, but by adding some numbers < x we can reduce total additions.
Why would adding numbers < x help? Adding < x increases L', which could help satisfy condition (2) by making the right side larger? Actually condition (2) involves L'+E', adding < x increases L' but not E', so RHS increases by 1, LHS increases by dp (0 or 1). That could help if the deficit was in upper bound and dp=0, meaning we increase RHS by 1 while LHS unchanged, making p ≤ L'+E' easier. But adding x also increases E' and RHS by 1, and also possibly LHS. So both help upper bound similarly. However, adding < x also increases L', which might make condition (1) harder: need p > L'. Adding x does not increase L'. So adding x seems strictly better for satisfying (1). However, there is nuance: when we add x, p may increase or stay; when we add < x, p may stay or increase. Could there be a scenario where adding a < x gives a better net effect because it does not increase p (when N parity causes p not to increase) but increases RHS by 1? Adding x also increases RHS by 1, and p may or may not increase. If p does not increase when adding x (i.e., when N goes from even to odd? Actually adding one element: p = ceil(N/2). When N increments by 1, p stays same if N was odd? Let's recalc: For N, p = ceil(N/2). When we add one element, N -> N+1. Ceil((N+1)/2) compared to ceil(N/2). If N is even, say N=2m, p=m. Then N+1=2m+1, ceil((2m+1)/2)=m+1, so p increases by 1. If N is odd, N=2m+1, p=m+1. Then N+1=2m+2, ceil((2m+2)/2)=m+1, so p stays same. So when N is odd before addition, adding any element does not change p; when N is even, p increases by 1.
So adding an element may or may not increase p, depending on current total parity. So the effect of adding an element depends on the parity of N before addition.
Thus adding x when N is odd yields p unchanged, RHS (L'+E') increases by 1, so upper bound condition becomes easier, and lower bound unchanged (since p unchanged, L' unchanged). So it's beneficial. Adding x when N is even yields p increases by 1, RHS increases by 1, so both sides increase by 1; inequality p ≤ L'+E' remains same (if originally satisfied) or maybe improves if originally unsatisfied? Actually if originally p ≤ L'+E' was false by a margin, both increase by 1, the difference p - (L'+E') remains unchanged. So adding x when N even does not improve the upper bound gap; it only increases p and E' equally, leaving the difference unchanged. However, lower bound p > L' becomes easier because p increases while L' unchanged.
Now adding < x: when N is odd, p unchanged, L' increases by 1, RHS (L'+E') increases by 1 (since L' increases), so p unchanged, L' increased, so lower bound p > L' becomes harder because L' increased. Upper bound: p unchanged, RHS increased by 1, so becomes easier. So adding < x when N odd: helps upper bound, hurts lower bound. When N even, adding < x: p increases by 1, L' increases by 1, RHS increases by 1. So p and L' both increase by 1, so p - L' unchanged. Upper bound: p increases by 1, RHS increases by 1, so p - (L'+E') unchanged (since E' unchanged). So adding < x when N even does not change either gap (both increase equally). So it's neutral for conditions, but increases k.
Adding > x: when N odd, p unchanged, L' unchanged, RHS unchanged, so conditions unchanged (k wasted). When N even, p increases by 1, L' unchanged, RHS unchanged, so lower bound improves, upper bound worsens.
Thus the most efficient additions are likely those that improve the gap needed. Typically, we need to satisfy both inequalities. Let's characterize the gaps.
Define initial state: L, E, n.
We are allowed to add numbers. We can think in terms of needed adjustments.
Let’s denote after k additions, we want p - L' in [1, E'].
We can consider the current offset d = p - L before any additions (p computed from n). But careful: p depends on n, and after additions p changes. However, we can think in terms of achieving the condition.
We need to find minimal k.
Maybe we can solve analytically: The minimal number of additions is something like:
If median already x, 0.
Otherwise, compute needed adds to make median x.
We can consider that we can only add numbers ≤ x or x itself, because adding > x seems suboptimal.
But is there a scenario where adding < x yields a lower total k than adding only x? Consider a case where adding only x would require many additions because the parity of additions leads to p increasing too much relative to L, perhaps causing the lower bound to be satisfied only after many adds, but adding some < x might adjust L' to help lower bound with fewer total adds? However, adding < x also increases L' which is detrimental for lower bound (p > L'). So it seems adding < x would only help upper bound but hurt lower bound. So it's generally not beneficial if lower bound is the limiting factor.
What if upper bound is the limiting factor? Adding x helps upper bound when N odd (by increasing E' and not p) and when N even also increases both equally, so net gap p - (L+E) remains same? Actually upper bound condition: p ≤ L+E. Suppose we have deficit: p > L+E. We need to reduce p - (L+E). Adding x: when N even, p and E both increase by 1, so p - (L+E) unchanged. When N odd, p unchanged, E increases by 1, so p - (L+E) decreases by 1. So adding x helps reduce the deficit only when N is odd before addition. Adding < x: when N odd, p unchanged, L increases by 1, so p - (L+E) decreases by 1? Actually upper bound condition involves L+E: we need p ≤ L+E. Adding < x when N odd: p unchanged, L increases by 1, so L+E increases by 1, so p - (L+E) decreases by 1 (if we consider deficit). So adding < x also reduces deficit by 1 when N odd. But it also increases L, which might affect lower bound. So when N odd, both adding x and adding < x reduce the deficit by 1 (by increasing E or L respectively). Adding x does not increase L, which is better for lower bound. So still x is better.
Now consider lower bound deficit: we need p > L. Initially maybe p ≤ L (median is less than x). That means the median position is within the numbers less than x, so we need to increase p relative to L. Adding x: when N even, p increases by 1, L unchanged, so p - L increases by 1. When N odd, p unchanged, L unchanged, so no effect. So adding x helps lower bound only when N is even. Adding < x: when N even, p increases by 1, L increases by 1, so p - L unchanged; when N odd, p unchanged, L increases by 1, so p - L decreases by 1. So adding < x never helps lower bound; it either hurts or does nothing. Adding > x: when N even, p increases by 1, L unchanged, so helps lower bound; when N odd, no effect. So for lower bound deficit, adding > x when N even could help similarly to adding x when N even. But adding > x hurts upper bound. However, if upper bound is not a problem (maybe we have many x's already), then adding > x might be as good as adding x for lower bound? But adding x also helps upper bound, which is beneficial or neutral. So adding > x seems strictly worse unless we are constrained that we cannot add x? But we can always add x. So adding > x is never better than adding x. However, could there be a scenario where adding x is not allowed? It's allowed. So it seems optimal strategy is to only add x. But wait, there might be a scenario where adding only x cannot achieve the condition because the condition (B) might never hold? But we argued (B) will eventually hold for large k, because left side decreases. However, condition (A) may also require increasing k, but both can be satisfied simultaneously by large enough k. But is it always possible to satisfy both by adding only x? Let's check thoroughly.
We need to find k such that:
(1) L < k + ceil((n - k)/2)
(2) ceil((n - k)/2) ≤ L+E.
Define A(k) = k + ceil((n - k)/2) - (L+1) >? Actually condition (1) is strict >. So we need k + ceil((n - k)/2) ≥ L+1 (since integer). Because L < f(k) means f(k) ≥ L+1.
(2) is ceil((n - k)/2) ≤ L+E.
Let’s denote g(k) = ceil((n - k)/2). Then conditions:
(A) k + g(k) ≥ L+1
(B) g(k) ≤ L+E
We want minimal k ≥ 0.
We can analyze function g(k). Since k integer, g(k) = ceil((n - k)/2). This is a non-increasing function as k increases, but not strictly decreasing each step; it decreases by 1 every two increments of k? Actually when k increases by 1, (n - k) decreases by 1, so ceil((n - k)/2) either stays same or decreases by 1 depending on parity. More precisely, g(k+1) = ceil((n - (k+1))/2) = ceil((n - k - 1)/2). This equals either g(k) or g(k)-1. Specifically, if (n - k) is even, then (n - k)/2 is integer, g(k) = (n - k)/2. Then (n - k - 1)/2 = (n - k)/2 - 0.5, ceil = (n - k)/2, so g(k+1) = g(k). If (n - k) is odd, then (n - k) = 2m+1, g(k) = ceil((2m+1)/2) = m+1. Then (n - k - 1) = 2m, g(k+1) = ceil(m) = m = g(k) - 1. So g decreases by 1 when n-k is odd, stays same when n-k even.
Now consider (B): g(k) ≤ L+E. As k increases, g(k) eventually becomes ≤ L+E, and then stays at or below that threshold for all larger k? Not necessarily, because g(k) can bounce? It is non-increasing, so once it goes below or equal to L+E, it will stay ≤ L+E for all larger k (since it never increases). So (B) will hold for all k ≥ some k0, where k0 is the smallest k such that g(k) ≤ L+E.
Now (A): k + g(k) is a function that overall increases with k? Let's check behavior. Compute h(k) = k + g(k). For k=0, h(0) = 0 + ceil(n/2) = ceil(n/2). For k=1, h(1) = 1 + ceil((n-1)/2). Compare h(1) vs h(0). If n is even, n=2m: h(0)=m, ceil((n-1)/2)=ceil((2m-1)/2)=m, so h(1)=1+m = m+1 > h(0). If n is odd, n=2m+1: h(0)=ceil((2m+1)/2)=m+1, ceil((2m)/2)=m, so h(1)=1+m = m+1 = h(0). So h(k) increases by either 1 or 0 when k increases by 1. Specifically, when (n - k) is even (i.e., g(k) = g(k+1)? Actually above: if n - k even, then g(k+1)=g(k) (since (n - k) even -> g stays same), so h(k+1)=k+1+g(k)=h(k)+1. If n - k is odd, then g(k+1)=g(k)-1, so h(k+1)=k+1+g(k)-1 = k+g(k) = h(k). So h(k) increases by 1 when n-k is even, stays same when n-k is odd. Thus h(k) is non-decreasing, and will eventually exceed any fixed bound L+1 for sufficiently large k. Indeed as k→∞, g(k) ~ -k/2 for negative? But careful: when k > n, g(k) becomes ceil((negative)/2). For large k, n - k is negative large, and ceil of negative number is negative or zero. But h(k) = k + g(k). For large k, if n - k = -M, then ceil(-M/2) = -floor((M-1)/2)? Actually need to compute asymptotic: if k = n + 2t, then n - k = -2t, g = ceil(-t) = -t? Because -2t/2 = -t integer, ceil = -t. Then h = (n+2t) + (-t) = n + t, which grows linearly with t. So h(k) grows as k/2 roughly. So (A) will be satisfied for sufficiently large k.
Thus there exists some k such that both (A) and (B) hold. So adding only x always works for some finite k.
But the question: is the minimal k achievable by only adding x always optimal? Could adding some numbers not x reduce k below the minimal k that works with only x? We need to consider if by adding some < x numbers we could achieve condition with a smaller total k than the minimal k_x (the smallest k satisfying conditions for only x additions). We need to check if mixing could achieve k < k_x.
Consider we can add both x and < x. The condition becomes:
Let a = number of added x, b = number of added < x, c = number of added > x (but we argued c is never beneficial, so set c=0). Then total k = a + b.
Then L' = L + b, E' = E + a, N = n + a + b.
p = ceil((n + a + b)/2).
We need:
(1) L + b < p
(2) p ≤ (L + b) + (E + a) = L + E + a + b
Simplify: (1) p - b > L, but better keep as L + b < p => p ≥ L + b + 1.
(2) p ≤ L + E + a + b.
We need minimal a+b.
Let’s denote k = a+b. The inequalities:
p = ceil((n + k)/2). Notice p depends only on total k, not on a and b separately! Indeed N = n + k, so p depends only on k. Good. So p is a function of total size, not composition. So p = f(k) = ceil((n+k)/2).
Now the inequalities become:
(1) L + b < f(k)
(2) f(k) ≤ L + E + a + b = L + E + k (since a+b = k, so a = k - b). So (2) becomes: f(k) ≤ L + E + k.
But note (2) does not involve a or b separately beyond k? Actually L + E + k is independent of split, because a+b = k, so L + E + a + b = L + E + k. So (2) becomes f(k) ≤ L + E + k. This is exactly the same as condition (B) we derived for only x additions! Because when we only add x, a=k, b=0, we had f(k) ≤ L + E + k. Indeed it's the same condition: f(k) ≤ L + E + k. So (2) is independent of how many added x vs < x; it only depends on total k.
Now condition (1) becomes: L + b < f(k). Since b is the number of added < x, we have b ≥ 0. So (1) can be rewritten as:
b < f(k) - L.
Since b is nonnegative integer, we need existence of b such that 0 ≤ b ≤ something? Actually b can be at most k (since a = k - b ≥ 0). So we need to be able to choose b satisfying 0 ≤ b ≤ k and also b < f(k) - L. Additionally, b can be any integer in that range, as we can decide how many < x to add.
So the feasibility for given total k is: there exists integer b with 0 ≤ b ≤ k such that b < f(k) - L. Since b < f(k) - L, the maximum possible b we can choose is min(k, f(k)-L - 1) if f(k)-L-1 >= 0? Actually b must be less than f(k) - L, meaning b ≤ f(k) - L - 1 if f(k) - L is integer. Because b is integer, condition b ≤ floor(f(k) - L - epsilon) = f(k) - L - 1 if f(k) - L is integer? Let's be careful: L + b < f(k) => b < f(k) - L. Since b integer, this is equivalent to b ≤ floor(f(k) - L - 1) if f(k)-L not integer? Actually if f(k)-L is an integer, say d, then b < d means b ≤ d-1. If f(k)-L is not integer, then b ≤ floor(f(k)-L). But f(k) and L are integers, so f(k)-L is integer. So condition is b ≤ f(k)-L - 1.
So we need to have some b satisfying 0 ≤ b ≤ k and b ≤ f(k)-L-1.
This is possible if and only if the lower bound 0 ≤ the upper bound: i.e., 0 ≤ f(k)-L-1 (i.e., f(k)-L ≥ 1) and also there exists b ≤ min(k, f(k)-L-1). The minimal b is 0, so we can always choose b=0 if 0 satisfies b < f(k)-L, i.e., 0 < f(k)-L => f(k) > L. So if f(k) > L, we can set b=0 (i.e., add only x) and condition (1) holds. If f(k) ≤ L, then even with b=0, condition (1) fails. But we could try b > 0? Wait condition (1) is L+b < f(k). If f(k) ≤ L, then even with b=0, L < f(k) fails. If we increase b, L+b becomes larger, making it worse. So if f(k) ≤ L, condition (1) cannot be satisfied for any b ≥ 0 because L+b ≥ L ≥ f(k). So condition (1) requires f(k) > L. Actually we need existence of b such that L+b < f(k). Since b ≥ 0, the minimum LHS is L (when b=0). If L < f(k), then b=0 works. If L ≥ f(k), then even b=0 fails, and any b>0 makes LHS larger, so impossible. So condition (1) reduces to simply f(k) > L. Because we can always choose b=0 if that holds. If f(k) > L, we don't need to add any < x; we can set b=0 and condition (1) satisfied. If f(k) ≤ L, then no matter what b (including 0), condition (1) fails. So adding < x does not help to satisfy (1) when f(k) ≤ L; it makes it worse. So the only way to satisfy (1) is to have total k such that f(k) > L.
Thus the feasibility with any mixture reduces to two conditions:
(1') f(k) > L (i.e., L < ceil((n+k)/2))
(2') f(k) ≤ L + E + k (i.e., ceil((n+k)/2) ≤ L + E + k)
And we can always set b=0 if both hold. So the minimal k is the smallest integer k ≥ 0 satisfying both inequalities. That is exactly the same as for only adding x. Therefore, the minimal number of additions is independent of whether we add < x or > x; the optimal strategy is to add only x (or equivalently, add any numbers but effectively we can achieve with only x). So answer = minimal k satisfying:
f(k) > L and f(k) ≤ L + E + k.
But wait: earlier we had condition (1) strict: L < f(k). So that's f(k) > L. Condition (2) f(k) ≤ L+E+k.
Thus answer is the minimal k ≥ 0 such that:
ceil((n+k)/2) > L
and
ceil((n+k)/2) ≤ L+E+k.
Now note that if k=0 satisfies both, answer 0.
We need to compute L = count of elements < x, E = count of elements = x.
This seems straightforward.
But we must also consider that we cannot add negative numbers; we can only add numbers from 1 to 1e5. But x is within range, so adding x is allowed. Also we could add numbers < x, but we concluded it's not needed. So the minimal k is as above.
Let's test with examples.
Example 1: n=3, x=10, A=[10,20,30]: L=0, E=1. Conditions:
f(k)=ceil((3+k)/2).
Check k=0: f=2 >0 ok. f≤ L+E+k =0+1+0=1? 2≤1 false.
k=1: f=ceil(4/2)=2 >0 ok, f≤1+1=2? 2≤2 true. So k=1.
Answer 1.
Example 2: n=3, x=4, A=[1,2,3]: L=3, E=0. Conditions:
k=0: f=2 >3? false, so fails.
k=1: f=ceil(4/2)=2 >3? false.
k=2: f=ceil(5/2)=3 >3? false (needs >, 3>3 false).
k=3: f=ceil(6/2)=3 >3 false.
k=4: f=ceil(7/2)=4 >3 true; f ≤ L+E+k =3+0+4=7 -> 4≤7 true. So k=4.
Matches.
Now test other edge cases.
Case where median already x: n=5, array = [1,2,3,3,4], x=3. L=2 (1,2), E=2 (two 3's?), Actually there are two 3's? 3,3 so yes E=2, G=1 (4). n=5, p=ceil(5/2)=3. Condition: L=2 <3 true, p=3 ≤ L+E=4 true. So k=0.
Now case where L is large, but E is also large? Might need few adds.
Consider n=1, x=5, array=[1]. L=1, E=0. Conditions: f(k)=ceil((1+k)/2). We need f(k) > 1 and f(k) ≤ 1+0+k = 1+k. So:
k=0: f=1 >1? false.
k=1: f=ceil(2/2)=1 >1 false.
k=2: f=ceil(3/2)=2 >1 true; f ≤ 1+2=3 -> 2≤3 true. So answer 2. Let's see: add two 5's? Array becomes [1,5,5]; sorted [1,5,5]; N=3, p=2 -> second element =5. Yes median=5. Could we add one number? If we add one 5: [1,5], N=2, p=1? Actually p = ceil(2/2)=1, median=1, not 5. Add one number less than 5, say 4: [1,4], median=1. Add one number greater than 5: [1,6], median=1. So indeed need at least 2 adds. So answer 2.
Now consider n=2, x=3, array=[1,4]. L=1 (1), E=0, G=1 (4). Need f(k)=ceil((2+k)/2) > L=1 and ≤ L+E+k =1+k. Let's compute:
k=0: f=ceil(2/2)=1 >1? false.
k=1: f=ceil(3/2)=2 >1 true; f ≤ 1+1=2 -> 2≤2 true. So answer 1? Check: Add one 3? Array [1,3,4]; sorted [1,3,4]; N=3, p=2, median=3. So answer 1. That seems correct.
Now consider scenario where L=0 but E is 0 as well? e.g., n=2, x=2, array=[1,3]. L=1? Actually numbers <2: 1, so L=1, E=0? Wait x=2, array [1,3] => <2:1, =2:0, >2:1. So L=1. But suppose L=0, E=0, G>0? Example: n=3, x=10, array [11,12,13]. Then L=0, E=0, G=3. Conditions: f(k)=ceil((3+k)/2) >0 always true for k≥? f(0)=2>0 true. f(k) ≤ L+E+k = k. So we need f(k) ≤ k. For k=0: 2 ≤0 false. k=1: f=2 ≤1 false. k=2: f=ceil(5/2)=3 ≤2 false. k=3: f=ceil(6/2)=3 ≤3 true. So answer 3. Check: add three 10's: array [10,10,10,11,12,13] sorted: [10,10,10,11,12,13]; N=6, p=3? Actually ceil(6/2)=3? median 3rd element = 10. Works. Could we add 2 numbers? Add two 10's: [10,10,11,12,13]; N=5, p=3? ceil(5/2)=3, median = third element sorted: [10,10,11,12,13] => 11. Not 10. So need 3. So formula works.
Now consider scenario where L is very large relative to n. Example: n=5, x=100, array all numbers <100: say [1,2,3,4,5]. L=5, E=0. Conditions: f(k) > 5 and f(k) ≤ 5 + k. f(k)=ceil((5+k)/2). We need smallest k.
k=0: f=3 >5? false.
k=1: f=3 >5 false.
k=2: f=ceil(7/2)=4 >5 false.
k=3: f=ceil(8/2)=4 >5 false.
k=4: f=ceil(9/2)=5 >5 false (needs >, so 5>5 false).
k=5: f=ceil(10/2)=5 >5 false.
k=6: f=ceil(11/2)=6 >5 true; f ≤ 5+6=11 -> 6≤11 true. So answer 6. Let's test: add six 100's? Array becomes [1,2,3,4,5,100,100,100,100,100,100]; total 11 elements, median position ceil(11/2)=6, element at 6 is? sorted: [1,2,3,4,5,100,...] so 6th is 100. Yes median 100. Could we do with less? Suppose we add 5 numbers? total 10, p=5, median 5th element: [1,2,3,4,5,100,...] 5th is 5, not 100. So need 6. So answer 6.
Now consider scenario where E is large. Might require zero or few adds.
Thus algorithm: compute L, E from array. Then iterate k from 0 upwards until conditions hold? Since n ≤ 500, k might be up to something? In worst case, maybe we need many adds. But we can compute directly using formulas.
We need minimal k such that:
(1) ceil((n+k)/2) > L
(2) ceil((n+k)/2) ≤ L + E + k
Since both sides are integers, we can manipulate.
Let t = n + k. Then k = t - n, t ≥ n integer.
Then p = ceil(t/2). Conditions become:
p > L => ceil(t/2) > L
p ≤ L + E + (t - n) => ceil(t/2) ≤ L + E + t - n
We can think in terms of t.
Alternatively, we can solve inequalities for k.
Inequality (1): ceil((n+k)/2) > L.
Since ceil((n+k)/2) > L implies (n+k)/2 > L? Actually ceil(q) > L is equivalent to q > L, because if q > L, then ceil(q) ≥ ceil(L+ε) >= L+1? But careful: L integer. ceil(q) > L means ceil(q) ≥ L+1, which is equivalent to q > L (since if q ≤ L, then ceil(q) ≤ L). Indeed for any integer L, ceil(q) > L iff q > L. Because if q > L, then ceil(q) ≥ q > L, and since ceil(q) is integer, it must be at least L+1. Conversely, if ceil(q) > L, then q > L because if q ≤ L then ceil(q) ≤ L. So condition (1) is equivalent to (n+k)/2 > L, i.e., n+k > 2L. Since n+k integer, n+k > 2L. So (1) <=> k > 2L - n.
Since k integer ≥0, condition (1) is k ≥ max(0, 2L - n + 1)? Let's derive: n+k > 2L => k > 2L - n. Since k integer, k ≥ 2L - n + 1 if 2L - n is integer. Because k > (2L - n). So minimal k satisfying (1) is k1 = max(0, 2L - n + 1). But note that this is necessary and sufficient? Check: For k satisfying this, we guarantee (n+k)/2 > L, so ceil > L. But also need to ensure that ceil > L indeed, which holds because if (n+k)/2 > L, then ceil((n+k)/2) ≥ floor((n+k)/2)+? Actually if (n+k)/2 > L, then since L integer, we have (n+k)/2 >= L + epsilon. The smallest possible > L is L + something. But if (n+k)/2 = L + 0.1, ceil = L+1 > L. So fine. If (n+k)/2 = L + 0.5, ceil = L+1 > L. If (n+k)/2 = L + 1, ceil = L+1 > L. So condition (1) is exactly k > 2L - n.
Thus k ≥ floor(2L - n) + 1? Actually 2L - n may be negative. Then k > negative number, so k ≥ 0 satisfies? But if 2L - n is negative, then k > negative always true for k ≥ 0? Not necessarily: k > -3 means k >= -2? Since k is nonnegative integer, k > -3 includes all k ≥ 0. So condition (1) automatically holds for all k ≥ 0 if 2L - n < 0. Indeed if 2L - n < 0, then n > 2L, then (n+k)/2 > L holds for all k≥0? Let's check: n > 2L ensures that even at k=0, n > 2L => n/2 > L, so (n+0)/2 > L, so condition holds. So (1) is automatically satisfied if n > 2L. If n = 2L, then at k=0, n/2 = L, so (n)/2 = L, not > L, so condition fails. So indeed (1) requires k > 0 in that case? Actually n = 2L gives (n)/2 = L, then (n+k)/2 > L requires k > 0. So minimal k1 = max(0, 2L - n + 1). But careful: if 2L - n is negative, then 2L - n + 1 might be ≤ 0. So k1 = max(0, 2L - n + 1) gives 0. But if 2L - n = -1, then 2L - n +1 = 0, so k1 = 0. Check: n = 2L +1? Actually if 2L - n = -1 => n = 2L +1. Then at k=0, (n)/2 = L + 0.5 > L, so condition holds. So k1=0 correct.
Now condition (2): ceil((n+k)/2) ≤ L + E + k.
We can try to simplify similarly. Let p = ceil((n+k)/2). Since p is integer, we can write:
ceil((n+k)/2)