We are given t test cases, each with n positive integers. Operation: choose two indices i != j such that |a_i - a_j| ≤ 1, then remove the smaller of the two. If equal, remove either (so one of them is removed). So each operation reduces the array size by 1. We want to know if it's possible to reduce to a single element (i.e., perform n-1 operations successfully).
We need to output YES or NO for each test case.
Constraints: n ≤ 50, a_i ≤ 100, t ≤ 1000.
We need to figure out condition for possibility.
Observation: The operation effectively allows us to remove an element if there exists another element that is within 1 of it, with the condition that the smaller is removed. But note: you can also remove the larger if the smaller is within 1? The rule: remove the smallest of these two elements. If equal, you can remove any of them (so effectively you remove one of the two). So if |a_i - a_j| ≤ 1, we can remove either the smaller one (if distinct) or if equal either one. However, it says "remove the smallest of these two elements". So if a_i < a_j, we must remove a_i, not a_j. If they are equal, we can choose to remove either. So the operation is not symmetric: you can only remove the smaller when the values are different. But if values are equal, you can remove either.
Thus, the operation is: pick two numbers where the larger minus the smaller ≤ 1. If they differ, we must delete the smaller one. If they are equal, we may delete either.
Goal: reduce to one element.
Interpretation: We can think of this as a process of merging? Not exactly merging, we remove one element each time. Ultimately we want to end up with one element. We can ask: Is there a way to delete all elements except one? Equivalent to: can we delete n-1 elements using allowed operations.
We need to find a condition.
Let's think about what constraints this imposes.
Consider the array sorted. The operation removes a small element when there is a slightly larger (or equal) element nearby. Also, if we have two equal numbers, we can delete either, so we can effectively reduce count of equal numbers arbitrarily as long as there is at least one left.
Observation: If there is any element that is isolated in the sense that no other element is within 1 of it, then that element cannot be removed because to remove it we would need another element that is within 1 difference, and if it's the smaller, we need a larger within 1 to remove it; if it's the larger, we could only be removed if it's the larger and the smaller is within 1? But wait: Could we ever remove a larger element? The operation only allows removal of the smallest of the two. So if we have two numbers a_i < a_j, we can only remove a_i, not a_j. If they are equal, we can remove either. So the only way to remove a larger element is if it is equal to another element (so we can remove either), or if there is an element that is larger than it? Actually if we have numbers a and b with a < b, we can remove a, but we cannot directly remove b. However, b might become the smaller relative to a larger element later? For instance, if we have numbers 1,2,3. We can remove 1 because |1-2| ≤1 (or 1 and 2). After removing 1, we have 2 and 3. Then |2-3| ≤1? 3-2=1 ≤1, so we can remove the smaller, which is 2. So 2 is removed after 1. That's allowed. So even though 2 was the larger relative to 1, it later becomes the smaller relative to 3. So any element except possibly the maximum could be removed as a smaller element relative to some bigger element. But what about the largest element? To remove the largest element, we need some other element that is larger or equal? Actually, to remove an element x, it must be the smaller in a pair (x,y) with y > x and |y-x| ≤ 1, or be equal to another element (so we can remove either). So if x is the absolute maximum in the array, there is no y > x. Therefore the only way to remove it is if there exists another element equal to it (so we can remove either of them, i.e., treat it as equal and remove one). That is, if the maximum value appears at least twice, then we can remove one of those maximums by pairing it with another maximum (equal). If the maximum appears only once, then that element cannot be removed at all, because there is no larger element within 1 to make it the smaller, and no equal element to pair with. However, note that if there is a larger element? But it's the maximum, so no larger element exists. So the unique maximum cannot be removed. Therefore, if the array has a unique maximum, then that element must be the final remaining element. So we must be able to delete all other elements while preserving that unique maximum (or at least ensure that eventually we end with that maximum). Is it possible to delete all other elements without ever needing to remove the unique maximum? Possibly, yes, if we can delete the rest by operations that do not involve removing the maximum, but the maximum might be used as the larger element to remove smaller ones, as long as we don't have to remove it. That's fine because when we pair the maximum (call it M) with some smaller element x where M - x ≤ 1, we can remove x (since x is smaller). So we can use the unique maximum to delete neighbors that are within 1 of it. After we delete some, the array changes. But the unique maximum remains. At the end, if we can delete all other elements, we end with that maximum alone, which is allowed. So the condition is not that the maximum must appear at least twice; rather, a unique maximum is okay as long as we can delete all others. However, is there any scenario where a unique maximum cannot be used to delete all others? Possibly if there are elements that are too far from M, i.e., difference >1, then we cannot pair M with them directly to remove them. But we might use intermediate elements. For example, array: [1, 3] with M=3, difference=2 >1. Can we reduce to one element? n=2. Operation requires |1-3| ≤1, false, so cannot choose any pair. Thus cannot remove any element; stuck with two elements, cannot reduce to one. So answer NO. For n=2, we need |a1-a2| ≤1 to be able to remove one. So unique maximum far away from others prevents removal.
But consider larger arrays: [1,2,4] - sorted: 1,2,4. Is it possible? Let's try. We have 1 and 2 diff=1, so can remove 1 (smaller). Now we have 2 and 4 diff=2 >1, cannot remove either. Stuck with two elements. Could we have removed 2 first? Pair 2 and 4: diff=2 >1, not allowed. Pair 1 and 4: diff=3 >1, not allowed. So only possible move is remove 1 using 2. After that, stuck. So NO. The example shows third test case (1 2 4) is NO. So that matches.
Now consider [1,3,4,4]. Sorted: 1,3,4,4. Let's see. We have pairs: 3 and 4 diff=1 -> can remove 3? Actually 3<4, diff=1 ≤1, so can remove 3 (smaller). Then array: 1,4,4. Now we have 4 and 4 equal, can remove one 4. Then array: 1,4. Diff=3>1, cannot remove. Could we have done different? Try remove 1? Pair 1 with 3 diff=2>1 no; 1 with 4 diff=3>1 no. So only 3 and 4. Remove 3. Then after removing one 4? Actually after removing 3, we have 1,4,4. Could we pair the two 4's to remove one 4, then we have 1 and 4, stuck. Could we pair 1 with 4? No. Could we have removed 4 using 4? That still leaves 1 and 4. So NO. Example says NO.
Now consider [5,5,5,5] -> all equal, obviously YES. [1,2,2] -> sorted:1,2,2. We can remove 1 using one of the 2's. Then we have 2,2, can remove one, leaving one. So YES.
Consider [100] single element -> YES.
Consider [1,1,3]? Let's test: 1,1,3. 1 and 1 equal, remove one -> [1,3]. Diff=2>1 => stuck. Could we do other: 1 and 3 diff=2 not allowed. 1 and 3 not allowed. So only option is remove one 1 using the other 1, then stuck. So NO. But if we had [1,2,3]? 1 and 2 diff=1, remove 1 -> [2,3] diff=1, remove 2 -> [3]. So YES. So it's about connectivity? Essentially we need to be able to delete all but one. Equivalent to: can we reduce the multiset to one element by repeatedly deleting an element that has a "neighbor" within 1, with the constraint that if the neighbor is larger, we can delete the smaller; if neighbor is equal, we can delete either. So deletion of an element is allowed if there exists another element b such that |a - b| ≤ 1 and either a < b or a = b. If a > b, then a is not the smaller, so that pair wouldn't allow deleting a; however, there could be another element c > a? But that would be within 1? Actually if a > b, then a could be the larger, so to delete a we would need an element c such that c > a and |c-a| ≤1, making a the smaller relative to c. Or an element equal to a. So condition for a specific element x to be deletable at a given state: ∃ y ≠ x such that either x = y, or x < y ≤ x+1, or x > y and? Actually if x > y and |x-y| ≤1, then x is larger, so the operation would delete y (the smaller) not x. So that pair does not allow deletion of x. So for x to be deletable, we need either: (i) ∃ y such that y = x (equal), then we can delete either x or y; (ii) ∃ y such that y > x and y - x ≤ 1, i.e., y = x or x+1 (but y = x already covered, so y = x+1). So essentially, an element x can be deleted if there is another element with value either x (so at least two copies) or x+1 (a value exactly one greater). Because if there is an element with value x+1, then pairing x (smaller) with x+1 allows deletion of x. Note: Could there be a scenario where x is the larger? For x to be deletable as the larger, we would need a y such that y = x (equal). Because if y < x and |x-y| ≤1, then x is larger, so the operation deletes y not x. So no. So the only way to delete an element is if there is another element equal to it, or if there is an element that is exactly one greater (so that it is the smaller). Also, can we delete an element that is the largest if there is an element with value exactly one less? That would make it larger, so it would not be deletable via that pair. But if it's also equal to some other element, then it's deletable. So the condition: an element with value v is deletable if count(v) >= 2 or count(v+1) >= 1.
But careful: In a dynamic process, after deletions, counts change. But perhaps we can think about the necessity: eventually we want to end with one element. If we can repeatedly delete elements while always maintaining the condition that the remaining set is "connected" in some sense, we might succeed. But maybe there's a simpler characterization: If the difference between the minimum and maximum is at most 1? Let's test. For [1,2,2], min=1, max=2, diff=1 => YES. For [5,5,5,5], min=5 max=5 diff=0 => YES. For [1,2,4], min=1 max=4 diff=3 >1 => NO. For [1,3,4,4], min=1 max=4 diff=3 >1 => NO. For [100], diff=0 => YES. For [1,1,3], min=1 max=3 diff=2 >1 => NO. For [1,2,3], min=1 max=3 diff=2 >1? Actually diff=2, but we saw YES. That's a counterexample! Wait, [1,2,3] min=1 max=3 diff=2 >1, but we found YES. Let's double-check: array [1,2,3] n=3. Move1: choose 1 and 2 (diff=1) remove 1 => [2,3]. Move2: choose 2 and 3 (diff=1) remove 2 => [3]. So YES. So diff >1 does not necessarily mean NO. So min-max diff condition not sufficient.
What about [1,3,4]? min=1 max=4 diff=3, try: 3 and 4 diff=1, remove 3 -> [1,4] diff=3 >1 stuck => NO. So some arrays with diff>1 work, others don't.
Let's analyze more systematically.
We need to see if there is a sequence of deletions that removes all but one. This is reminiscent of a graph or process where we can "eliminate" elements under certain conditions. Since n is small (≤50), we could simulate all possibilities via DP or BFS over subsets? But t up to 1000, n up to 50, total states 2^50 impossible. But we can find a simpler condition.
Observation: The operation is basically: if two numbers are adjacent or equal, you can delete the smaller (or any if equal). So you can always delete an element that is not the unique maximum, provided there is a "support" from a neighbor with value one greater. But to eventually get down to one element, you need to be able to eliminate all except one.
Idea: Consider the multiset of values. The process is similar to repeatedly removing a minimal element that has a neighbor with value equal or one greater? But not exactly minimal.
Alternatively, think about the possibility of merging the array into one value by repeatedly picking two numbers that are close. Could there be an invariant? For instance, consider the sum? Or parity? Probably not.
We can think in terms of connectivity in the "value graph" where nodes are values and edges between numbers that are within 1. But removal may depend on relative ordering.
Maybe we can characterize by the existence of a value that can "survive" as the last element. The last element must be such that we can delete all others using it or using intermediate values. In the process, we can delete any element x if there is a y with y = x or y = x+1. This suggests that we can only delete elements that are not "isolated" in the sense of not having a "support" from above or from duplicate.
If we think about sorting the array: a1 ≤ a2 ≤ ... ≤ an. We can try to simulate a greedy elimination: always try to delete the smallest element first? In [1,2,3], deleting 1 works. In [1,3,4], deleting 1 is impossible because 1 only has neighbor? Actually 1 and 3 diff=2, 1 and 4 diff=3, no neighbor within 1. So 1 is "isolated". Could we delete 3 first? 3 and 4 diff=1, remove 3 (since 3 smaller). Then we have [1,4], stuck. Could we delete 4? Not initially, because 4 has no equal and no element 5, so cannot delete 4. So only possible move is delete 3. That leads to deadlock. So NO. In [1,2,3], smallest 1 has neighbor 2 (diff=1) so deletable. After removal, 2 has neighbor 3, deletable.
What about [2,3,4]? smallest 2 has neighbor 3, delete 2 => [3,4] delete 3 => [4] YES. So it works.
What about [1,2,4]? smallest 1 has neighbor 2, delete 1 => [2,4] stuck. Could we delete 2 first? 2 and 4 diff=2, cannot. Delete 4? cannot. So only move deletes 1, stuck. So NO.
What about [1,2,2,3]? Sorted: 1,2,2,3. Many possibilities. Try: delete 1 using 2 => [2,2,3]; then delete one 2 using another 2? Actually equal, delete either => [2,3]; then delete 2 using 3 => [3] YES. So YES. Good.
What about [1,1,2,3]? Delete one 1 using the other 1? Actually equal, can delete a 1 => [1,2,3]; then delete 1 using 2 => [2,3]; delete 2 using 3 => [3] YES. Or delete 1 using 2? Possibly. So YES.
What about [1,3,3,4]? Sorted:1,3,3,4. Can we delete 1? No neighbor within 1 (3 diff=2,4 diff=3). Delete 3? 3 and 3 equal, can delete one 3 => [1,3,4]. Now we have [1,3,4] which we saw NO. Could we delete 3 and 4? 3 and 4 diff=1, delete 3 => [1,3,4]? Actually after deletion of 3 (smaller) we get [1,3,4]? Wait starting [1,3,3,4]. If we delete the 3 that is smaller relative to 4, we remove that 3, leaving [1,3,4]. That is NO. If we delete 4? Cannot. If we delete the other 3 using the other 3? That yields same [1,3,4]. So seems NO. But maybe another order: delete 3 and 3 equal -> [1,3,4] NO. So overall NO. Let's test manually: We have 1 and 3,3,4. Perhaps we could delete 4 using something? No. So likely NO.
What about [2,2,3,4]? Sorted:2,2,3,4. Delete 2 using 2? equal -> [2,3,4]; then delete 2 using 3? => [3,4]; delete 3 using 4? => [4] YES. So YES.
Seems that the only problematic cases are when there is a "gap" of more than 1 between some values, but note that [1,2,3] had a gap? 1 to 2 diff=1, 2 to 3 diff=1, but max-min diff=2, there is no gap larger than 1 between consecutive sorted values? Actually sorted consecutive differences: 1 and 2 diff=1, 2 and 3 diff=1. So all consecutive differences are ≤1. Indeed, the condition for YES might be that after sorting, every adjacent pair difference is ≤1. Let's test: For [1,2,4], differences: 1-2 diff=1, 2-4 diff=2 >1 => NO. For [1,3,4]: differences: 1-3 diff=2, 3-4 diff=1 => NO. For [1,2,3]: differences all ≤1 => YES. For [1,2,2,3]: differences: 1-2 diff=1, 2-2 diff=0, 2-3 diff=1 => all ≤1 => YES. For [5,5,5,5]: all differences 0 => YES. For [100]: trivial YES. For [1,1,3]: differences: 1-1 diff=0, 1-3 diff=2 => NO. That matches the test case? Actually the example didn't include [1,1,3], but we predicted NO, and it fits condition (difference >1). For [1,3,3,4]: differences: 1-3 diff=2, 3-3 diff=0, 3-4 diff=1 -> includes a >1 => NO. So far condition holds.
But need to check edge cases: Consider [1,2,4,5]? sorted:1,2,4,5. Differences:1-2=1, 2-4=2, 4-5=1. There is a gap >1. Is it possible? Let's attempt: n=4. Could we reduce? Options: delete 1 using 2 -> [2,4,5]; then we have [2,4,5]. 2 and 4 diff=2, cannot; 4 and 5 diff=1, delete 4 => [2,5]; diff=3 >1 stuck. Or after [2,4,5] could delete 5? cannot. Or initially delete 4 using 5? diff=1 delete 4 => [1,2,5]; now 1 and 2 diff=1, delete 1 => [2,5]; stuck. Or delete 2? cannot. So seems NO. So condition holds.
What about [1,2,3,5]? sorted:1,2,3,5 differences:1-2=1,2-3=1,3-5=2 >1. Try: remove 1 -> [2,3,5]; then remove 2 -> [3,5]; diff=2 stuck. Or remove 3 using? 3 and 5 diff=2 not allowed; so maybe other order: remove 3 using? 3 and 5 diff=2 no; but 3 and 2 diff=1, remove 2? Actually if we remove 2 first? Pair 2 and 3 diff=1, remove 2 => [1,3,5]; then 1 and 3 diff=2, 3 and 5 diff=2, 1 and 5 diff=4; no move. So NO.
Thus far, the condition "all consecutive differences ≤ 1" seems necessary and maybe sufficient? Let's test more complex scenarios.
Consider [1,1,2,2,4]? differences: 1-1=0,1-2=1,2-2=0,2-4=2 >1 => NO. Probably impossible. Try to see if any clever order could work? Could we eventually merge to eliminate the gap? The gap is between 2 and 4. If we could somehow get a 3, but we don't have 3. So we cannot bridge that gap. Because any removal operation does not change the values; we only remove elements, we never change the values of remaining ones. So the set of values present can only shrink; we never introduce new values. Thus if there is a gap greater than 1 between some values, that gap persists. To end with one element, that final element must be some value from the original set. All other values must be removed. For a value to be removed, it needs a neighbor with value either equal or +1. If there is a value v that is isolated because the next higher value is at least v+2, and v is unique? Actually, consider a value v that is the maximum of a "cluster" where the next higher value is at least v+2. Then to remove v, it would need a value v+1 or another v. If there is only one copy of v, and no v+1 exists, then v cannot be removed. So if v is such that count(v)=1 and there is no value v+1 present, then v is undeletable. If there are multiple v's, then they can delete each other? Wait: if count(v) >=2, can we delete one v using another v? Yes, equal values allow removal of either. So you could delete some v's until only one remains. But that last v still has no v+1, so it would be undeletable if it remains. But maybe you could delete the last v by pairing it with something larger? But there is no larger within 1 because next larger is at least v+2. So that last v cannot be deleted. However, if that v is the final element we want to keep, that's okay; we just need to delete all others. But if we have other values that are higher and need to be deleted, they might have their own requirements. So the condition for success is that there exists at least one value that can survive as the last element, and all other values can be eliminated.
If the set of values is such that there is a "connected component" where all values are within 1 of each other (i.e., for any two values in the component, the absolute difference is at most 1? Actually "within 1" is not transitive? If a and b differ by 1, b and c differ by 1, then a and c differ by 2, so not necessarily within 1. But for removal, we only need adjacency between numbers that are close. However, the ability to delete all numbers in a component might require that the component is contiguous in the integer line without gaps >1. Let's formalize.
Define the multiset of values. Since we never change values, the only possible final value is some integer x that originally appears. For the process to succeed, we need to be able to delete all other numbers. For any other number y ≠ x, we must eventually delete it. To delete y, at the moment of deletion, there must exist some z still present such that either z = y or z = y+1 (if y < z) with z - y ≤ 1. Note that if z = y, then we need at least two copies of y at that moment. If we plan to delete all copies of y except maybe one that might be kept? But ultimately we want to delete all y (since final element is x). So we must ensure that for each value y that is not the final x, we can arrange deletions to eliminate all instances.
This looks like we can treat the values as items on the number line. The operation allows us to delete an item if there is another item at the same point or exactly one point to the right. This is reminiscent of a process where we can "shift" deletions to the right? Actually, the ability to delete an item at position v depends on the existence of an item at v or v+1. So if we think from smallest to largest, we might try to delete all items except maybe the largest in a connected chain.
Observation: If the values are such that the maximum value M appears, and for every value v < M, there exists some element at v+1 (or v itself if multiple). Then we could potentially delete all smaller values by repeatedly pairing them with the next higher value. But careful: To delete all copies of a value v, we need enough "capacity" from v+1 to assist in deletions. Since deleting a v requires a v+1 (or another v). But if we delete v's using v+1's, the v+1's remain (unless we also delete some of them later). So v+1's can be used many times to delete multiple v's? Yes, because after deleting a v (the smaller), the v+1 remains unchanged and can be used again to delete another v. So one v+1 can delete arbitrarily many v's, as long as they are present. That's important. Similarly, multiple v's can delete each other if they are equal. So for a set of values, we can think about "can we eliminate all values except possibly one final value?" This resembles a kind of "stacking" where we start from the top? Let's think from the largest downward.
Suppose we decide the final element will be the maximum M. Then we need to delete all other values. For any value v < M, we can potentially delete it using a v+1 (if present) or using duplicate v's. Since M is present, we can delete values that are M-1 using M. Then those M-1's are gone, but M remains. Then we can delete M-2 using M-1? But after we delete all M-1's, there is no M-1 left to help delete M-2. However, we could leave at least one M-1 to help delete M-2 before deleting that last M-1. But we eventually need to delete all M-1's as well because final is M. So we need a strategy that deletes all lower values while eventually also deleting the intermediate ones.
Consider a chain of consecutive integers: e.g., values 1,2,3,...,M. Can we delete all except M? Let's test with M=3, values 1,2,3. Yes: delete 1 using 2 (or using 2? Actually 1 can be deleted by 2; 2 can be deleted by 3; after deleting 1, we have 2 and 3; delete 2 using 3; left 3). That worked. For M=4, values 1,2,3,4: delete 1 using 2 -> [2,3,4]; delete 2 using 3 -> [3,4]; delete 3 using 4 -> [4]. Works. More generally, if we have a contiguous set of integers from some L to M (with no gaps), and at least one of each? Actually we may have multiple copies. But what if we have only some of them? Suppose we have values 1 and 3, and we want final 3. To delete 1, we need a 2 or another 1. There's no 2 and only one 1, so 1 can't be deleted. So fails. So we need that for every value except possibly the final, there is a path of increments of 1 to connect to the final? More precisely, for any value v that is less than final x, we must be able to delete it eventually. The condition for deleting v is that at the time of deletion, there is some element at v+1 (or duplicate v). Since we can orchestrate order, we could use intermediate values that may later be deleted themselves.
A natural greedy algorithm: sort values ascending. Try to delete from smallest upwards, always using the next higher value if available. If at some point, the smallest remaining value has no duplicate and no value exactly one greater, then it's impossible (since it cannot be deleted). Conversely, if we can always delete the current smallest value (by pairing with either a duplicate or the next higher), and continue until only one remains, then YES. This greedy might work because deleting the smallest first seems safe: if we can delete it now, doing so won't hinder future deletions? But careful: Deleting a small element might remove a duplicate that could be used to delete another small element? Actually if we have two 1's and one 2, deleting one 1 using the other 1 (equal) is fine; or using 2 also fine. If we delete a 1 using a 2, the 2 remains. That seems safe. If we instead keep the 1 for later, could it cause trouble? Probably not; but is there a scenario where deleting the smallest now might preclude later deletions that would have been possible if we kept it? Suppose we have values: 1,2,2. Smallest is 1, we can delete it using a 2. After deletion, we have 2,2; we can delete one 2 leaving one 2. That's fine. If we had not deleted 1 first, we could also delete one 2 using the other 2, then have 1,2, then delete 1 using 2, leaving 2. Also works. So either order works.
But consider a case where the smallest value cannot be deleted because it has no duplicate and no 2? Actually if it can't be deleted, we are stuck. So for success, every value except possibly the final one must at some point have a "deleter". A greedy that always picks a deletable element and deletes it should succeed if and only if it's possible.
However, there might be tricky cases where the smallest is deletable, but deleting it now might cause later a situation where some other value becomes undeletable. Let's search for a counterexample.
Suppose we have values: 1,1,2,3. Sorted. Greedy: delete smallest 1? Options: can delete a 1 using another 1 (duplicate) or using 2. If we delete a 1 using 2, we get [1,2,3]. Then delete 1 using 2 -> [2,3]; delete 2 using 3 -> [3]. Works. If we delete 1 using duplicate, get [1,2,3] same. So works.
Consider: 1,2,3,3. Delete 1 using 2 -> [2,3,3]; delete 2 using 3 -> [3,3]; delete one 3 -> [3]. Works.
Consider: 2,2,3,4. Delete smallest 2 using duplicate? or using 3? if we delete one 2 using duplicate, get [2,3,4]; then delete 2 using 3 -> [3,4]; delete 3 using 4 -> [4]. Works.
Consider: 2,3,4,4. Delete smallest 2 using 3 -> [3,4,4]; then delete 3 using 4 -> [4,4]; delete one 4 -> [4]. Works.
Consider: 1,3,4,4,5? Wait, but there is a gap 1-3=2. Already smallest 1 cannot be deleted (no 2, only one 1). So impossible regardless.
What about case with multiple gaps but possible due to duplicates bridging? E.g., values: 1,2,2,4,4? Sorted:1,2,2,4,4. Smallest 1 can be deleted using 2. After deletion: [2,2,4,4]. Now smallest 2: can delete using duplicate 2 or using? 3? no 3, but duplicate works. Delete one 2 using other 2 -> [2,4,4]; now we have 2,4,4. Smallest 2: can it be deleted? It needs a duplicate 2 or a 3. No duplicate (only one 2) and no 3. So it becomes undeletable. But could we have chosen a different deletion order to avoid this? Originally after first deletion (removing 1), we had [2,2,4,4]. Instead of deleting a 2 via duplicate, perhaps we could delete a 4? But 4 can be deleted using duplicate 4? Yes, we could delete a 4 using the other 4. Let's try: start [1,2,2,4,4]. Delete 1 using 2 -> [2,2,4,4]. Now we have two 2's and two 4's. Instead of deleting a 2, delete one 4 using the other 4 -> [2,2,4]. Now we have [2,2,4]. Now we can delete a 2 using duplicate? Actually we have two 2's, can delete one 2 using the other 2 -> [2,4]. Now we have [2,4] with diff=2 >1, stuck. So still stuck. Could we have deleted a 2 using 4? 2 and 4 diff=2, not allowed. So after that, we are stuck. Maybe a different initial move? Could we delete a 2 first? Starting [1,2,2,4,4]. Could we delete a 2? 2 has duplicate, so we can delete one 2 using the other 2 -> [1,2,4,4]. Now we have [1,2,4,4]. Smallest 1 can be deleted using 2 -> [2,4,4]; then we have [2,4,4]. Now 2 cannot be deleted (no 3, only one 2). Could we instead after [1,2,4,4] delete a 4 using duplicate? [1,2,4]; then 1 and 2 diff=1, delete 1 -> [2,4] stuck. Or delete 2? can't. So no.
What about deleting a 4 using duplicate first? Starting [1,2,2,4,4], delete one 4 using the other 4 -> [1,2,2,4]. Then we have [1,2,2,4]. Now we can delete 1 using 2 -> [2,2,4]; then delete a 2 using duplicate -> [2,4]; stuck. Could we instead after [1,2,2,4] delete a 2 using duplicate? [1,2,4]; then 1 and 2 -> [2,4] stuck. So seems impossible. So [1,2,2,4,4] appears NO. Check condition: sorted differences: 1-2=1, 2-2=0, 2-4=2 >1, 4-4=0 => there is a gap >1, so condition says NO. Good.
Now consider a more tricky case: [1,2,3,5,6]? differences:1-2=1,2-3=1,3-5=2,5-6=1 => gap at 3-5 >1, likely NO. Try to see if any order can succeed: We have values 1,2,3,5,6. Want to end with maybe 6. Could we delete 5 using 6? Yes (5 and 6 diff=1, remove 5). Then we have [1,2,3,6]. Now we have 1,2,3,6. 1-2-3 chain, but 3 and 6 diff=3, so after we delete 1 and 2? Let's try: delete 1 using 2 -> [2,3,6]; delete 2 using 3 -> [3,6]; stuck because 3 and 6 diff=3. Could we delete 3 before? Initially after deleting 5, we could try to delete 3 using something? 3 and 6 diff=3 not allowed; 3 and 2 diff=1, but if we delete 3 using 2? Actually 2 is smaller than 3? Wait 2 and 3 diff=1: if we pair 2 and 3, we must remove the smaller, which is 2, not 3. So cannot delete 3 via 2. Could we delete 3 using something else? There's no 4. So 3 is stuck if we want to keep 6. Could we end with 3? Then need to delete 1,2,5,6. Delete 5 using 6 -> [1,2,3,6? Actually after delete 5 using 6, we have 1,2,3,6. To delete 6, need 7 or duplicate 6; none. So 6 cannot be deleted if final is 3. Could we end with 5? Then need to delete others. Delete 1,2,3,6. 6 can be deleted? Need 7 or duplicate, none. So fails. End with 2? then need delete 1,3,5,6. 3 cannot be deleted by 2? Actually 2 and 3 diff=1, but then we would remove 2 (smaller) not 3. So to delete 3, need 4 or duplicate 3; none. So fails. So indeed NO.
Now consider case where there is a gap but we have duplicates that might allow bridging? For example, [1,2,2,3] works (no gap >1). [1,2,3,3] works. So condition still holds.
But is the condition "max difference between any two consecutive distinct values after sorting is ≤ 1" both necessary and sufficient? Let's test a more complex scenario: Suppose values are [1,1,3,3]. Differences: 1-1=0, 1-3=2 >1, 3-3=0. So gap >1. Is it possible? Try to reduce to one element. n=4. Can we delete any? 1 and 1: equal, can delete one 1 -> [1,3,3]. Now [1,3,3]. 1 cannot be deleted (no 2, only one 1). 3 and 3 equal, can delete one 3 -> [1,3]; stuck. What about other order: delete 3 and 3 first -> [1,1,3]; then delete one 1 using other 1 -> [1,3]; stuck. Or delete 1 and 3? diff=2 not allowed. So NO. So condition holds.
Now consider [1,2,4,5,5]? differences:1-2=1,2-4=2,4-5=1,5-5=0. gap >1 -> likely NO. Try: maybe end with 5? Delete 1 using 2 -> [2,4,5,5]; delete 2? need 3 or duplicate; only one 2, so cannot. Delete 4 using 5? remove 4 -> [2,5,5]; then 2 cannot be deleted. Alternate: delete 4 using 5 first -> [1,2,5,5]; then delete 1 using 2 -> [2,5,5]; then 2 cannot be deleted. Delete 5 using duplicate? That leaves [1,2,4,5] etc. So NO.
Now consider [2,3,5,5,6]? differences:2-3=1,3-5=2,5-5=0,5-6=1 -> gap >1. Probably NO.
What about [1,2,2,3,4]? differences all ≤1? Sorted:1,2,2,3,4 => diffs:1-2=1,2-2=0,2-3=1,3-4=1 => all ≤1. Should be YES. Let's test: delete 1 using 2 -> [2,2,3,4]; delete 2 using duplicate? or using 3? delete one 2 using other 2 -> [2,3,4]; delete 2 using 3 -> [3,4]; delete 3 using 4 -> [4]. Works. YES.
Thus, condition seems plausible: The array can be reduced to one element iff after sorting, the maximum gap between consecutive elements (in sorted order) is at most 1. But wait, we must also consider the case where the maximum appears only once, but the condition of gaps ≤1 ensures that the maximum-1 is present (since the second largest value is either equal to max or max-1). Actually if all consecutive differences ≤1, then the sorted list is such that the values are all in an interval of length at most? Actually if we have values like [1,3] the diff is 2 >1, so fails. For [1,2,3] diff between 1 and 2 is 1, 2 and 3 is 1, so passes. For [1,2,4], diff between 2 and 4 is 2 >1 fails. So condition is that there is no gap of at least 2 between any two consecutive values in the sorted distinct list? More precisely, for sorted array a1 ≤ a2 ≤ ... ≤ an, we require that for all i from 1 to n-1, a_{i+1} - a_i ≤ 1. That is, adjacent elements (including duplicates) differ by at most 1. Since duplicates have difference 0, that's fine.
Is this condition sufficient? Let's attempt to prove or find counterexample.
Assume we have sorted array where each consecutive difference ≤ 1. Then the values are all in a contiguous integer range [min, max] with no gaps. However, note that there might be missing values within that range? If consecutive difference is ≤1, it could be 0 or 1. If it's 1, then the numbers are adjacent integers, but could we have missing a value? Example: values 1,3: diff=2 >1, so fails. So if all differences ≤1, then the sorted list covers every integer between min and max? Not necessarily; consider values 1,1,2,2,4? That would have 2 then 4 difference=2, so fails. So indeed, if the sorted list (with duplicates) has differences only 0 or 1, then the set of distinct values is a set of consecutive integers. Because if we have a gap, then somewhere we would have a_i and a_j where a_j - a_i ≥ 2 and there is no value between, but because the list is sorted, there will be some consecutive elements with difference ≥2. So condition is equivalent to: the distinct values form a set of consecutive integers (i.e., if min = m, max = M, then for every integer v in [m, M], there is at least one element with that value). Actually, we also need that there are no gaps; but it's enough that for each integer from min to max-1, there is at least one occurrence. This is equivalent to: the sorted list's adjacent differences are ≤1. Yes.
Now, under this condition, can we always reduce to one element? Let's try to think of a constructive strategy. Since values are consecutive integers from L to R inclusive. We have some multiplicities. We need to end with one element. Is it always possible? Consider a case: [1,1,1,1] (all same) - trivially yes. [1,2] with L=1,R=2: we can remove 1 using 2 -> [2] yes. [1,2,2] works. [1,2,3] works. [1,1,2] works? Let's test: 1,1,2. Options: delete one 1 using the other 1 -> [1,2]; then delete 1 using 2 -> [2] yes. Or delete 1 using 2 directly -> [1,2] then same. So yes. [1,2,2,2]? works. [2,3,4] works. [1,3] is not allowed because gap.
But consider a case where the max appears only once, and the min appears many times? Example: [1,1,1,2]? Sorted:1,1,1,2. Differences:1-1=0,1-1=0,1-2=1. All ≤1. Should be YES. Let's test: We have three 1's and one 2. We need to end with either 2 or maybe 1? If we end with 2, we must delete all 1's. Can we delete all 1's? Use 2 to delete one 1 (since 1 and 2 diff=1, remove 1). After that, we have two 1's and one 2? Actually start [1,1,1,2]. Delete a 1 using 2: remove one 1 -> [1,1,2]. Now we have two 1's and 2. Next, we can delete a 1 using the other 1? Yes, equal, remove one 1 -> [1,2]. Then delete 1 using 2 -> [2]. Works. So yes.
What if we have [1,2,2,2]? ending with 1? Could we end with 1? We would need to delete all 2's. To delete a 2, we need a 3 or duplicate 2. There is no 3, but we have duplicate 2's. So we can delete two of the 2's using each other (equal) leaving one 2. But then we have [1,2]. Now to delete the last 2, we need a 3 or duplicate, none. Could we delete 1 instead? But final is 1, we want to keep 1, so we need to delete the last 2. But 2 cannot be deleted because it has no 3 and no other 2. So ending with 1 fails. But we could instead aim to end with 2. Since condition says we can obtain array consisting of only one element, not necessarily a particular one. So we just need existence of some final element. In this case, we can end with 2 (as shown). So overall YES. So condition not requiring that any specific element can be last; just that there exists some sequence leading to one element.
Now, consider a more subtle scenario: Suppose we have values that form a consecutive set, but maybe the multiplicities are such that the only possible final element is a certain value, but that might be impossible to achieve because we might be forced to delete that value? But we can choose which element to keep by controlling deletions. So as long as we can keep at least one element and delete all others, we are fine.
Is it always possible when the values are consecutive? Let's attempt to prove by induction on n.
Given multiset of positive integers with the property that the set of distinct values is a contiguous integer interval [L, R] (i.e., for every integer v from L to R, there is at least one occurrence). Claim: We can reduce to a single element.
Proof idea: We can always delete the smallest element. If the smallest value L appears at least twice, we can delete one L using another L (equal) — that's fine. If L appears only once, then we must delete it using L+1 (since L+1 exists by contiguity). So we can delete that unique L by pairing with an element of value L+1. After deletion, the new multiset still has the property that its distinct values form a contiguous interval? Possibly, if L was unique and we delete it, we might remove L entirely, so the new min becomes L+1. However, we need to ensure that L+1 still exists after using it? In the operation, when we delete L using L+1, we remove L, but L+1 remains. So L+1 is still present. So the new set's distinct values are from L+1 to R (still contiguous). Also, note that if L+1 had only one copy and we used it to delete L? Actually we didn't delete L+1; we only used it as the larger, and it remains. So count of L+1 unchanged. So the new multiset still satisfies that all integers from new min to R are present at least once? Need to check: After removing the only L, the integer L may no longer be present, but that's okay because min is now L+1, and we know L+1 exists. But what about L+2? Might be missing? But originally, we had all integers from L to R present at least once. After removing L, the set of distinct values becomes {L+1, L+2, ..., R} which is still contiguous. So property holds for the new min. So we can repeatedly delete the smallest element. Eventually, we will have only one element (the maximum maybe). But careful: When we delete the smallest, we must ensure that at that moment, the condition for deletion holds: either duplicate of smallest exists, or an element with value smallest+1 exists. By the contiguous property, smallest+1 always exists (since smallest < R). However, if smallest appears only once and we try to delete it using smallest+1, that's allowed. So the step is always possible as long as there is more than one element. But there is a catch: What if the smallest value is L and L+1 exists, but we might have used the only copy of L+1 to delete something else earlier? But we are always deleting the smallest first, so L+1 is never deleted before L because L is smaller, and when we delete L, we don't delete L+1. So L+1 remains. So by always deleting the current smallest element, we ensure that for each step, the smallest value can be deleted because either it has a duplicate or the next integer exists (since we haven't deleted that next integer yet because it's larger). This is a typical greedy reduction.
But is it always true that the next integer exists? At the moment we are about to delete the smallest, say current min = m. We have at least two elements overall. Since the original set was contiguous, after some deletions, could it happen that some integer between m and R is missing? Possibly if we had only one copy of some intermediate value and we deleted it earlier? But in our greedy, we only delete the smallest each time. That means we never delete any element larger than the current minimum until it becomes the minimum. So we never delete a value v while there exists a smaller value w < v still present. Because we always delete the smallest first. Therefore, when the minimum is m, all values larger than m have not been deleted yet (since we only deleted smaller ones). Thus, all original copies of values > m are still present. Since originally, for each integer from L to R there was at least one copy, in particular for m+1, there was at least one copy originally, and it hasn't been deleted yet (because we only deleted values ≤ m). Therefore, m+1 is present. So the condition holds. Also, if there are multiple copies of m, we could use duplicate; but even if only one copy, we can use m+1.
Thus, by repeatedly deleting the current smallest element (choosing to delete it either via duplicate if exists or via m+1), we can reduce the array to one element. The process will end when only one element remains, which will be the original maximum R (or maybe some other if we deviate, but greedy works).
Therefore, the condition that the sorted array has no gaps >1 is sufficient.
Now, we must also check necessity: If there is a gap >1, is it impossible? Suppose sorted array has a gap: there exist i such that a_{i+1} - a_i ≥ 2. Let L = a_i, R = a_{i+1} with R - L ≥ 2. Then the values are split into two groups: those ≤ L and those ≥ R, with no values in (L, R). Note that L < R and at least one integer between them (L+1) is missing. We claim that it's impossible to reduce to one element.
Why? Consider any process. At the start, we have some elements with value ≤ L and some with value ≥ R. The operation only allows removal when the two chosen elements differ by at most 1. Therefore, an element from the lower group (value ≤ L) can only interact with elements also in the lower group or possibly with an element of value L+1? But L+1 does not exist. Since the smallest value in the upper group is R, which is at least L+2, the difference between any element from lower group (max L) and any from upper group (min R) is at least 2. So they cannot be paired together. Thus, the two groups are "disconnected": no operation can involve one element from the lower group and one from the upper group because their absolute difference >1. Therefore, the process on the whole array is essentially two independent processes on each group, as they never interact. Since we must end with one element overall, one group must eventually become empty (all its elements removed) and the other group must end with exactly one element. But can we empty a group completely? For a group to be completely removed, we need to be able to delete all its elements. However, note that for the lower group, its maximum value is L, and for the upper group, its minimum value is R. Within each group, the values may have their own gaps or not, but we need to check if it's possible to delete all elements of a group. In particular, consider the lower group: its elements are all ≤ L, and there is no value L+1 available in the whole array. So for any element with value L (the maximum in lower group), to delete it, it would need either a duplicate L or an element with value L+1. Since L+1 is absent, the only way to delete an L is if there are at least two copies of L. If there is exactly one copy of L, that L cannot be deleted. Similarly, for the upper group, its minimum value is R. To delete an element with value R, it would need either a duplicate R or an element with value R+1? Actually to delete R, as the smaller, it needs R+1; as equal, it needs duplicate. But also could R be deleted if it is the larger? If there is an element with value R-1? But R-1 is ≤ L (since the gap ensures R-1 ≤ L? Actually R - L ≥ 2, so R-1 ≥ L+1? Wait if R - L ≥ 2, then R-1 ≥ L+1, but L+1 is not present. However, could there be an element with value R-1? That would be in (L, R), but by definition there is no element with value between L and R. So R-1 is absent as well (since if R-1 > L, it's in the gap). So R-1 is missing. Therefore, to delete an element with value R, it would need either duplicate R or R+1. But R+1 may or may not exist; if it doesn't, and if there is only one copy of R, then R cannot be deleted.
Thus, if either group has a unique maximum (with count 1) and no next integer, that element cannot be deleted. But we could aim to have that group be the one that survives (i.e., its single element remains). However, we need to have exactly one element total. So one group must be completely eliminated, and the other must reduce to exactly one. But if the group to be eliminated has an element that is undeletable (i.e., unique max with no next integer), then elimination is impossible. Similarly, if the group that is to survive has more than one element, we need to reduce it to one; but maybe that group itself can be reduced if it satisfies the contiguous condition internally.
Thus, necessity might require more careful argument: If there is a gap, is it always impossible? Let's test a borderline case: Suppose lower group is [1,1] (two 1's) and upper group is [3,3] (two 3's). Gap: 1 to 3 diff=2. Can we reduce to one element? Let's try. We have [1,1,3,3]. Lower group: two 1's. Upper group: two 3's. They cannot interact. Can we reduce lower group to zero? To eliminate lower group completely, we need to delete both 1's. 1's can be deleted if there is a duplicate or a 2. Since there is duplicate, we can delete one 1 using the other 1 -> [1,3,3]. Now we have one 1 left, and two 3's. To delete that last 1, we need a duplicate 1 (none) or a 2 (none). So the last 1 cannot be deleted. Could we instead first delete one 3? That doesn't help because 1 and 3 still cannot interact. If we delete one 3 using the other 3 -> [1,1,3]. Then we have two 1's and one 3. Then we could delete a 1 using duplicate -> [1,3]. Then stuck with 1 and 3. If we instead after [1,1,3,3] delete a 1 using duplicate? Already did. So it seems impossible to get rid of both groups entirely; we always end with at least one element from each group, and they can't merge. The only way to have a single element is if one group reduces to exactly one and the other reduces to zero. But as argued, to reduce a group to zero, we need to be able to delete all its elements. For the lower group of 1's, the last 1 cannot be deleted because no 2. So lower group cannot become empty. Similarly, upper group of 3's: last 3 cannot be deleted because no 4 or duplicate? Actually if we have two 3's, we can delete one, leaving one 3; that last 3 cannot be deleted because no 4 or duplicate. So upper group also cannot become empty. So both groups leave at least one element. Thus overall we cannot reach a single element.
What if lower group can be completely eliminated because it has more than one copy of its maximum, allowing deletion of all? Example: Lower group: [1,2]? But 1 and 2 difference=1, that's contiguous, but there is still a gap to upper group? Let's construct: values: [1,2,4,4]. Here distinct values: 1,2,4. Gap between 2 and 4 is 2 (since 3 missing). Lower group: values ≤2: 1,2. Upper group: values ≥4: 4,4. Can we eliminate lower group entirely? Lower group: 1 and 2. They are contiguous. Can we delete both? Let's try: We can delete 1 using 2 -> [2,4,4]. Now lower group has only 2. To delete 2, we need a 3 or duplicate 2. No duplicate 2 and no 3. So 2 cannot be deleted. So lower group cannot become empty. What if lower group had two 2's? Example: [2,2,4,4]. Lower group: two 2's. Upper: two 4's. Can we eliminate lower group? Delete one 2 using the other 2 -> [2,4,4]. Then we have one 2 left, cannot delete. So fails. What about lower group having 2 and 3? But then gap would be between 3 and 4? Actually if we have 3, then the set would include 3, so gap would be less than 2; but we assume a gap exists, so there is at least one missing integer. Suppose the gap is between 2 and 4, but lower group includes 1,2,3? Then 3 is present, so gap is not there. So gap means there is an integer k such that no element has that value, but there are elements both below and above k. For the lower group, its maximum is some L, and the next integer L+1 is missing. So the lower group's maximum is L, and there is no L+1 in the whole array. Therefore, any element with value L, if it's the only one, cannot be deleted. If there are multiple L's, you can delete all but one L using duplicates, but the last L still cannot be deleted because you need L+1 or duplicate. So the last L is undeletable. Thus, the lower group cannot become empty; it must retain at least one element of value L. Similarly, the upper group's minimum is some R, and there is no R-1 (since missing integer includes R-1? Actually if gap is between L and R, with R - L ≥ 2, then L+1,..., R-1 are missing. So R-1 is missing. For the upper group, its minimum is R. To delete an element of value R, you need either duplicate R or R+1. R-1 is not helpful because if you pair R with R-1, R is larger, so you would delete R-1, not R. So to delete R, you need R+1 or duplicate. R+1 may or may not exist; if it doesn't, then the last R is undeletable. But perhaps the upper group could be eliminated if it contains some value greater than R? For example, suppose array: [1,3,4,5]. Distinct: 1,3,4,5. Gap between 1 and 3 (2 missing). Lower group: values ≤1: just 1. Upper group: values ≥3: 3,4,5. Upper group is contiguous from 3 to 5. Can we eliminate lower group? 1 cannot be deleted (no 2). So we must keep 1 as the final element and eliminate upper group. Can we eliminate upper group completely? Upper group's values are 3,4,5. The maximum is 5. To delete 5, need 6 or duplicate 5. No 6, only one 5? Actually we have 5 appears once? In this example 5 appears once. So 5 cannot be deleted. Could we delete 5 by pairing with 4? That would delete 4 (since 4 is smaller). Not helpful. So 5 is undeletable. Could we end with 1? Then we need to delete all upper elements. But 5 is undeletable. So impossible. Could we end with something else? Could we eliminate lower group and end with an element from upper group? As argued, lower group cannot become empty because its only element 1 is undeletable. So impossible. So overall NO.
But consider case where upper group has duplicate of its maximum so that last maximum can be deleted? Wait, to delete the maximum of the whole array, we need either duplicate or value M+1. If M is the global maximum and it appears at least twice, we can delete one M using another M (equal), but the last M still remains because to delete the last M, we would need another M (none) or M+1 (none if M is global max and no larger). So the last M is always undeletable. That's okay if we intend to keep that M as final element. But if we need to eliminate the group that contains the global maximum, we would need to delete all its elements, including the last M. That is impossible. So in a split scenario, one group will contain the global maximum, the other the global minimum. If we try to eliminate the group containing the global maximum, we cannot delete the last occurrence of the maximum (unless there is a larger value, but that would be in the same group if exists). So the group with the global maximum cannot be completely eliminated unless there is a larger value to delete that max, but that would be in the same group, and then the new maximum would still be present. Ultimately, the only way to eliminate a group entirely is if the group does not contain the global maximum? Actually, if there are two groups separated by a gap, each group has its own maximum. The group with the larger values contains the global maximum. That group cannot be eliminated because its maximum cannot be deleted (since no larger value exists). However, the other group (the lower group) might be eliminated if its maximum can be deleted? But as argued, its maximum L has no L+1 (since gap), so L cannot be deleted either. So neither group can be eliminated entirely. Unless one group has only one element that is equal to the other group's element? But they are separated by >1, so can't.
But what if one group is such that its maximum is not unique? For instance, lower group: values: [2,2] (L=2), upper group: [4,5,5]? Gap between 2 and 4 (3 missing). Can we eliminate lower group? Lower group has two 2's. We can delete one 2 using duplicate -> [2,4,5,5]. Now one 2 left. To delete that last 2, we need 3 or duplicate. No 3, no duplicate. So 2 remains. So lower group cannot be eliminated. Upper group: its maximum is 5, appears twice. We could delete one 5 using duplicate -> [2,4,5]. But last 5 cannot be deleted (no 6). So upper group cannot be eliminated either. So impossible.
Thus, when there is any gap ≥2, it's impossible to reduce to one element. But is there a scenario where the gap is exactly 2, but one group can be completely eliminated because its maximum has a larger neighbor within the same group? Wait, if the group is not "contiguous"? But we are considering a gap between groups; within each group, there might be internal gaps as well. But even if a group internally has gaps, it might still be impossible to reduce that group to one element? But the necessity argument: If there is a gap somewhere, we partition the sorted list into two non-empty sets A = values ≤ L, B = values ≥ R, with L < R-1 (so no values in between). We claim that in any sequence of operations, we cannot reduce to a single element. Reason: Since no operation can involve one element from A and one from B (difference >1), the two sets are independent. The only way to reduce to one element is to completely eliminate one set and reduce the other to one element. However, consider the set A. Its maximum value is L. To eliminate A entirely, we need to delete all elements of A. In particular, the last remaining element of A (when only one left) would have value v ≤ L. To delete that last element, it would need either a duplicate (impossible, as only one left) or a value v+1. But v+1 ≤ L+1. Since B's smallest is R ≥ L+2, v+1 is either L+1 (which is missing because gap) or if v < L, maybe v+1 ≤ L could be present? But if v is not the maximum of A, there might be a larger value within A? However, when A is down to one element, there are no other elements in A. Could it be that v is not the maximum of A but all larger elements in A have already been deleted. So v could be, say, some value less than L. At that point, to delete v, we need v+1. Since all elements of A with value > v are gone, the only possible source for v+1 is from B. But v+1 could be ≤ L? If v ≤ L-1, then v+1 ≤ L. Since L is missing from B? Actually B contains values ≥ R, which are ≥ L+2. So v+1, being at most L, is not in B. And it's not in A because only v remains. So v+1 is absent. Therefore, that last element of A cannot be deleted. So A cannot be eliminated completely. Similarly, B cannot be eliminated completely because its minimum value is R, and to delete the last element of B (value w ≥ R), you would need w+1 or duplicate. If w = R and there is no larger value in B (since B may have values > R, but they might have been deleted earlier; if w is the minimum and the only left, then w+1 is missing (unless present in A? But A's values are ≤ L < R-1, so w+1 ≥ R+1, not in A). So B cannot be eliminated completely. Thus, it's impossible to reduce to a single element. This argument seems solid.
But careful: What if one of the groups is already empty? That would be the trivial case where there is no gap? Actually if there is a gap, both groups are non-empty by definition of gap: there exist at least one element below the gap and at least one above. So both groups are non-empty.
Thus, the condition is both necessary and sufficient.
Therefore, the problem reduces to: For each test case, check if after sorting the array, the maximum difference between consecutive elements is ≤ 1. Equivalently, check if max - min <= something? No, that's not enough because [1,2,3] max-min=2 but still YES. The condition is that there is no gap of at least 2 between successive elements in sorted order. That is equivalent to: if we let min = smallest, max = largest, then for every integer v from min to max, there is at least one occurrence. Since values are integers, we can check by counting frequencies or by sorting.
Given constraints n ≤ 50 and a_i ≤ 100, we can easily sort and check adjacent differences.
Implementation: For each test case, read n, read list a. Sort a. Then loop i from 0 to n-2, if a[i+1] - a[i] > 1, then print "NO"; else after loop print "YES".
But need to ensure that we consider duplicate differences 0, which is fine.
Test with given examples:
-
[1,2,2] sorted -> [1,2,2] differences: 1 (2-1) =1, 0 (2-2)=0 -> all ≤1 => YES.
-
[5,5,5,5] -> differences all 0 => YES.
-
[1,2,4] -> differences: 1,2 -> 2>1 => NO.
-
[1,3,4,4] sorted [1,3,4,4] differences: 2,1,0 -> 2>1 => NO.
-
[100] -> no differences, vacuously true => YES.
Matches.
Also test [1,2,3] -> YES. [1,1,3] -> NO. Good.
Edge Cases: n=1 always YES.
Thus solution is straightforward.
Now we must confirm there is no hidden nuance. Let's double-check the operation rule: "remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one)." This matches our reasoning.
Now consider a potential counterexample where sorted differences ≤1 but still impossible due to some parity or something? Let's attempt to manually try a tricky one: [1,1,2,2]? Should be YES. Strategy: delete one 1 using other 1 -> [1,2,2]; delete 1 using 2 -> [2,2]; delete one 2 -> [2]. Works. [1,2,3,3]? Works. [2,3,3,4]? Works. [1,1,1,2,2]? Works.
What about [1,2,3,4,4]? Works: delete 1 using 2 -> [2,3,4,4]; delete 2 using 3 -> [3,4,4]; delete 3 using 4 -> [4,4]; delete one 4 -> [4].
Now consider a case where the values are consecutive but the multiplicities are such that the greedy might get stuck if we delete the smallest using a duplicate when we should have used the next integer? But we argued greedy works. Let's test a more complex case: [1,1,2,2,3]? Greedy: delete smallest 1. Options: using duplicate 1 (remove one 1) -> [1,2,2,3]; then delete smallest 1 using 2 -> [2,2,3]; then delete a 2 using duplicate -> [2,3]; then delete 2 using 3 -> [3]. Works. If we instead delete 1 using 2 initially: [1,2,2,3] same. So fine.
Now consider [1,2,2,3,3]? Greedy: delete 1 using 2 -> [2,2,3,3]; delete smallest 2 using duplicate -> [2,3,3]; delete 2 using 3? Actually 2 is smallest, can delete using 3 -> [3,3]; delete one 3 -> [3]. Works.
What about [2,2,3,3,4]? delete 2 using duplicate -> [2,3,3,4]; delete 2 using 3 -> [3,3,4]; delete 3 using duplicate -> [3,4]; delete 3 using 4 -> [4]. Works.
Thus seems robust.
Now consider if the sorted array has all differences ≤1 but the min appears only once and the max appears only once, but there are intermediate values. That's fine.
Thus solution is correct.
We can implement.
Time complexity: O(t * n log n) with n ≤ 50, t ≤ 1000, trivial.
Edge Cases: Input reading, multiple test cases.
Thus answer.
But before finalizing, let's re-express the condition in a more concise way: The answer is YES if and only if the maximum difference between any two consecutive elements in the sorted array is at most 1.
Alternatively, we could check: Let mn = min(a), mx = max(a). Then we need that for every integer i from mn to mx, there is at least one occurrence. Since values up to 100, we could count frequencies and check for any missing integer in range. That is also easy.
Implementation: After reading array, find min and max, then create a set or boolean array to check if every integer between min and max appears at least once. If any missing, NO; else YES.
But careful: Example [1,2,2,4] min=1 max=4, missing 3 -> NO. Works. However, [1,2,3] min=1 max=3, all appear -> YES. [1,2,2,3] -> all appear (1,2,3) -> YES. [1,1,3] min=1 max=3, missing 2 -> NO. So condition holds.
But note: [1,2,4,5] min=1 max=5, missing 3 -> NO. Indeed NO.
Thus we can also use that check.
But is it equivalent to the sorted adjacent difference condition? Yes, because if there is any missing integer between min and max, then when sorted, there will be some jump >1.
One nuance: The condition "all integers between min and max appear at least once" might be slightly stronger? For example, consider [1,3,3]? min=1 max=3, missing 2 -> NO. Indeed sorted [1,3,3] differences: 2 and 0 -> 2>1 -> NO. So fine.
Now, is there any case where sorted adjacent differences ≤1 but there is a missing integer? Actually if sorted adjacent differences are ≤1, then the distinct values are consecutive, so no missing integer. Conversely, if all integers between min and max appear, then sorted adjacent differences between distinct values are exactly 1 (or 0 for duplicates), so overall differences between consecutive sorted elements are ≤1. So they're equivalent.
Thus both methods work.
Given constraints small, either is fine.
I'll implement with sorting and checking differences, simpler.
Pseudo:
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
a.sort()
ok = True
for i in range(n-1):
if a[i+1] - a[i] > 1:
ok = False
break
print("YES" if ok else "NO")
Double-check with examples.
Edge: n=0? Not possible, n≥1.
Thus final answer.
But let's think about a possible counterexample suggested by the necessity proof: The argument that groups can't interact is sound. However, consider the possibility that before the gap becomes a problem, we could delete elements from one group in such a way that we introduce a new value that bridges the gap? No, because we only remove elements, never change values. So the set of values present is a subset of the original set. Thus, if a value is missing initially, it will never appear. So the gap persists.
Thus condition is necessary.
Now, is there any possibility where the array can be reduced to one element even with a gap if one group is completely eliminated before the gap matters? But as argued, elimination of a group requires deleting its last element, which is impossible due to missing neighbor. But what if the group has only one element to begin with, and that element is the one we intend to keep, and we eliminate the other group? Let's examine: Suppose lower group has a single element with value L, upper group has many elements with values from R upward, and there is a gap (R - L ≥ 2). We aim to keep the lower element L and eliminate the upper group. Is it possible to eliminate the upper group? Upper group's maximum is, say, M. To eliminate the upper group entirely, we need to delete all its elements, including the maximum M. However, to delete M, we need either duplicate M or M+1. Since M is the global maximum, no M+1 exists. So unless there are at least two copies of M, we cannot delete the last M. If there are two copies of M, we can delete one, but the last M still remains (since no larger). So we can never delete the last occurrence of the global maximum. Thus, the upper group cannot become empty; it will always retain at least one element (the global maximum). So we cannot eliminate the upper group. Conversely, we could try to keep an element from the upper group and eliminate lower group. Lower group's maximum L (which might be the only element if group size 1) cannot be deleted because no L+1. So lower group cannot become empty. Thus both groups always retain at least one element. Hence overall can't reduce to one.
Thus condition stands.
Therefore solution is correct.
We'll produce final answer accordingly.
But before finalizing, test a few more random cases to ensure no oversight:
Case: n=2, [1,2] -> diff=1 => YES. Operation: remove 1 using 2 => [2]. Yes.
n=2, [1,3] -> diff=2 => NO. Indeed no move.
n=3, [2,2,3] -> sorted:2,2,3 differences:0,1 => YES. Try: delete one 2 using other 2 -> [2,3]; delete 2 using 3 -> [3]. Yes.
n=3, [2,3,3] -> sorted:2,3,3 differences:1,0 => YES. Delete 2 using 3 -> [3,3]; delete one 3 -> [3]. Yes.
n=4, [1,2,3,3] -> YES.
n=4, [1,1,2,3] -> YES.
n=4, [1,1,1,2] -> YES.
n=4, [1,2,2,2] -> YES (end with 2).
n=4, [1,1,1,1] -> YES.
n=4, [1,1,3,3] -> NO.
n=4, [2,4,4,4] -> NO? Let's check: sorted:2,4,4,4 differences:2,0,0 -> 2>1 => NO. Attempt: Delete a 4 using duplicate? [2,4,4]; then 2 and 4 diff=2, can't delete 2; can delete 4 using duplicate? [2,4]; stuck. So NO.
n=5, [2,2,3,4,5] -> YES? Sorted:2,2,3,4,5 differences:0,1,1,1 => YES. Try: delete 2 using duplicate -> [2,3,4,5]; delete 2 using 3 -> [3,4,5]; delete 3 using 4 -> [4,5]; delete 4 using 5 -> [5]. Yes.
n=5, [1,2,4,5,6] -> diff:1,2,1,1 => NO.
All consistent.
Thus final.
We'll output code accordingly.
One more nuance: The problem statement says "remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one)." This is exactly what we used.
Now, to be thorough, could there be a scenario where the array is like [1,2,2,2] and we want to end with 1? We argued that's impossible because the last 2 cannot be deleted. But is it possible to end with 1 by some clever order? Let's test fully: start [1,2,2,2]. Option1: delete a 2 using duplicate: [1,2,2]; delete another 2 using duplicate: [1,2]; now we have 1 and 2 diff=1, we can delete 1 (smaller) or 2? Actually operation: if we pick 1 and 2, we must remove 1 because it's smaller. So we would remove 1, leaving 2. That gives 2, not 1. Could we instead delete 1 first? Pair 1 and 2, remove 1 -> [2,2,2]; then we have three 2's, can delete two of them to leave one 2. So final 2. Can we ever have final 1? To have final 1, we need to delete all 2's. To delete a 2, we need a 3 or duplicate. There is no 3. So we can delete two of the 2's using duplicates, but the last 2 cannot be deleted. So final cannot be 1. So indeed only possible final is 2. But overall YES because we can get to single element (2). So condition holds.
Now, could there be a scenario where the sorted differences are all ≤1, but still impossible because the only possible final element is something that cannot be achieved due to parity? Not that I see.
Thus answer is correct.
Implementation details: read t, loop, read n, read list (maybe split). Use input().split().
Now produce final solution. ```python
import sys
def solve() -> None:
it = iter(sys.stdin.read().strip().split())
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
a = [int(next(it)) for _ in range(n)]
a.sort()
ok = True
for i in range(n - 1):
if a[i + 1] - a[i] > 1:
ok = False
break
out_lines.append("YES" if ok else "NO")
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()