We need to find the longest subsequence of the given array that is "strange": For any two elements, absolute difference >= max element of that subsequence. Also any singleton or empty is automatically strange.
Given the condition: For all i<j: |b_i - b_j| >= MAX, where MAX is the maximum element in the subsequence. Note MAX is the largest value in the subsequence, not absolute value. Since elements can be negative, MAX might be negative or positive? The maximum element means the greatest numerical value. So if all numbers are negative, MAX is the largest (closest to zero) negative. For example (-2021, -1, -1, -1) is strange: check max? The largest among those is -1. Pairwise absolute differences: between -2021 and -1: | -2021 - (-1) | = 2020 >= 1? Actually MAX = -1, but condition uses MAX as a number (maybe negative). The absolute difference is non-negative. The condition is |a_i - a_j| >= MAX. If MAX is negative, then any non-negative absolute difference automatically >= a negative number? Actually if MAX is negative, say -1, then condition |diff| >= -1? But -1 is negative, and |diff| >= -1 is always true because left side is non-negative, right side is negative? Wait careful: The condition is |a_i - a_j| >= MAX. MAX is the largest element, which could be negative. But then the inequality |diff| >= (negative number) is automatically true because |diff| >= 0, and 0 >= negative number. But the example says (-2021, -1, -1, -1) is strange. Let's test: MAX = -1. Check pairs: between -2021 and -1: | -2021 - (-1) | = 2020 >= -1? 2020 >= -1 is true. Between -1 and -1: | -1 - (-1) | = 0 >= -1? 0 >= -1 is true. So indeed any sequence where the maximum is negative will automatically satisfy the condition because the RHS is negative, and LHS >= 0 always. But wait, is there any case where MAX is negative and condition fails? Since |diff| is always >= 0, and 0 >= negative number, so always satisfied. Therefore any subsequence consisting entirely of negative numbers (where the maximum is negative) is automatically strange. And also if there are zeros? If max = 0, then condition is |diff| >= 0, which is always true (since absolute diff >=0). So any subsequence where the max <= 0 is automatically strange. Because for max=0, condition is nonnegative >=0, true. For max negative, RHS negative, also true. So the only tricky part is when the subsequence contains at least one positive element, making MAX > 0. Then we need to ensure that any two elements have absolute difference at least MAX (which is positive). So the condition is restrictive only when max > 0.
Thus the problem reduces: we want the longest subsequence. Since any negative and zero elements can always be included without harming the condition, provided we don't include a positive element that causes violation? Actually if we include positive elements, we need to check condition with those positives. Also if we include only non-positive numbers (max <= 0), we can take all such elements (all a_i <= 0) and that subsequence will be strange. So a candidate answer is the count of elements <= 0. But perhaps we can also include some positive elements and still maintain strangeness, which might yield a longer subsequence if there are many positive numbers that satisfy condition among themselves and with negatives.
Thus we need to consider mixing positive numbers with non-positives.
First note: If the subsequence contains any positive element, then MAX is the largest positive element among them. Denote M = max(b_i) > 0. Then for every pair (i,j), we must have |b_i - b_j| >= M. This includes pairs where both are positive, both negative, or one positive one negative.
Since M > 0, condition is strong. Let's analyze.
Let’s denote the set of selected numbers: S. Let M = max(S) > 0. Then the condition is: For all x,y in S, |x - y| >= M.
We want to maximize |S|.
Observation: For any two elements in S, their difference must be at least M. This implies that the elements must be very spread out; they can't be too close. In particular, if we have two numbers that are both positive but less than M apart, they can't coexist. Also if we have a negative number and a positive number, the absolute difference is |positive - negative| = positive - negative if positive >= negative? Actually absolute difference = |x - y|. For negative x, positive y, difference = y - x if y >= x? Actually if x is negative and y positive, then y - x = y + |x| >= y. Since y <= M? Actually y could be less than M. But M is the max, so the largest positive is M. For any negative x, the absolute difference with M (the largest) is M - x = M + |x| >= M (since |x|>=0). So that condition is automatically >= M. But is it possible that a negative x and a smaller positive y (not the max) violate? Suppose y is positive but not max, and x is negative, then |x - y| = y - x = y + |x|. Since y < M, could y + |x| be less than M? Possibly if |x| is small and y is much smaller than M. For example, M=10, y=1, x=-1 => |1 - (-1)| = 2 < 10. That would violate condition because we need |x-y| >= M=10. So negative numbers can only be included if they are sufficiently far from all positive numbers (including the max). Actually for any negative x and any positive y (even the smallest positive), we need |x-y| >= M. Since |x-y| = y - x if y > x, which is y + |x|. For this to be >= M, we need y + |x| >= M. Since y <= M (max), the worst case is when y is the smallest positive? Actually condition must hold for all pairs. So for a fixed negative x, we need that for every positive y in S, |x - y| >= M. In particular, for the smallest positive (call p_min) among S, we need |x - p_min| >= M. Also for the largest positive M, need |x - M| >= M. Since |x - M| = M - x (if x <= M, x could be negative) = M - x = M + |x|, which is always >= M. So the condition with the maximum positive is automatically satisfied for any x <= 0 (since M - x >= M). But the condition with smaller positives might be problematic. So if we include any positive numbers besides the maximum, we must ensure that all numbers (including negatives) are at least M apart from each other? Let's analyze systematically.
We want to maximize size. Intuitively, including many negative numbers is easy, as long as we don't include any positive numbers that are too close to them. However, if we include a positive number M (the max), then for any other element x, we need |x - M| >= M. For x negative, that's automatically true. For x positive but less than M, we need |x - M| = M - x >= M => M - x >= M => -x >= 0 => x <= 0. But x > 0, so impossible unless M - x = 0? Actually M - x >= M implies -x >= 0 => x <= 0, contradiction. So for any other positive x < M, we would have M - x < M (since x>0, M-x < M). Thus condition fails. Therefore, if we include a positive maximum M, we cannot include any other positive number. Because for any other positive y < M, we have |M - y| = M - y < M. So it violates. So at most one positive number can be present in a strange subsequence when the max is positive. Indeed, if there are two distinct positive numbers, let the larger be M and smaller p, then |M - p| = M - p < M. So condition fails. So S can contain at most one positive element.
Now consider negative numbers and zeros (non-positives). Let M be the unique positive element (if any). Then for any negative (or zero) x, we need to satisfy the condition with that positive element? Actually condition with M is automatically satisfied for x <= 0 because |x - M| = M - x >= M (since -x >= 0). That's true. But also condition between negative elements themselves must hold: for any two non-positive elements x and y, we need |x - y| >= M. Since M > 0, this is a strong condition: the absolute difference between any two non-positive numbers must be at least M. Since non-positive numbers are <=0, the difference between them could be small if they are close. For example, two negatives -1 and -2: difference = 1, which might be less than M if M > 1. So we cannot include arbitrarily many non-positives; they need to be sufficiently spaced: the absolute difference between any two must be >= M. Also condition between the positive M and any non-positive is automatically ok, as argued. So the only constraints are: among the selected non-positive numbers, they must be pairwise distance at least M. Also there can be at most one positive (and if we include positive, it must be the max, and no other positive). Also note zeros are non-positive; they are <=0. So zero counts as non-positive. But if max is 0 (i.e., no positive), then condition is trivial: all non-positive (including zeros) automatically satisfy because max<=0. So in that case we can take all non-positive numbers. Also we could possibly include positive numbers? If we include a positive, then max becomes positive and we have the above constraints.
But wait, consider the case where we include no positive numbers: then max is either 0 (if any zero) or negative. Condition holds for any set of non-positive numbers. So then the longest strange subsequence would be all non-positive elements. Could including a positive increase the length? Possibly if the count of positive elements is 1 and we can include many non-positives that satisfy the spacing condition relative to that positive's value. However, we might also consider not including that positive and just take all non-positives. So the answer is max( #non_positive, some length including one positive + a subset of non-positives that are pairwise spaced by at least M ).
Additionally, could we have a strange subsequence that contains only positives? As argued, at most one positive. So if we take only one positive and no negatives, length = 1. That's likely smaller than many non-positives if there are many. So to get longer than #non_positive, we need to include a positive plus many non-positives that are sufficiently spaced. But note that non-positives themselves must be pairwise distance >= M. Usually M is positive, perhaps large, which forces that the non-positives must be far apart. That might severely limit how many we can select. In contrast, taking all non-positives yields potentially many elements. So unless the number of non-positives is very small, the best might be to take all non-positives. However, we also note that if there are no non-positives (all numbers positive), then the best we can do is at most one positive, length 1 (or possibly 0? but singleton always strange, so answer >=1). But if all numbers are positive, we could also consider taking any single element; that's 1. Could we take two positives if they are both negative? No, they are positive, so max positive >0, two positives cannot coexist. So answer =1. If there is at least one non-positive, we could also take only non-positives, which might be many. So answer is at least #non_positive.
But the examples: Let's test with provided examples.
Example 1: n=4, a = [-1, -2, 0, 0]. Non-positives count = 4 (all). Output 4. So fits.
Example 2: 7 elements: -3,4,-2,0,-4,6,1. Non-positives: -3,-2,0,-4 => 4 elements. But output is 5, which is larger than 4. So they got length 5. So there is a strange subsequence of length 5 that includes some positives. Indeed they said (a1, a3, a4, a5, a7) => -3,-2,0,-4,1? Wait a7=1? Actually given: a1=-3, a2=4, a3=-2, a4=0, a5=-4, a6=6, a7=1. So the subsequence they gave: a1 (-3), a3 (-2), a4 (0), a5 (-4), a7 (1). That's elements: -3, -2, 0, -4, 1. That includes one positive (1) and four non-positives. Check condition: max = 1. Need all pairwise absolute differences >=1. Check between non-positives: -3 and -2: diff=1 >=1 ok. -3 and 0: diff=3>=1 ok. -3 and -4: diff=1>=1 ok. -2 and 0: diff=2>=1 ok. -2 and -4: diff=2>=1 ok. 0 and -4: diff=4>=1 ok. Check with positive 1: 1 and -3: diff=4>=1 ok; 1 and -2: diff=3>=1; 1 and 0: diff=1>=1; 1 and -4: diff=5>=1. So condition holds. So indeed we can include 1 positive and many non-positives if the non-positives are spaced at least 1 apart. In this set, non-positives are -4,-3,-2,0. Their differences: successive differences are 1,1,2, etc. All differences >=1. So they satisfy pairwise diff >=1. So this set of non-positives is a subset that is "1-separated". The count of such non-positives we can select depends on M (the chosen positive). We can choose the positive that allows many non-positives to be included.
Thus the problem becomes: Given an array of integers, find the maximum size of a subset S such that either (Case 1) S contains only non-positive numbers (max <=0) -> size = count of all non-positive elements; or (Case 2) S contains exactly one positive number p (>0) and some set of non-positive numbers (including zero) such that all pairwise differences among the non-positives are at least p, and also differences between p and each non-positive automatically satisfy? We argued p with any non-positive x <=0 gives |p - x| = p - x >= p (since -x>=0). So automatically satisfied. So only condition is: Among the chosen non-positives, they must be pairwise at least p apart in absolute difference.
Also note: Could we include zero? Zero is non-positive, works.
But also what about including negative numbers only, with max negative? That is covered by case 1 because max <=0. So case 1 includes all subsets with no positive, but we can take all non-positives without any restriction. However, maybe we could take a subset of non-positives that is not all but includes some positives and yields larger total? Since we can always take all non-positives, that's a candidate. So case 2 only matters if we can exceed that count. But it's possible that number of non-positives is small, and we can add a positive to get +1, maybe yielding longer than all non-positives? For instance, if non-positives count = 0, we can take one positive => length 1, while all non-positives=0, so 1 > 0. That fits. But if there are non-positives, can we add a positive to a subset of them to exceed total? The total length = 1 + (size of chosen subset of non-positives). If we choose all non-positives, total = #non-positives + (maybe add a positive? but we can't add a positive if we already have all non-positives? Because including a positive would change max to that positive, and then we would need to check condition among non-positives: they must be p-separated. If we take all non-positives, they may not satisfy the separation condition for some p. So we might need to drop some non-positives to accommodate the positive. So we might have to trade off: drop some non-positives to include a positive, hoping the total (1 + selected non-positives) > #non-positives. That means we need selected non-positives >= #non-positives - 1? Actually 1 + k > N_nonpos => k >= N_nonpos? Since 1 + k > N_nonpos => k >= N_nonpos (if integers). But if k = N_nonpos, then 1+N_nonpos > N_nonpos, so longer. But is it possible to select all non-positives and also a positive? That would give length N_nonpos+1, but need condition among non-positives pairwise diff >= p. If all non-positives satisfy that for some p, then we could indeed get N_nonpos+1. But if they don't, we might drop some non-positives to meet condition. So case 2 could give length = 1 + k where k is the maximum size of a subset of non-positives such that the pairwise absolute differences are at least some p (p being the chosen positive). But p is a positive number we can choose from the array (any positive element). Different p may allow different maximum k. So we need to consider for each positive p present (or maybe we can consider p not in array? But subsequence must consist of array elements. The positive we include must be an element from the array. So we can only choose p from the set of positive numbers in a.)
Thus problem reduces to: Let A be the list of non-positive elements (including zero). Let B be the list of positive elements. We can always take all A (case1) => ans >= |A|. Additionally, for each positive p in B, we can consider selecting p and a subset S of A such that for all x,y in S, |x - y| >= p. We want to maximize |S|+1. Since we can choose any subset S, the best for given p is the largest possible subset of A that is p-separated (i.e., the elements have pairwise differences >= p). Among all p, we take max of (1 + maxSize(p)), and then answer = max(|A|, 1 + maxSize(p) over p in B). Also note we could consider case with no positive but subset of A? But taking all A is optimal for case1 because no restrictions; we can include all non-positives. So case1 is simply |A|.
Thus main difficulty: For each positive p, compute the maximum size of a subset of A (non-positive numbers) where any two differ by at least p. Since A contains non-positive numbers (<=0), they are sorted increasingly? Actually as numbers become more negative, the order: e.g., -100, -10, -5, 0. They are increasing if we sort normally: -100 < -10 < -5 < 0. So we can sort A. Then we want to choose as many elements as possible such that the difference between consecutive chosen elements (in sorted order) is at least p. This is a classic greedy: take the smallest possible elements? Actually to maximize count given a minimum gap, we can greedily pick elements from smallest to largest (most negative to 0) adding if the difference from the last taken is at least p. That yields the maximum cardinality for a given p because the set is 1D and we want maximum independent set with minimum distance; it's like a "maximum size of a p-separated subset" which can be obtained by greedy: sort and then take the smallest, then next that is >= last + p, etc. Since numbers are not necessarily integers? But they are integers. However p is positive integer (since array elements are integers, p is positive integer). The condition |x - y| >= p is symmetric; in sorted order, if we have x <= y, then condition is y - x >= p. So the greedy works: sort ascending, then take the first element, then as long as next - last >= p, take it, update last.
But careful: Since numbers are non-positive, the differences: e.g., -10 and -5: -5 - (-10) = 5, which is >= p if p <=5. So greedy works.
Thus for each p, we can compute k(p) = maximum size of a p-separated subset of A (including possibly zero). However, we need to consider that we may also choose to not include all A; the greedy yields the maximum cardinality. So for each p, we can compute that.
But the number of distinct positive p can be up to n, and n overall test cases sum to 1e5. Could we compute for all p efficiently? Naively, for each p, we would need to scan A sorted, O(|A|) per p, leading to O(|A|*|B|) which could be O(n^2) worst-case (if half positives, half negatives). But total n sum 1e5, maybe O(n^2) too high.
We need a more efficient method. Possibly note that p only matters up to certain values; maybe we can precompute the maximum possible size for various p via binary search on p? But we need to consider each p individually? Perhaps we can find the optimal p that maximizes (1 + k(p)). Since k(p) is non-increasing as p increases: larger p imposes stricter separation, so the maximum subset size is decreasing with p. So we want to maximize 1 + k(p). k(p) is a step function decreasing as p increases. Among all positive p, the best might be for small p, because k(p) is larger when p is small. But small p may not be present as a positive element; we can only use p that exist in B. So the best p is likely the smallest positive in the array? But not necessarily, because if the smallest positive is 1, and there is also a positive 2, then k(1) >= k(2). So 1 + k(1) >= 1 + k(2). So the smallest positive yields the largest k(p) because p is smallest, giving weakest constraint. So the maximum of 1 + k(p) would be achieved at the smallest positive value present. Is that always true? Let p_min = min(B). For any other p > p_min, k(p) <= k(p_min). So 1+k(p) <= 1+k(p_min). Therefore the optimal positive to include is the smallest positive element. However, there is a nuance: The positive element itself must be present; if we choose p_min, we include that element. But does including that element impose any additional restrictions on the non-positives? Already we considered that all non-positives must be p-separated. With p_min being smallest positive, we have the weakest constraint. So indeed, if we include any positive, we would choose the smallest positive to maximize possible non-positives. So answer for case2 would be 1 + k(p_min) where p_min is the smallest positive in the array (if any). But is it always valid to include that specific positive p_min? We also need to ensure that p_min indeed is >0 and exists. So yes.
But wait: Could there be a scenario where we could include a positive that is not the smallest but the set of non-positives we pick is larger than k(p_min)? No, because k(p) is non-increasing with p. So for larger p, k(p) <= k(p_min). So maximum is at smallest p.
Thus we only need to compute k(p_min) where p_min = the smallest positive number in the array. However, there might be the scenario where there are multiple copies of the same smallest positive. That's fine; we can include any one.
But is it always optimal to include a positive? The answer is max(|A|, 1 + k(p_min)) if B non-empty. If B empty, answer = |A|. Additionally, we might consider the case where we include no positive but take all non-positives, which is |A|.
So seems simple: Count non-positives (<=0) as cnt_nonpos. Let minPos = minimum positive number (if any). Compute the maximum size of a subset of non-positives such that pairwise absolute differences >= minPos. Then answer = max(cnt_nonpos, 1 + maxSize(minPos)).
But wait, example 2: A = [-3, -2, 0, -4] => sorted: -4, -3, -2, 0. cnt_nonpos = 4. minPos? Positives are 4, 6, 1. Minimum positive is 1. Compute k(1) for A: we want largest subset with differences >=1. Greedy: start with -4, take it; next -3: -3 - (-4) = 1 >=1 => take; next -2: -2 - (-3) = 1 >=1 => take; next 0: 0 - (-2) = 2 >=1 => take. So we can take all 4. So 1+4=5. Answer is max(4,5)=5. Works.
Example 3: Input: 5
0 5 -3 2 -5
A: 0, -3, -5 => nonpos = 3? Actually 0 is non-positive, -3, -5: yes 3. cnt_nonpos=3. Positives: 5,2. minPos = 2. Compute k(2) on A: sorted: -5, -3, 0. Greedy: start -5 (take), next -3: diff = 2 >=2 => take, next 0: 0 - (-3) = 3 >=2 => take. So all 3 can be taken? Check pairwise: |-5 - (-3)|=2>=2; |-5-0|=5>=2; |-3-0|=3>=2. Yes. So 1+3=4. Answer max(3,4)=4. Output is 4. Works.
Example 4: n=3, a=[2,3,1] (all positive). cnt_nonpos = 0. minPos = 1 (smallest positive). k(1) for A (empty) = 0. 1+0=1. Answer = max(0,1)=1. Output 1. Works.
Example 5: n=4, a=[-3,0,2,0]. A: -3,0,0 => cnt_nonpos=3. Positives: 2. minPos=2. Compute k(2) for A: sorted: -3,0,0. Greedy: start -3 (take). next 0: 0 - (-3) = 3 >=2 => take (first 0). next 0: 0 - 0 = 0 < 2 => skip. So max size = 2. Then 1+2=3. cnt_nonpos=3, answer max(3,3)=3. Output 3. Works.
Example 6: n=6, a=[-3,-2,-1,1,1,1]. A: -3,-2,-1,? Are there zeros? No. Actually elements: -3,-2,-1,1,1,1. So A = [-3,-2,-1] (since negative numbers; zero not present). cnt_nonpos=3. Positives: 1,1,1. minPos = 1. Compute k(1) for A: sorted: -3,-2,-1. Greedy: start -3 (take), next -2: diff=1>=1 => take, next -1: diff=1>=1 => take. So all 3. 1+3=4. Answer = max(3,4)=4. Output 4. Works.
So matches all examples.
But we must be cautious: Could there be a scenario where including the smallest positive is not optimal because the smallest positive might be so small that the condition on non-positives becomes too weak but the positive itself may be very small, but wait: The condition uses MAX which is the maximum element. If we include the smallest positive p_min, then MAX = p_min (assuming all non-positives are <=0 <= p_min). That's fine. The only restriction is that non-positives must be at least p_min apart. So smaller p_min is easier. So indeed including p_min is best among positives.
But consider if there are negative numbers and zeros, and we include a positive p that is not the smallest, maybe we could also include additional positive? No, only one positive allowed. So indeed p_min is best.
But there is a nuance: The set of non-positives we select must also satisfy the condition with each other. That condition depends on p (MAX). So p_min gives weakest condition, thus maximum possible number of non-positives we can include. So optimal.
But is it always possible to achieve the greedy maximum subset size k(p_min) while also including p_min? The greedy algorithm yields some subset of A, but we need to ensure that p_min itself, when added, does not break any condition with the selected non-positives. As argued, for any non-positive x, |p_min - x| = p_min - x (since p_min >0, x <=0) = p_min + |x| >= p_min (since |x| >=0). So it's automatically satisfied. So including p_min does not impose extra restrictions beyond those among non-positives. So we can take the greedy maximal subset of A for gap p_min, and add p_min, and the whole set is strange. So 1+k(p_min) is achievable.
Thus answer = max(cnt_nonpos, 1 + maxSize(A, p_min)) if there exists at least one positive; else cnt_nonpos.
But we must also consider case where there are multiple copies of the same smallest positive? Could we include more than one positive if they are equal? Wait, earlier we argued that if there are two positive numbers, even if they are equal, say both are p, then max = p, and |p - p| = 0 < p. So condition fails because need >= p. So cannot have two positives even if equal. So at most one positive.
Thus answer is as above.
Now we need to compute maxSize(A, p) efficiently for p = min positive (if any). Since we only need to compute once per test case, we can simply sort A (non-positive numbers) and run greedy scanning O(|A|). That's fine because total n over test cases ≤ 1e5. So overall O(n log n) per test case? Actually we need to sort A and B perhaps. But we just sort the whole array or just A? We can separate.
But careful: A includes zeros. And note: if there is no positive, answer is cnt_nonpos. That is trivial.
Thus algorithm per test case:
- Read n, array a.
- Let neg_zero = [x for x in a if x <= 0].
- Let pos = [x for x in a if x > 0].
- cnt = len(neg_zero).
- If pos is empty: answer = cnt.
- Else: min_pos = min(pos).
- Sort neg_zero ascending.
- Run greedy to select as many as possible with min gap = min_pos:
- If neg_zero empty: select = 0.
- else: last = neg_zero[0]; taken = 1; for i from 1 to len-1: if neg_zero[i] - last >= min_pos: taken++; last = neg_zero[i].
- answer = max(cnt, taken + 1).
- Output answer.
But wait, is cnt always >= taken? Since taken is the maximum size of a p-separated subset, it could be less than cnt, but maybe we can also take some of the neg_zero that are not selected? But we considered the maximum we can take under gap p. That's taken. So for case2 we have taken+1. For case1 we have cnt. So answer = max(cnt, taken+1).
But need to double-check: Could there be a scenario where we include a positive and also some non-positives, but the positive is not the minimum positive? That would give taken' ≤ taken, so less total. So not better.
Edge Cases: What if min_pos is very large, say 10^9, then the greedy may take at most 1 non-positive? Actually the condition among non-positives: they must differ by at least 10^9. Since non-positives are at most 0 and at least -10^9, the range is 10^9. So it's possible to take at most 2 if we pick the smallest and the largest maybe? But anyway.
But what about including zero? Zero is non-positive; works.
Now, is there any scenario where we could achieve a longer subsequence by including no positive, but taking a subset of non-positives that is larger than cnt? No, cnt is all non-positives, and we can take all. So case1 can't be exceeded.
Thus solution seems straightforward.
But let's test more cases mentally:
Case: a = [-5, -3, -1, 2, 4]. Non-positives = [-5,-3,-1] count=3. min_pos=2. Greedy on sorted [-5,-3,-1] with gap 2: -5 take; -3: diff=2 >=2 take; -1: -1 - (-3)=2 >=2 take => all 3. So 1+3=4 >3 answer 4. Is there a strange subsequence of length 4? Take -5,-3,-1,2. Check: max=2. Pairwise differences: among negatives: as computed 2,4,2 all >=2? |-5-(-3)|=2 ok; |-5-(-1)|=4 ok; |-3-(-1)|=2 ok. With 2: 2-(-5)=7>=2; 2-(-3)=5>=2; 2-(-1)=3>=2. So yes length 4. Could we also include 4? No, because max would become 4, then differences with negatives: e.g., 4-(-5)=9>=4, ok; but with 2: |4-2|=2<4, fails. So only one positive.
Case: a = [-2, -1, 0, 3]. Non-positives count=3. min_pos=3. Greedy on sorted [-2,-1,0] gap 3: take -2; next -1: diff=1<3 skip; 0: 0-(-2)=2<3 skip? Actually after -2, next candidate -1 diff=1<3 skip; then 0 diff from -2=2<3 skip. So taken=1. Then 1+1=2. Answer max(3,2)=3. So better to take all non-positives. Indeed check if we take -2,-1,0,3? max=3, need differences >=3. Among negatives: |-2-(-1)|=1<3 so fails. So can't include all three. So answer 3.
Case: all negative: answer = n.
Case: mix with positive but many non-positives that are tightly clustered: e.g., a = [-1, -1, -1, 1]. Non-positives count=3. min_pos=1. Greedy on sorted [-1,-1,-1] gap 1: start -1 take; next -1 diff=0<1 skip; next -1 diff=0<1 skip => taken=1. So 1+1=2. Answer max(3,2)=3. So all negatives works. Indeed, taking all three negatives is strange because max=-1? Actually all negatives: max = -1, condition |diff| >= -1 always true. So length 3. So answer 3.
Now, are there any tricky aspects? Let's think deeper about the condition: The formal definition says: For every pair (i,j), |a_i - a_j| >= MAX, where MAX is the largest element of the sequence. It didn't specify absolute value of MAX; MAX is just the largest element. So if MAX is negative, condition becomes |diff| >= negative number, always true. So fine.
But what if MAX is 0? Then condition is |diff| >= 0, always true. So any set with max=0 is okay.
Thus any set with max <= 0 has no restrictions.
Now consider if we have both positive and negative, but we choose a positive that is not the smallest, but perhaps we could also include some non-positives that are far apart, and the total might be larger than cnt_nonpos? But cnt_nonpos is all non-positives, which may be many but may not be all allowed with that positive. However, since cnt_nonpos >= taken for any p, and cnt_nonpos is the full count, the only way case2 beats case1 is if taken+1 > cnt_nonpos. Since taken <= cnt_nonpos, this requires taken = cnt_nonpos, and then taken+1 > cnt_nonpos, i.e., we can include all non-positives plus one positive. So case2 beats case1 only if all non-positives already satisfy the gap condition for some positive (specifically the smallest positive). Then we can add that positive to get length cnt_nonpos+1. If not, case1 yields cnt_nonpos, which may be larger than taken+1. So essentially the answer is either cnt_nonpos or cnt_nonpos+1, depending on whether we can add a positive without dropping any non-positive. But wait, is it possible that taken+1 > cnt_nonpos even when taken < cnt_nonpos? For example, suppose cnt_nonpos=5, taken=4, then taken+1=5, equal to cnt_nonpos. So not better. For taken+1 to be strictly greater than cnt_nonpos, we need taken >= cnt_nonpos. Since taken <= cnt_nonpos, this forces taken = cnt_nonpos. So the only way case2 yields a strictly larger answer is if we can take all non-positives and also one positive. So the answer is either cnt_nonpos or cnt_nonpos+1, provided there is at least one positive and the non-positives satisfy the separation condition with respect to the smallest positive. But is it possible that taken+1 could be > cnt_nonpos even if taken < cnt_nonpos? Let's think: cnt_nonpos is the total number of non-positive elements. taken is the maximum size of a subset of them satisfying the gap. Since we can always take all non-positives in case1, that yields cnt_nonpos. So to beat that, we need to find a set of size > cnt_nonpos. The only way to get > cnt_nonpos is to include at least one positive. So the size is 1 + (some subset of non-positives). The maximum possible is 1 + taken. For this to exceed cnt_nonpos, we need taken > cnt_nonpos - 1, i.e., taken >= cnt_nonpos. Since taken ≤ cnt_nonpos, that implies taken = cnt_nonpos. So indeed the only improvement possible is by +1 when all non-positives already satisfy the gap. So the answer is either cnt_nonpos or cnt_nonpos+1. Check examples: Example 1: cnt_nonpos=4, but answer=4, not 5; so non-positives must not allow adding positive? Let's check example1: array [-1,-2,0,0]. Non-positives = all 4. min positive? There is no positive; so answer =4. So no positive.
Example2: cnt_nonpos=4, answer=5 => cnt_nonpos+1. Yes.
Example3: cnt_nonpos=3, answer=4 => +1.
Example5: cnt_nonpos=3, answer=3 => no +1.
Example6: cnt_nonpos=3, answer=4 => +1.
So pattern holds.
Thus we can simplify: answer = cnt_nonpos + (1 if there exists a positive p such that all non-positives (sorted) have consecutive differences >= p, i.e., the minimum difference between any two distinct non-positives? Actually the condition for being able to take all non-positives and add a positive p is: For every pair of non-positives (including duplicates?), we need |x - y| >= p. This is equivalent to the minimum absolute difference among any two distinct non-positives being at least p. Since if there are duplicates (difference 0), then condition fails because 0 < p (p >0). So duplicates among non-positives would prevent taking all non-positives with any positive p >0. Indeed, if there are two equal non-positives, then their difference is 0, which is < p for any positive p. So all non-positives must be distinct? Actually not necessarily distinct but if there are duplicates, you cannot take both because then |x - x| = 0 < p. So to include all non-positives, they must all be distinct? But also the differences between distinct ones must be >= p. So the set of non-positives must be such that the minimal absolute difference between any two is at least p. This implies that they are all at least p apart. So if the non-positives are tightly clustered, you cannot include all.
But we might still be able to include a positive while dropping some non-positives to get total > cnt_nonpos? Since total would be 1 + (subset) and if subset size = cnt_nonpos - d, then total = cnt_nonpos - d + 1. To exceed cnt_nonpos, we need d=0. So dropping any non-positive reduces total below cnt_nonpos+? Actually if we drop some, total = 1 + (cnt_nonpos - d) = cnt_nonpos + 1 - d. This is > cnt_nonpos only if d = 0. So indeed only if we can keep all non-positives does the total exceed cnt_nonpos. If we drop any, total ≤ cnt_nonpos. So the only case where including a positive yields a strictly longer subsequence is when we can include all non-positives. So the answer is cnt_nonpos + (1 if there is at least one positive and all non-positives are pairwise separated by at least the smallest positive). But careful: What if there are no non-positives (cnt_nonpos=0)? Then answer = 1 if there is any positive (since we can take one positive). That is cnt_nonpos+1, which fits the condition: "all non-positives are pairwise separated by at least the smallest positive" is vacuously true because there are no non-positives. So we can add +1 if there is any positive. So that's consistent: For cnt_nonpos=0 and positive exists, answer = 1 = 0+1.
If cnt_nonpos>0 and there is a positive, we need to check if we can take all non-positives together with that positive. That requires that for every pair of non-positives, |x - y| >= min_pos. That is equivalent to: after sorting non-positives, the minimum difference between consecutive elements is >= min_pos. Additionally, duplicates: if any duplicate exists, min diff = 0 < min_pos, so fails.
So answer = cnt_nonpos + (1 if (pos nonempty) and (min_diff_nonpos >= min_pos) else 0). But careful: The condition is pairwise, but if the min consecutive diff >= min_pos in sorted order, that ensures all pairs are >= min_pos? Not necessarily: For sorted values v1 <= v2 <= ... <= vm, the minimal difference among any pair is actually the minimum of consecutive differences. Because for any i<j, vj - vi >= v_{i+1} - vi (since vj - vi = sum of consecutive gaps). So if each consecutive gap >= min_pos, then all gaps are >= min_pos. So yes, it's sufficient.
Thus we can compute:
- cnt_nonpos = count of <=0.
- If cnt_nonpos == 0: answer = 1 if there is any positive else 0? But n>=1, so at least one element. If all are positive, answer =1. (Because we can take any one positive, or maybe we can take more? No, can't take two positives.)
- If cnt_nonpos > 0:
- if no positive: answer = cnt_nonpos.
- else:
- min_pos = minimum positive.
- Sort the non-positive list.
- Check if for all i from 0 to len-2, nonpos[i+1] - nonpos[i] >= min_pos.
- If true: answer = cnt_nonpos + 1.
- else: answer = cnt_nonpos.
But wait, is this always optimal? Consider scenario where there are multiple positives, but the smallest positive is not the one we can use because maybe that smallest positive is not present? It is present. So we can use it.
But could there be a scenario where even though the non-positives are not all pairwise separated by min_pos, we might still be able to achieve cnt_nonpos+1 by selecting a different positive that is smaller? But min_pos is the smallest; any other positive is larger, so the condition would be even stricter. So if condition fails for min_pos, it definitely fails for larger p. So no improvement.
Thus answer is as above.
Let's test with example 5: nonpos = [-3,0,0]. Sorted: [-3,0,0]. Consecutive differences: between -3 and 0 => 3; between 0 and 0 => 0. min diff = 0 < min_pos=2. So condition fails => answer = cnt_nonpos = 3. That matches.
Example 2: nonpos = [-3,-2,0,-4] sorted: [-4,-3,-2,0] diffs: 1,1,2 min=1 >= min_pos=1? min_pos=1, min diff=1 >=1 true => answer = 4+1=5. Works.
Example 6: nonpos = [-3,-2,-1] diffs: 1,1 min=1 >= min_pos=1 => answer = 3+1=4.
Example 3: nonpos = [-5,-3,0] diffs: 2,3 min=2 >= min_pos=2 => answer = 3+1=4.
Example 1: no positive => answer = 4.
Example 4: cnt_nonpos=0, positive exists => answer = 1.
So far so good.
But is there any scenario where taking all non-positives is not optimal because we might drop some non-positives and include a positive to get a total equal to cnt_nonpos (i.e., same as all non-positives), but maybe that's not better; we just take all non-positives.
But could there be scenario where cnt_nonpos = 0 (all positives) and we might be able to take two positives if they are both non-positive? No, they are positive. So only one.
What about including zero? Zero is non-positive, so fine.
Thus solution seems trivial: answer = count of non-positive + (1 if there exists a positive and all non-positive elements (if any) are spaced at least the minimum positive apart). That's O(n log n) due to sorting.
But wait, is there any nuance about the case where max is zero? That is covered by cnt_nonpos (since zero is non-positive). And if we have zeros and also positive numbers, but zero is not positive. So we can take all zeros and negatives, and if they are spaced properly we can also add a positive. But zeros may be duplicates: if there are multiple zeros, difference between zeros is 0, which would fail the condition for any positive >0. So if there are at least two zeros, we cannot include all non-positives plus a positive, because two zeros would violate. Could we instead drop one zero and include the positive, giving total = (cnt_nonpos -1) +1 = cnt_nonpos, which doesn't improve. So all non-positives remains best.
Thus algorithm is correct.
But let's double-check the reasoning: Could there be a scenario where the longest strange subsequence contains no non-positive numbers but multiple positives because the maximum is not positive? No, if all numbers are positive, the max is positive. So condition fails for any two positives. So only one positive.
What about a mix where we include both a positive and a zero, and zeros are many but zeros are same; but if we have two zeros, we can't include both with a positive. However, maybe we can include two zeros and a positive if zeros are considered? Condition: for zeros, |0 - 0| = 0, need >= max (which would be positive). So fails. So can't have two zeros.
Thus indeed.
Now, but careful: The condition uses MAX as the largest element in the subsequence. If we have a subsequence that contains only non-positive numbers, the max is the largest among them, which could be 0 or a negative. If the max is negative, condition is automatically true. If max is 0, condition is |diff| >= 0, always true. So no restrictions. So all non-positives can always be taken together, regardless of their values. That is a crucial point: Even if non-positives have duplicates or are close, as long as max <=0, condition holds. So case1 always yields cnt_nonpos.
Now, case2: we try to add a positive. Then max becomes that positive >0. Now the condition must hold for all pairs, including pairs among the non-positives. So non-positives must satisfy the gap condition. So that's why we need them spaced.
Thus answer as described.
But let's verify more edge cases.
Case: array has one positive, many non-positives that are all spaced >= that positive. Then answer = cnt_nonpos+1.
Case: array has multiple positives, smallest positive = 1, non-positives include numbers that are not 1-separated, e.g., -5, -5.1? But integers only. So duplicates would cause diff 0. So if there is any duplicate non-positive, condition fails. Also if there are two non-positives with difference 0 (duplicates), fails. If there are two with difference 1 but min_pos=1, then diff=1 ok. So condition: min diff >= min_pos.
But what about negative numbers that are spaced less than min_pos? E.g., non-positives = [-10, -9], min_pos=2, diff=1 <2, fails.
Now, is it possible that the best subsequence includes a positive that is not the smallest but we drop some non-positives and still get total = cnt_nonpos? That would be equal, not better. But could it be that cnt_nonpos is zero and we have many positives; we can only take one positive => answer 1. That's correct.
Now, check if there is any scenario where a strange subsequence could contain more than one positive if some positive is not the maximum? No, the maximum would be the largest positive, and any other positive would be smaller, then difference with max < max. So can't.
Thus solution is indeed simple.
But wait, could there be a scenario where we have non-positives and we also include a positive, but the max is not that positive because there is a larger non-positive? No, non-positives are ≤0, positive >0, so positive is always larger. So max is the positive.
Now, could there be a scenario where we include a positive and some non-positives, and the condition between non-positives is automatically satisfied because the max (positive) is very large? No, condition is |x - y| >= max, which is large, so it's stricter, not automatically satisfied. So must check.
Thus answer stands.
Now, we should also consider if there is possibility to have a strange subsequence with max <=0 that includes some positive? No, if any positive present, max >0.
Thus final algorithm per test case:
- Input n, list a.
- Separate into neg_zero = [x for x in a if x <= 0], pos = [x for x in a if x > 0].
- cnt = len(neg_zero)
- if not pos: print(cnt); continue.
- min_pos = min(pos)
- if cnt == 0: print(1) (since we can take one positive) Actually if cnt==0 and pos nonempty, we can take one positive => 1. That's covered by cnt+1? cnt=0, condition? We need to check if we can add positive? The condition for adding +1 would be: if all non-positives are spaced >= min_pos (vacuously true), so answer = cnt + 1 = 1. So works if we handle.
- else: sort neg_zero.
- check consecutive differences: for i in range(1, cnt): if neg_zero[i] - neg_zero[i-1] < min_pos: ok = False; break.
- if ok: print(cnt + 1) else: print(cnt).
Time complexity: O(n log n) per test case due to sorting neg_zero. Since total n ≤ 1e5, fine.
But we need to be careful: The problem allows n up to 1e5 per test, sum ≤ 1e5. Sorting each test case is fine.
But is there any scenario where we might get answer > cnt+1? Could we include more than one positive by also including a larger positive and dropping the smaller? That would still be only one positive. And non-positives may be taken as many as possible. So maximum size is at most cnt + 1. Could there be a scenario where cnt is not the total non-positives but we can take a subset of non-positives and two positives? No, because two positives impossible. So answer ≤ cnt + 1. So indeed answer is either cnt or cnt+1.
Thus we can even avoid greedy and just check if condition for all non-positives holds for min_pos.
But wait, what about the possibility of not taking all non-positives but taking a subset that is larger than cnt? That's impossible because subset of non-positives can't exceed total count of non-positives.
Thus solution is straightforward.
But before finalizing, we should double-check the logic with more examples, especially borderline with negative and zero.
Consider: a = [-5, -2, 0, 0, 1]. cnt_nonpos=4 ( -5,-2,0,0). min_pos=1. Sorted nonpos: -5, -2, 0, 0. diffs: -2 - (-5)=3 >=1; 0 - (-2)=2>=1; 0-0=0 <1 => fails. So answer = 4. Could we get 5? If we take -5,-2,0,1 (drop one zero) => check: non-positives: -5,-2,0. Are they all pairwise diff >=1? -5,-2 diff=3; -5,0 diff=5; -2,0 diff=2 => ok. With 1: differences: 1-(-5)=6>=1; 1-(-2)=3>=1; 1-0=1>=1. So length 4? Actually that's 4 elements: -5,-2,0,1 -> length 4. That's equal to cnt_nonpos (which is 4). The full non-positives set (all four) yields length 4 without positive. So both give 4. So answer 4.
What about taking -5,0,0,1? That fails because two zeros. So not.
Thus answer 4 is correct.
Now consider a = [-5, -4, -3, 2]. cnt_nonpos=3. min_pos=2. Sorted nonpos: -5,-4,-3. diffs: 1,1 both <2 => fails. So answer = 3. Could we take a subset of non-positives and the positive to get 3? If we take -5,-3,2: check nonpos pairwise: |-5 - (-3)|=2>=2 ok; with 2: 2-(-5)=7>=2, 2-(-3)=5>=2 => length 3. That's same as cnt_nonpos. If we take -5,-4,2: diff -5,-4=1<2 fails. So 2 nonpos + positive =3. So answer 3 matches.
Now consider a = [ -10, -8, -6, -4, -2, 1, 1] . cnt_nonpos=5. min_pos=1. Sorted: -10,-8,-6,-4,-2. diffs: 2,2,2,2 all >=1 => passes. So answer=6. Check: take all negatives and one 1. Pairwise among negatives: differences all even numbers >=2 >=1. So ok. With 1: 1-(-10)=11>=1 etc. So length 6. Could we also take two 1's? No, because two positives not allowed.
Now consider a = [ -10, -9, -8, 2, 3] . cnt_nonpos=3. min_pos=2. Sorted nonpos: -10,-9,-8. diffs: 1,1 <2 => fails. So answer =3. Could we get 4 by dropping one nonpos? If we take -10,-8,2: diffs among nonpos: 2 >=2 ok; +2 => total 3? Actually that's 3 elements, not 4. If we take -10,-9,2,3? No, two positives not allowed. So max 3.
Thus consistent.
Now consider case where there is a positive that is not the smallest but we might drop some non-positives and include that positive and get cnt_nonpos? That would be equal or less.
Thus the problem reduces to: answer = number of non-positive elements + (1 if there is at least one positive and the minimum positive ≤ minimum absolute difference among non-positive elements? Actually condition is that the min diff among non-positives is >= min_pos. So if min diff among non-positives is >= min_pos, we can add a positive.
But careful: What if there is a positive that is 0? No, positive >0, so min_pos >=1. So min diff among non-positives must be at least 1? Possibly more.
Thus we can implement.
But wait: There's one nuance: The subsequence must consist of elements from the original array. When we add a positive, we must ensure that there is at least one positive element to add. That's given.
Now, does the positive we add have to be the smallest positive? Suppose we want to add a positive, we can choose any positive. If the condition holds for the smallest positive, then we can add that one. If it only holds for a larger positive but not the smallest, could that yield cnt_nonpos+1? Let's examine. Suppose non-positives have min diff = 3. Smallest positive = 1. Then condition for p=1: need min diff >=1, which is true (3 >=1). So it holds. So we can add the smallest. If the smallest positive is 5 and min diff=3, then condition fails for p=5 (since need min diff >=5). Could we add a different positive, say 4, if 4 exists? But smallest positive is 5, so 4 doesn't exist. So we can't add a positive that is not present. So if smallest positive is 5 and min diff=3, we can't add any positive while keeping all non-positives because any positive present is >=5, and condition fails. So answer = cnt_nonpos. If there existed a positive 3, that would be the smallest, and condition would hold (3>=3). So again smallest positive is the one that matters.
Thus the condition is min diff >= min_pos.
Now, is it possible that there are no non-positives but multiple positives? Then cnt=0, answer = 1 (since we can take one positive). That's cnt+1.
Now, confirm with more tricky cases: What about non-positive numbers that are not sorted? Already handled.
What about the case where we have zeros and negative numbers, and min_pos is 0? No, positive >0, so min_pos at least 1.
Thus solution seems simple.
But we must be careful about reading input and large t. Implementation straightforward.
Now, we should double-check the reasoning about "any sequence of length at most 1 is strange" - yes.
Now, let's ensure we didn't miss a scenario where a subsequence has max positive, but we could also include another positive if it is equal to the max? Actually if they are equal, then difference is 0 < max, so fails. So not.
Now, check: If all numbers are negative, answer = n.
If all numbers are zero, they are non-positive, answer = n. Since no positive, case2 not applicable. But what about adding a positive? There is none. So answer = n.
If there are zeros and negatives only, answer = n.
Now, confirm with a test: a = [-5, -5, 1] => cnt_nonpos=2, min_pos=1. Sorted nonpos: [-5,-5] diff = 0 <1 => fails. So answer = 2. But can we take subsequence [-5,1]? That's length 2. [-5, -5]? That's length 2, but is [-5, -5] strange? max = -5, condition: | -5 - (-5) | = 0 >= -5? 0 >= -5 true, so yes. So both length 2. So answer 2. Could we get 3? If we take both -5 and 1, need both -5's? That would be three elements: -5, -5, 1. Check: max=1. For the two -5's, | -5 - (-5) | = 0 < 1 => fails. So can't. So answer 2.
Now, what about a = [-5, -3, 2, 4] where min_pos=2. nonpos sorted: -5,-3 diff=2 >=2 ok. So answer = 2+1=3? Wait cnt_nonpos=2, so answer would be 3. Can we take -5,-3,2? Check: max=2; differences: -5,-3 diff=2>=2 ok; with 2: 2-(-5)=7>=2; 2-(-3)=5>=2 => yes length 3. Could we also include 4? No. So answer 3.
Now, a = [-5, -4, 2, 3] cnt_nonpos=2, min_pos=2, nonpos diff = 1 <2 => answer=2. But can we take -5,2? That's 2; -4,2? 2; -5, -4? 2. So 2. So ok.
Now, more complex: a = [-100, -50, -1, 0, 5, 6]. cnt_nonpos=4, min_pos=5? Actually positives: 5,6; min_pos=5. Sorted nonpos: -100,-50,-1,0. diffs: 50,49,1 => min diff=1 <5 => fails. So answer =4. Could we take subset of nonpos with 5 to get 5? If we take -100,-50,-1,5? Check nonpos pairwise: -100,-50 diff=50>=5; -100,-1 diff=99>=5; -50,-1 diff=49>=5. So those three nonpos are fine. That gives total 4. But we dropped 0. Could we take -100,-50,0,5? nonpos: -100,-50 diff=50; -100,0 diff=100; -50,0 diff=50 => all >=5, so we could take three nonpos +5 =4. Still 4. So no improvement.
Now, consider scenario where cnt_nonpos is large but min diff is small; but we might be able to take almost all nonpos and a positive, giving cnt_nonpos (if drop one) or cnt_nonpos (if drop more). So not exceeding.
Thus answer is either cnt or cnt+1.
Given this, the problem becomes trivial.
But wait, the examples included output 5 for test case 2 where cnt_nonpos=4 and answer=5, so +1. Others.
Now, let's think about the possibility of including two positives if one of them is not the maximum because we include a larger negative? That can't happen because positive > negative always.
Now, re-read problem statement: "absolute difference between any pair of its elements is greater than or equal to the maximum element in the sequence." They use MAX as the largest element. That's clear.
Now, we must confirm: Could the maximum be negative? Yes, if all elements are negative. Then condition is trivially satisfied. So all-negative subsequence of any length is fine.
Thus, solution stands.
Now, let's see if any hidden nuance: The array can have values -10^9 to 10^9. So we must use 64-bit ints.
Now, edge: n=1, array [5] => cnt_nonpos=0, pos exists => answer = 1. Works.
n=1, array [-5] => cnt_nonpos=1, no pos => answer=1.
n=1, array [0] => cnt_nonpos=1, no pos => answer=1.
Now, what about array with multiple zeros and a positive? For example: [0,0,1]. cnt_nonpos=2, min_pos=1, sorted nonpos: [0,0] diff=0 <1, so answer=2. Check: can we get length 2? Yes, both zeros (max=0) works, or [0,1] works? Check [0,1]: max=1, diff=1>=1 ok => length 2. So answer=2, which is cnt_nonpos. That's correct; can't get 3 because [0,0,1] fails due to two zeros.
Thus algorithm passes.
Now, we might consider if the greedy selection of all non-positives is always optimal for case2. Suppose min_pos is very small, say 1. Then if there are duplicates among non-positives, we can't take all. But maybe we could take all non-positives except duplicates, and add the positive, giving total = cnt_nonpos - d + 1. Since cnt_nonpos is the number of all non-positives, if we drop d duplicates, we get total = cnt_nonpos - d + 1. For this to exceed cnt_nonpos, we need d = 0. So indeed, only when there are no duplicates (and all gaps >= min_pos) we can get +1. But what if there is a duplicate but we could drop one duplicate and still have total = cnt_nonpos? That's not better.
Thus answer is as described.
Now, could there be scenario where we have multiple positive numbers and we might include a positive that is not the smallest, and by dropping some non-positives we get total > cnt_nonpos? For example, suppose non-positives: -10, -9, 0 (diff min=1). min_pos=2, so fails. But there is positive 2 and also positive 1? But if there is positive 1, min_pos would be 1, and condition would pass (min diff=1 >=1) so we would have +1. So min_pos matters.
But if min_pos is 2, condition fails. Could we use positive 2 and drop -9? Then we take -10,0,2: nonpos: -10,0 diff=10>=2 ok; total = 3. cnt_nonpos=3, so total=3. No improvement.
Thus fine.
So implement.
Now, let's consider if there might be a scenario where the longest strange subsequence does not include all non-positives, nor includes a positive, but rather a subset of non-positives that is larger than cnt_nonpos? Impossible.
Thus answer = cnt_nonpos + (1 if there is any positive and for all i < j among non-positives |a_i - a_j| >= min_positive).
But we must also consider: The positive we add must be from the array. If there is a positive, we can always add at least one positive? Not if condition fails; we might add one positive alone, but that gives length 1. But if cnt_nonpos >=1, then 1 is not better. So we only consider adding if it yields longer than cnt_nonpos.
Thus answer = max(cnt_nonpos, 1 if pos else 0, maybe also 1 + something). But we derived answer = cnt_nonpos + (extra condition).
But wait, is it possible that cnt_nonpos = 0 and condition vacuously true? Then answer = 0+1 =1. Good.
Now, what about scenario where cnt_nonpos=0 and there are many positives, but we might get length >1 by including multiple positives? No.
Thus final answer as above.
Now, we need to ensure we handle large t and sum n. Complexity O(n log n) per test case.
Potential pitfalls: Sorting only non-positives; that list may be up to n. Using Python, okay.
Now, we also need to handle that we might have to consider negative numbers and zero; but we used condition x <= 0.
Now, let's think about an alternative: Could there be a strange subsequence where the maximum is negative, but we include positive numbers? If maximum is negative, then all elements are <= that negative? No, if there is a positive, the maximum would be positive, not negative. So impossible.
Thus fine.
Now, confirm with some random test using brute force for small n to ensure formula holds. Since I can't run code now, but mental check: Let's generate a small array and manually enumerate.
Case: a = [-2, -1, 2]. cnt_nonpos=2, min_pos=2, nonpos sorted: [-2,-1] diff=1 <2 => answer=2. Check all subsets:
- All three: max=2, need | -2 - (-1) | =1 <2 => no.
- [-2,2]: max=2, diff=4 >=2 => ok length2.
- [-1,2]: diff=3 >=2 => length2.
- [-2,-1]: max=-1, diff=1 >= -1 (1 >= -1) => length2.
- single elements: all length1. So max length =2. Good.
Case: a = [-2, -1, 1]. cnt_nonpos=2, min_pos=1, nonpos diff=1 >=1 => answer=3. Check all three: max=1, diffs: |-2-(-1)|=1>=1 ok; |-2-1|=3>=1; |-1-1|=2>=1 => length3. So 3.
Case: a = [-5, -3, -2, 1]. cnt_nonpos=3, min_pos=1, nonpos sorted: -5,-3,-2 diffs: 2,1 => min diff=1 >=1 => answer=4? That would be 3+1=4. Check all four: max=1, pairwise: |-5-(-3)|=2>=1; |-5-(-2)|=3>=1; |-3-(-2)|=1>=1; with 1: 6,4,3 all >=1. So length 4 works. Good.
Case: a = [-5, -4, -3, 2]. cnt_nonpos=3, min_pos=2, nonpos sorted: -5,-4,-3 diffs: 1,1 min=1 <2 => answer=3. Check all four? fails because -4,-3 diff=1<2. Check subsets: maybe -5,-3,2: works length3; -5,-4,2 fails; -5,-3,2 length3; all three negatives max=-3? Actually all three negatives: max=-3, condition trivial, length3. So max=3.
Case: a = [0, 0, 1]. cnt_nonpos=2, min_pos=1, nonpos sorted: 0,0 diff=0<1 => answer=2. Check all three? fails because two zeros diff 0<1. Subsets: two zeros length2 (max=0) works; 0 and 1 length2; both zeros and 1 fails. So max=2.
Case: a = [-1, 0, 0, 2]. cnt_nonpos=3, min_pos=2, nonpos sorted: -1,0,0 diffs: 1,0 => min=0<2 => answer=3. Check all four? fails; maybe -1,0,2? nonpos: -1,0 diff=1<2 fails; -1,0,0? with max=0 works length3; 0,0,2 fails; -1,2? length2; 0,2? length2; etc. So max=3.
Case: a = [-10, -5, 0, 3, 4]. cnt_nonpos=3, min_pos=3, nonpos sorted: -10,-5,0 diffs: 5,5 min=5 >=3 => answer=4? That would be 3+1=4. Check -10,-5,0,3: max=3, diffs: |-10-(-5)|=5>=3; |-10-0|=10>=3; |-5-0|=5>=3; with 3: 13,8,3>=3 => length4 works. Could we include 4? would become max=4, need diff >=4; |-10-(-5)|=5>=4; |-10-0|=10; |-5-0|=5; with 4: 14,9,4? |0-4|=4>=4 ok; but with 3: |3-4|=1<4 fails. So not all.
Thus answer 4, matches.
Case: a = [ -100, -1, 0, 2, 3, 5 ]. cnt_nonpos=3, min_pos=2? Actually positives: 2,3,5 min_pos=2. nonpos: -100,-1,0 diffs: 99,1 min=1 <2 => answer=3. Could we pick -100,0,2? nonpos diff: 100>=2 ok => length3; -100,-1,2? diff 99>=2 => length3; -1,0,2? diff 1<2 fails; So max 3. But what about -100,2,5? That's two positives, not allowed. So 3.
Thus formula works.
Now, consider case where there are no non-positives but multiple positives: answer=1.
Now, all good.
But wait: There is an example in the problem: (3,0,1) is not strange because |0-1|<3. Indeed max=3, diff 0 and 1 =1 <3. So fails. Our reasoning: If we include 3 as positive (the largest), we cannot include 1 because they are both positive? Actually 3 and 1 are both positive, but we said you can't have two positives. So (3,0,1) has two positives 3 and 1, fails as expected.
Now, (-2021,-1,-1,-1) is strange because max=-1, condition trivial. So okay.
Now, finalize solution.
But is there any scenario where the longest strange subsequence includes a positive and also a number greater than that positive? That would be a larger positive, making max larger, which would then violate with the smaller positive. So no.
Thus solution is trivial: answer = count of non-positive elements + (1 if there exists a positive and the smallest positive is ≤ the minimum gap among non-positives? Actually we need min_gap >= min_pos. So check if min_gap >= min_pos.
But careful: The "minimum gap" among non-positives is the minimum absolute difference between any two distinct non-positives. Since we require all pairs to have diff >= min_pos, it's equivalent to min_gap >= min_pos. So compute min_gap after sorting.
Implementation steps:
For each test case:
- Read n.
- Read list a.
- Initialize cnt_nonpos = 0; list_nonpos = [].
- min_pos = INF (large).
- For each x in a:
if x <= 0: cnt_nonpos += 1; append x to list_nonpos.
else: if x < min_pos: min_pos = x.
- If min_pos == INF (no positive): print(cnt_nonpos) and continue.
- If cnt_nonpos == 0: print(1) and continue. (Because we can pick one positive; but actually if there are positives, we can always pick at least one; so answer = 1. This is also captured by condition below: sorted nonpos empty, we treat as vacuously true, answer = 0+1=1.)
- else:
sort list_nonpos.
ok = True
for i in range(1, len(list_nonpos)):
if list_nonpos[i] - list_nonpos[i-1] < min_pos:
ok = False
break
if ok:
print(cnt_nonpos + 1)
else:
print(cnt_nonpos)
But is it enough to check only consecutive differences? Yes, because if all consecutive diffs >= min_pos, then any pair diff is sum of consecutive diffs, which is >= min_pos.
Edge case: duplicates: consecutive diff =0 < min_pos, catches.
Now, consider if there is only one non-positive element: then there are no pairs, so condition vacuously true. So we can add the positive. So answer = 1+1=2 if cnt_nonpos=1 and min_pos exists? Check example: a = [-5, 2]. cnt_nonpos=1, min_pos=2, nonpos only one, so condition vacuously true, answer = 2? Let's test: subsequence [-5,2] max=2, diff=7>=2 => yes length2. So answer 2. Good.
Now, consider if min_pos is negative? No, positive.
Now, what about zero and positive? a = [0, 1] => cnt_nonpos=1, min_pos=1, condition vacuously true => answer=2. Check: [0,1] max=1, diff=1>=1 => yes. So length2.
Now, a = [0,0,1] => cnt_nonpos=2, min_pos=1, list_nonpos sorted [0,0], consecutive diff=0<1 => answer=2. That's correct.
Thus fine.
Now, we must ensure we handle the case where min_pos is 0? No, positive.
Now, what about when there are positive numbers but we might not want to add the smallest positive because perhaps the smallest positive itself is not in the subsequence? But we can always choose to add it. If it exists, we can include it. So it's fine.
But is there a scenario where we cannot include the smallest positive because it's part of the array but we need to ensure the non-positives are spaced accordingly? That is what we check.
Thus solution is correct.
Now, let's test with random small cases to be absolutely sure:
Case: n=2, a=[-2, -1]. cnt_nonpos=2, no positive => answer=2. Works.
Case: n=2, a=[-2, 3]. cnt_nonpos=1, min_pos=3, condition vacuously true => answer=2. Works.
Case: n=3, a=[-5, -4, 2]. cnt_nonpos=2, min_pos=2, nonpos sorted: -5,-4 diff=1<2 => answer=2. Could there be a longer subsequence? 2 elements only. So ok.
Case: n=3, a=[-5, -3, 2]. cnt_nonpos=2, min_pos=2, nonpos diff=2 >=2 => answer=3. Yes.
Case: n=4, a=[-10, -8, -6, 1]. cnt_nonpos=3, min_pos=1, diffs: 2,2 => >=1 => answer=4. Good.
Case: n=5, a=[-7,-5,-3,-1,2]. cnt_nonpos=4, min_pos=2, diffs: 2,2,2 => min=2>=2 => answer=5? Actually cnt_nonpos=4, answer would be 5. Check: all four negatives and 2: max=2, pairwise negatives diffs: between consecutive: 2,2,2 all >=2, so yes length5. Good.
Now, scenario: a includes both positive and negative, but the negative numbers are not all spaced enough, but there might be a positive larger than min_pos that could be included with all negatives? No, if min_pos fails, any larger positive requires larger gap, so fails even more.
Thus solution stands.
Now, let's think about potential pitfalls: The condition for case2 is that we can take all non-positives. But what if there are non-positives that are not in sorted order? Already handled.
But is it possible that there is a positive that is not the smallest, but we could drop some non-positives and still achieve cnt_nonpos+1 by including two positives? No.
Thus final answer.
Now, we can implement.
Now, I want to double-check the logic about duplicates among non-positives and zeros. If there are multiple zeros, they are duplicates with diff 0. So condition fails. So answer = cnt_nonpos. That matches.
Now, check the sample 5: [-3,0,2,0] -> zeros duplicate? Actually there are two zeros, diff=0 < min_pos=2 => answer=3. Good.
Now, what if there are duplicate negatives, like -5, -5? Then same.
Thus fine.
Now, one more nuance: Could there be a strange subsequence that includes a positive and all non-positives except one duplicate, resulting in total = cnt_nonpos (if we drop the duplicate) but that equals cnt_nonpos, not better. So answer remains cnt_nonpos.
Thus we are done.
Now, complexity: O(n log n) per test case, sum n <= 1e5, so fine.
Now, we should produce code.
But I recall that Codeforces problem "Strange Subsequence" perhaps? I think it's from Codeforces Round #... I've seen similar. Indeed, I recall a problem: "Strange Subsequence" from Codeforces 1472B? Not sure. Actually I think there is a problem "Strange Subsequence" in Codeforces Round #1385? Let me think: Actually there is a problem "Strange Subsequence" in Codeforces Round #1500? I'm not sure. But I've seen editorial: The answer is either number of non-positives or that+1 depending on if the smallest positive is less than or equal to the minimum difference among non-positives. Something like that.
Thus final answer.
Now, let's write solution explanation accordingly.
But before finalizing, I want to think: The condition we used for case2: all non-positives must have pairwise diff >= min_pos. But is it necessary to check all pairs? Yes. But we used consecutive differences. That's correct.
Now, what about the absolute difference? Since non-positives are sorted ascending, difference is non-negative; it's the absolute difference because larger minus smaller. So fine.
Now, think about edge case with non-positives including very large negative numbers: The difference between -10^9 and -10^9+1 is 1; that's fine.
Now, potential integer overflow? Not in Python.
Now, implementation details:
For each test case:
- read n
- read list a
- neg = []
- min_pos = 10**10 (or None)
- for x in a:
if x <= 0:
neg.append(x)
else:
if min_pos is None or x < min_pos:
min_pos = x
- cnt = len(neg)
- if min_pos is None:
print(cnt)
continue
- if cnt == 0:
print(1)
continue
- neg.sort()
- ok = True
- for i in range(1, cnt):
if neg[i] - neg[i-1] < min_pos:
ok = False
break
- if ok:
print(cnt+1)
- else:
print(cnt)
That's it.
But we need to be careful: The condition for adding a positive is that we can take all non-positives and at least one positive. However, what if there are multiple positives and the smallest positive is min_pos, but that smallest positive might be less than the minimum gap among non-positives, but the non-positives themselves may have min gap >= min_pos, but there could be an issue: The positive we add must be an element from the array; we are adding the smallest positive, but is it guaranteed that the smallest positive is not causing a problem with some non-positive? As argued, any non-positive x, |x - min_pos| = min_pos - x (since min_pos>0, x<=0) = min_pos + |x| >= min_pos. So condition holds. So no problem.
Now, edge case: Suppose there is a positive number that is exactly the same as the smallest positive, but we need to ensure it's not used twice? We just need one.
Thus fine.
Now, we can also consider that there could be positive numbers that are zero? No.
Now, just to be extra safe, let's test with a case where non-positives have min gap = 5, min_pos = 5, but there is also a positive 3? Actually if min_pos is 5, that means the smallest positive is 5, so there is no positive 3. So okay.
What if there is a positive 5 and also positive 3? Then min_pos would be 3, not 5. So min_pos would be 3. Then condition would be min_gap >= 3, which is true if min_gap=5. So we could add positive 3. But could we also add positive 5? Possibly, but that would not increase length beyond adding 3; we can only add one positive anyway. So 3 works.
Thus correct.
Now, final answer.
Now, we need to output answer for each test case.
One more check: If there are non-positives, but no positive, answer = cnt_nonpos. If there are positives but no non-positives, answer = 1.
All good.
Now, we should also consider if the answer could be 0? n>=1, so at least one element. If that element is positive, answer at least 1. If negative, answer at least 1. So answer >=1.
Now, we're ready.
But wait: There's a nuance: The definition says "any sequence of length at most 1 is strange". So empty sequence is also strange. But we want the longest subsequence, so we can always take at least one element. So answer >=1.
Now, confirm that our solution returns at least 1. For n=1, if element >0, cnt_nonpos=0, min_pos exists, cnt==0 case prints 1. If element <=0, cnt_nonpos=1, min_pos None, prints 1. So good.
Now, we should also consider memory: store list of non-positives up to n.
Now, let's also think about possibility of a subsequence that contains only positive numbers but with max <=0? impossible.
Thus final solution.
Now, we need to produce final answer with code.
I'll write in Python.
Implementation plan:
def solve():
import sys
input = sys.stdin.readline
t = int(input().strip())
for _ in range(t):
n = int(input().strip())
a = list(map(int, input().split()))
neg = []
min_pos = None
for x in a:
if x <= 0:
neg.append(x)
else:
if min_pos is None or x < min_pos:
min_pos = x
cnt = len(neg)
if min_pos is None:
print(cnt)
continue
if cnt == 0:
print(1)
continue
neg.sort()
ok = True
for i in range(1, cnt):
if neg[i] - neg[i-1] < min_pos:
ok = False
break
if ok:
print(cnt + 1)
else:
print(cnt)
Now, we should test on given examples.
We'll simulate:
Input:
6
4
-1 -2 0 0
7
-3 4 -2 0 -4 6 1
5
0 5 -3 2 -5
3
2 3 1
4
-3 0 2 0
6
-3 -2 -1 1 1 1
Case1: neg=[-1,-2,0,0] cnt=4, min_pos=None => print 4.
Case2: neg=[-3,-2,0,-4] cnt=4, min_pos=1 (since positives 4,6,1 min=1). sort neg -> [-4,-3,-2,0]. Check diffs: 1,1,2 all >=1 => ok => print 5.
Case3: neg=[0,-3,-5] cnt=3, min_pos=2 (positives 5,2 min=2). sort -> [-5,-3,0] diffs: 2,3 >=2 => ok => print 4.
Case4: neg=[] cnt=0, min_pos=1 => print 1.
Case5: neg=[-3,0,0] cnt=3, min_pos=2. sort->[-3,0,0] diffs: 3,0 -> 0<2 => not ok => print 3.
Case6: neg=[-3,-2,-1] cnt=3, min_pos=1. sort->[-3,-2,-1] diffs:1,1>=1 => ok => print 4.
Matches expected.
Now, edge test: n=1, a=[5] => neg=[], cnt=0, min_pos=5 => print 1. Good.
n=1, a=[-5] => neg=[-5], min_pos=None => print 1.
Now, confirm that we didn't miss the scenario where there are zeros and a positive, and zeros are duplicate, but maybe we can take all non-positives except one zero, and the positive, resulting in cnt_nonpos? That would be equal to cnt_nonpos if we drop one zero? For cnt_nonpos=3 with two zeros, we could take one zero, the other negative, and the positive, total=3, which equals cnt_nonpos. So not larger.
Thus fine.
Now, one more nuance: The condition for case2 requires that the positive we add is the maximum. But if we add the smallest positive, it's the maximum because all non-positives are <=0 and that positive >0. So it's the maximum.
Thus final.
Now, we can also note that we don't need to compute maximum subset size via greedy; just check if all non-positives can be taken together with some positive. That's O(n log n). Good.
Now, we should produce final answer.
But before finalizing, I want to consider if there is any scenario where we could achieve length > cnt_nonpos+1. Suppose we have non-positives that are all spaced enough, and we add a positive, that's cnt_nonpos+1. Could we also add another element that is non-positive but not originally in the array? No. Could we add a second positive? No. So max is cnt_nonpos+1.
But is it possible that we could have a strange subsequence that consists only of non-positives but we could have more than cnt_nonpos? That's impossible because cnt_nonpos is total count of non-positives in array.
Thus max is cnt_nonpos+1.
Thus answer is bounded.
Now, we are done.
But wait: The problem statement says "strange if for every pair (i, j) with 1 ≤ i<j ≤ k, we have |a_i-a_j|≥ MAX, where MAX is the largest element of the sequence." There's no absolute of MAX, it's the largest element. So if the largest element is negative, condition is trivial. That we used.
Thus correct.
Now, we could also think about the possibility of a subsequence with max positive, but we might include a positive that is not the maximum? No, the maximum is the largest, so any positive we include will be the maximum if it's the only positive. But if we include a positive that is not the maximum, that would require a larger positive also present. So can't.
Thus fine.
Now, just to be absolutely thorough, let's consider a case where non-positives are like [-100, -99, -98] and min_pos=2. min diff=1 <2, so answer=3 (all non-positives). But could we get length 4 by selecting two positives? No.
Now, consider a case where there are both positive and negative numbers, but the non-positives are far apart, but the smallest positive is large, say 100, and non-positives diff min=200 >=100, then answer=cnt_nonpos+1. That works.
Now, all good.
Thus solution is straightforward.
Now, let's write final answer.
But I recall there is a Codeforces problem "1472B: Fair Division" not this. Another: "1385C? No. I've seen problem "1500: Strange Subsequence" maybe from Codeforces Round #684? Actually there is a problem "Strange Subsequence" from Codeforces Round #1476? I'm not sure. But the solution is as described.
Now, I think it's correct.
Now, we must output answer for each test case.
Implementation details: Use sys.stdin.read for faster input? But sum n 1e5, so okay.
Now, final code.
Now, I'll provide explanation in final answer. ```python
import sys
def solve() -> None:
it = iter(sys.stdin.read().strip().split())
t = int(next(it))
out = []
for _ in range(t):
n = int(next(it))
neg = []
min_pos = None
for _ in range(n):
x = int(next(it))
if x <= 0:
neg.append(x)
else:
if min_pos is None or x < min_pos:
min_pos = x
cnt = len(neg)
# no positive numbers -> take all non‑positive
if min_pos is None:
out.append(str(cnt))
continue
# no non‑positive numbers -> take any one positive
if cnt == 0:
out.append("1")
continue
# check whether we can take all non‑positives together with a positive
neg.sort()
ok = True
for i in range(1, cnt):
if neg[i] - neg[i-1] < min_pos:
ok = False
break
if ok:
out.append(str(cnt + 1))
else:
out.append(str(cnt))
sys.stdout.write("\n".join(out))
if name == "main":
solve()