Skip to content
TheChatArchiveThe Chat Archive
DeepSeekDeepseek v3.2· deepseekHugging Face Frontier Reasoning Traces

Let's call an array t dominated by value v in the next situation. At first, array t…

  1. User

    Let's call an array t dominated by value v in the next situation.

    At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not.

    Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not.

    You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays.

    The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 ≤ i ≤ j ≤ n. Input

    The first line contains single integer T (1 ≤ T ≤ 1000) — the number of test cases. Each test case consists of two lines.

    The first line contains single integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array a.

    The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — the corresponding values of the array a.

    It's guaranteed that the total length of all arrays in one test doesn't exceed 2 ⋅ 10^5.

    Output

    Print T integers — one per test case. For each test case print the only integer — the length of the shortest dominated subarray, or -1 if there are no such subarrays.

    Example

    Input

    4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3

    Output

    -1 6 3 2

    Note

    In the first test case, there are no subarrays of length at least 2, so the answer is -1.

    In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray.

    In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray.

    In the fourth test case, all subarrays of length more than one are dominated.

  2. DeepSeek Assistant

    We need to solve the problem: Given an array a of length n (1 ≤ n ≤ 2e5 per test, total n across tests ≤ 2e5), find the length of the shortest dominated subarray (contiguous) with at least 2 elements, dominated means there exists a value v such that its count in the subarray is strictly greater than the count of any other value (i.e., v is a strict majority, not just tie). If no such subarray exists, output -1.

    We need to process up to T test cases quickly.

    Observations:

    • If any two consecutive elements are equal, then subarray of length 2 is dominated? Let's check: [x, x] has occ(x)=2, any other number has at most 0, so yes dominated. Actually, check: condition requires at least 2 elements, and occ(v) > occ(v') for any other v'. For length 2 with both same, it's dominated (by that value). So if there exists any i such that a[i] = a[i+1], then answer is 2 (since length 2 dominated subarray exists). Also length 2 with different numbers: [1,2] -> occ(1)=1, occ(2)=1, no v has strictly greater count, so not dominated. So the minimal possible length is 2 if there is any adjacent equal pair.

    • Could there be a dominated subarray of length 2 with different numbers? No, because both have count 1, so no strict majority. So length 2 only works if equal.

    • Length 3: possibilities: [x,x,y] -> occ(x)=2, occ(y)=1, so dominated by x. Similarly [x,y,y] dominated by y. [x,y,z] all distinct: counts all 1, no majority. So length 3 dominated subarray exists if there is any subarray of length 3 where the majority element appears twice (and the third is different). That means either a[i]=a[i+1] (already length 2) or a[i]=a[i+2] (i.e., pattern x y x) yields length 3 dominated? Let's check: [x,y,x] -> occ(x)=2, occ(y)=1, dominated. So if we have two equal elements with exactly one index gap (i.e., a[i]=a[i+2]), that subarray of length 3 is dominated. Also a[i+1]=a[i+2]? That's adjacent, length 2. So basically we need to check if there exists any i such that a[i] = a[i+2] (adjacent gap 2). Also maybe a[i]=a[i+1]=a[i+2]? That's also dominated, but already covered.

    Thus, the answer is:

    • If any adjacent equal (a[i]==a[i+1]), answer = 2.
    • Else if any "gap 2 equal" (a[i]==a[i+2]), answer = 3.
    • Else answer = -1? But wait, maybe there exist longer dominated subarrays even without these patterns? Possibly, but we need the shortest. So if no length 2 or 3 dominated subarray exists, could there be length 4 or more? Let's test: suppose array with all distinct values? [1,2,3,4] length 4: all distinct => no majority. What about pattern like [1,1,2,3]? Actually that has adjacent equals -> length2 exists. So if no adjacent equals and no gap2 equals, does that guarantee no dominated subarray? Let's think: A dominated subarray requires some element to appear more than half of the subarray length. For a subarray of length L, dominated by v implies occ(v) > L/2. So v must appear at least floor(L/2)+1 times. This is the classic majority element condition.

    Now suppose we have an array with no two equal elements within distance 2 (i.e., any two equal elements are at least distance 3 apart). In such an array, can there be a subarray where an element appears more than half? For a subarray of length L, the maximum frequency of any element is limited by how close repeats can be. If any two equal elements are at least distance 3 apart, then in any contiguous segment, the number of occurrences of a particular value is at most ceil(L/3)? Let's derive more precisely. Consider the positions of a particular value x: p1 < p2 < p3 < ... They are spaced at least 3 apart (p_{k+1} - p_k >= 3). In a subarray of length L (contiguous interval), the maximum number of these positions that can lie inside is roughly floor((L-1)/3)+1? Actually if they are at least 3 apart, then the minimal index difference between the first and last occurrences in a segment of length L is at least 3*(k-1). So for k occurrences to fit within an interval of length ≤ L-1 (distance from first to last), we need 3*(k-1) ≤ (L-1). So k ≤ (L-1)/3 + 1 = (L+2)/3. For dominated subarray we need k > L/2. So we require (L+2)/3 > L/2 => 2(L+2) > 3L => 2L+4 > 3L => 4 > L => L < 4. So L ≤ 3. So for L=3, condition gives (3+2)/3 = 5/3 ≈1.66, so k can be at most 1? Actually with spacing ≥3, if L=3, you can have at most one occurrence of a specific value? Let's check: if two occurrences are at least 3 apart, then if they appear in a length-3 window, the distance between them would be at most 2, impossible. So indeed for L=3, k ≤1. So cannot have k > 1.5 => k≥2 would be needed for domination (since need k > 1.5, so k ≥2). But with spacing ≥3, you cannot have two occurrences in any length-3 window. So dominated subarray of length 3 also impossible. For L=2, need k>1 => k≥2, but spacing ≥3 means can't have two occurrences within distance 1? Actually two equal adjacent would be distance 1, which violates spacing ≥3. So cannot. So if no adjacent equals (distance 1) and no gap 2 equals (distance 2), then any two equal elements are at least distance 3 apart. Then as argued, dominated subarray cannot exist for any length because the required frequency condition cannot be met. But is that rigorous for all L? Let's prove:

    Given an array where for any i, j with a[i]=a[j] and i<j, we have j-i ≥ 3? Actually condition "no adjacent equal and no gap 2 equal" means: For any i, a[i] ≠ a[i+1] and a[i] ≠ a[i+2]. However, could there be equal elements with gap larger than 2? Yes, e.g., indices i and i+3 could be equal: a[i] = a[i+3] is allowed. So it's not that all equal elements are spaced at least 3 apart? Actually if a[i] = a[i+3], then the distance is 3, which is allowed (≥3). So indeed the minimum distance between equal elements is at least 3 (since distance 1 and 2 are forbidden). So spacing at least 3.

    Now consider any subarray of length L. Let v be any value. The positions of v inside the subarray are some indices with pairwise differences at least 3. If there are k such positions, then the distance from the first to the last is at least 3*(k-1). Since the subarray length L covers this span plus possibly extra on ends, but the minimal span that contains them is at most L-1 (if the subarray exactly from first to last). Actually if the first occurrence is at position p_start within the subarray and last at p_end, then p_end - p_start ≥ 3*(k-1). And since both are within the subarray of length L, we have p_end - p_start ≤ L-1 (because the subarray indices are inclusive, and the length L = j-i+1, the distance between first and last indices inside it is at most L-1). So 3*(k-1) ≤ L-1 => k ≤ (L-1)/3 + 1 = (L+2)/3.

    Now for dominated subarray we need k > L/2. So we need (L+2)/3 > L/2 => cross multiply: 2(L+2) > 3L => 2L+4 > 3L => L < 4. So L ≤ 3. For L=3: we need k > 1.5 => k ≥2. But from inequality, k ≤ (3+2)/3 = 5/3 ≈ 1.66, so k ≤1. So impossible. For L=2: need k >1 => k ≥2, but k ≤ (2+2)/3=4/3≈1.33 => k ≤1. So impossible. So indeed no dominated subarray exists. So answer -1.

    Thus, the problem reduces to: find if there exists i with a[i]==a[i+1] (answer 2) or else if exists i with a[i]==a[i+2] (answer 3) else answer -1. That's it.

    But we must be careful: Is it always true that any dominated subarray of length >3 would imply existence of either length 2 or 3 dominated subarray? Not necessarily, but our analysis shows that if there is no adjacent equal and no gap-2 equal, then there cannot be any dominated subarray at all. So if a dominated subarray exists, then either there is one of length 2 or 3 (or maybe length 1? But not allowed). So answer is either 2, 3, or -1. However, there is a nuance: Could there be a dominated subarray of length 4 that is the shortest, even though no length 2 or 3 dominated subarray exists? According to our derived condition, if no length 2 or 3 dominated, that means no adjacent equal and no gap-2 equal, which implies spacing ≥3, then no dominated subarray of any length exists. So no length 4. So indeed the answer is determined by these local patterns.

    But wait: Could there be a case where there is adjacent equal but answer is not 2 because we have to output the shortest dominated subarray? Obviously 2 is shortest possible, so if adjacent equal exists answer is 2. If no adjacent equal but there is gap2 equal, answer is 3. If neither, answer -1. So algorithm is O(n) per test.

    But we need to be absolutely sure that there is no dominated subarray of length >3 without these local patterns. Let's double-check with examples:

    Example: array [1,2,1,2] -> adjacent equal? No. gap2? Check: a[1]=1, a[3]=1 => yes! So length 3 dominated subarray from indices 1-3: [1,2,1] dominated by 1. So answer would be 3. Indeed there might also be length 4: [1,2,1,2] -> counts: 1 appears twice, 2 appears twice, not dominated. So 3 is correct.

    Another: [1,2,3,1] -> adjacent? no. gap2? check indices: a1=1, a3=3? no; a2=2, a4=1? no; so no gap2. Also check a1 and a4 distance 3? equal? a1=1, a4=1 => yes, gap 3. So according to our condition, there is no length 2 or 3 dominated. Check any length 4 subarray: the whole array: counts 1 appears twice, others once each, not dominated because 1's count=2, others? Actually need > for any other: For 1, occ(1)=2, occ(2)=1, occ(3)=1 -> 2 > 1, so condition holds? Wait, condition: occ(v) > occ(v') for any other number v'. So we need to check if there exists v such that its count strictly greater than every other individual count. For array [1,2,3,1], occ(1)=2, occ(2)=1, occ(3)=1. So occ(1) > occ(2) (2>1) and occ(1) > occ(3) (2>1). So indeed v=1 is dominated! But we said no adjacent equal, no gap2 equal, yet we have dominated subarray of length 4. Let's re-evaluate: Our earlier reasoning that spacing ≥3 prevents domination may be flawed. Because the spacing condition for a specific value: positions of 1 are at indices 1 and 4. Their distance is 3, which is allowed if we only forbid distance 1 and 2. So spacing at least 3. For L=4 subarray [1,2,3,1], we have two occurrences of 1, spaced 3 apart. The inequality we derived: For value 1, k=2, L=4. Check condition: 3*(k-1)=3 <= L-1=3, so equality holds, so k=2 satisfies k ≤ (L+2)/3 = (4+2)/3=2, so indeed k can be exactly 2. So k > L/2? L/2=2, we need >2, but k=2 is not >2. So need k > L/2, i.e., strict majority, so k must be at least 3 when L=4. Actually for L=4, strict majority means occ(v) > 2, so at least 3. So indeed [1,2,3,1] is not dominated because occ(1)=2 is not >2. It's not > occ(2)=1? Wait, condition is occ(v) > occ(v') for any other v'. Not that occ(v) > L/2. But does occ(v) > each other individually imply occ(v) > L/2? Let's check: Suppose L=4, occ(v)=2, occ(other1)=1, occ(other2)=1. Then occ(v) > occ(other1) yes, > occ(other2) yes. So condition is satisfied? The definition: "occ(v) > occ(v') for any other number v'." That means for every other v', the count of v is strictly larger. That does not require v to have > half; it only requires v's count to be strictly greater than each other individual count. In this example, 2 > 1, so condition holds. However, is that the definition of dominated? The problem statement says: "let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'." So indeed it's not "strict majority" in the sense of > half, but simply that v's count is strictly larger than the count of any other element. That means it's enough to be the unique maximum frequency, not necessarily majority. For example, array [1,2,3,1] has frequencies: 1:2, 2:1, 3:1. 1 is the unique maximum, so it is dominated. So my earlier assumption that dominated requires > L/2 was incorrect! That's crucial.

    Thus our simple pattern of length 2/3 might not cover all cases, because we might have a dominated subarray of length 4 or longer that is the shortest, even when there is no adjacent equal or gap2 equal. We need to re-analyze.

    Let's read problem statement carefully: "t is dominated (by v) if occ(v) > occ(v') for any other number v'." So it's "strictly greater than each other", not necessarily > half. So we need to find the shortest subarray where some value appears strictly more times than any other value. The subarray must have at least 2 elements.

    Now we need to compute the length of the shortest such subarray.

    We can attempt to solve the problem more generally.

    Observation: The condition is that the maximum frequency in the subarray is unique (strictly greater than any other frequency). So the subarray is dominated if the most frequent element appears more times than the second most frequent element. Equivalent: The subarray's frequency distribution has a unique mode with strictly highest count.

    Goal: Shortest such subarray length.

    We can think of necessary conditions: For a subarray to be dominated, it cannot have all frequencies equal. It must have at least one element appearing at least twice? Not necessarily: consider subarray of length 2 with both distinct: frequencies both 1, not unique max (tie). So at least one element must appear at least 2 times, and its count must exceed all others, so it must be at least 2 and others at most 1. That suggests that in the shortest dominated subarray, the dominating element appears exactly twice, and all other elements appear at most once. For length 2: both same -> count=2,0 others -> dominated. For length 3: patterns: two same and one different (2,1) -> dominated; three same (3,0,0?) Actually three same: count=3, others 0 -> dominated; but that includes adjacent equals so length2 exists? If three same, then there is adjacent equal so length2 already exists. So shortest will be 2 if any equal adjacent. So the interesting case is when there is no adjacent equal. Could there be a dominated subarray of length 4 with no adjacent equal? Example: [1,2,1,2] frequencies: 1:2, 2:2 -> tie, not dominated. [1,2,3,1] frequencies: 1:2, 2:1, 3:1 -> dominated as we saw. Does it have adjacent equal? No. Gap2 equal? Check a[1] and a[3]: 1 vs 3? not equal. a[2] and a[4]: 2 vs 1? not equal. So no gap2 equal. Yet length 4 dominated exists. So answer could be 4. But is there maybe a shorter one? Could there be a length 3 dominated without gap2 equal? Length 3 dominated requires two same and one different (or three same). Three same implies adjacent equal. Two same but not adjacent: that means positions i and i+2 equal, i.e., gap2 equal. So indeed length 3 dominated requires either adjacent equal (i,i+1) which would give length2 dominated anyway, or gap2 equal (i,i+2) which gives length3. So if no adjacent equal and no gap2 equal, then length 2 and 3 dominated cannot exist. But length 4 might exist as shown. So we need to handle lengths >=4.

    Generalizing: A dominated subarray of length L must contain some element that appears at least twice (since with all distinct frequencies are all 1, tie). Actually if L=2, all distinct gives tie. So at least one element appears at least twice. And that element's count must be strictly greater than the count of any other element.

    Thus the minimal L where such a condition can be satisfied without length 2/3 patterns might be L=4, with a pattern like x y x z? But need that the count of x is > count of any other. Let's test [1,2,1,3]? Frequencies: 1:2, 2:1, 3:1 -> dominated. So yes. Similarly [1,2,3,1] works. So any subarray where some element appears exactly twice, and all others appear at most 1, and there are at least two other distinct elements? Actually if it appears twice and there is only one other distinct element (like [x,y,x]? That's length 3, but that's gap2 equal; or [x,x,y]? that's length3 with adjacent equal). So for length 4 without adjacent/gap2, we need exactly one element appearing twice, and two other distinct elements each once, giving counts (2,1,1). That's dominated. Also possibility of one element appearing 3 times and others totaling 1? But that would require adjacent or gap2? Example length 4: [x,x,x,y] -> adjacent equal, so length2 exists. So shortest would be 2. So in case no length 2, we might consider length 4 with pattern (2,1,1). Also length 5 could be (2,2,1)? That's tie for max, not dominated. (3,1,1) -> dominated? But 3,1,1: max 3 > 1, but can we have 3 occurrences without adjacent equal? Possibly: positions like 1,3,5 (gap2) but that would require gap2 equal, which we assumed not present. Actually if no adjacent equal and no gap2 equal, then the minimum distance between equal elements is at least 3. Could we have three occurrences within a length 5 subarray without violating spacing? Suppose positions at i, i+3, i+5? That's not contiguous subarray; we need them within a window of length 5. If equal elements are at indices p, p+3, p+5? That's distance 3 and 2? Wait p+3 - p = 3 (allowed if >=3) and p+5 - p+3 = 2 (not allowed because gap 2 would be equal? But if the values are equal, having two occurrences with difference 2 would be a gap2 equal pattern within the array, which is not allowed. So if no gap2 equal anywhere, then you cannot have two equal elements with indices differing by 2. So the minimum spacing is at least 3, meaning if you have two equal elements, the distance >=3. For three equal elements, the distances between consecutive ones must each be at least 3. So the span from first to last is at least 3*(k-1). For k=3, span >=6. So to fit in a subarray of length L, we need L-1 >= 6 => L >=7. So for L=5 or 6, you cannot have 3 occurrences of the same element if spacing >=3. So the only possible dominated subarray with spacing >=3 (i.e., no adj or gap2) would have the dominating element appearing exactly twice, and the rest distinct. And total length L must be such that counts of other elements are less than 2, so they can be at most 1 each. So the subarray contains exactly k=2 occurrences of the dominating value, and (L-2) other distinct values (each distinct from each other and from the dominating value, but also they may repeat among themselves? If they repeat, then they'd have count >=2 as well, causing tie or greater than dominating? For domination, the dominating count must be strictly greater than each other. So if another value appears twice, then occ(dom) must be >2, so at least 3. But as argued, with spacing >=3, you can't have 3 occurrences within a short window. So any other value can appear at most once, otherwise it would appear twice, which would require two occurrences of that value with spacing at least 3, but they could be within the subarray? Possibly, but if two distinct values each appear twice, then the dominating would need >2. So we must avoid any other value appearing twice. So the other elements must all be distinct. Therefore the subarray length L = 2 + d, where d = number of other distinct elements, each appears once. And these other elements must be distinct from each other and from the dominating value.

    Now, is it possible to have a dominated subarray of length 5 under spacing >=3? For L=5, we would have 2 occurrences of dom, and 3 other distinct values. That's (2,1,1,1). The dominating count 2 > 1 each, so dominated. But can we have such a subarray without any adjacent equal or gap2 equal? Let's test if such a pattern can exist: We need two positions for the dominating value, say indices i<j, with j-i ≥3 (since no gap2). Also, the three other distinct values occupy the other positions. The subarray is contiguous. Let's try to construct: indices within a window of length 5: positions p, p+1, p+2, p+3, p+4. Choose two positions for dominating value x, distance at least 3. Options: (p, p+3), (p, p+4), (p+1, p+4). Also possibly (p, p+3) gives distance 3; (p, p+4) distance 4; (p+1,p+4) distance 3. All satisfy spacing >=3. Fill the rest with distinct values not equal to x and all distinct among themselves. That seems possible if the array contains such values. So length 5 dominated subarray might exist even without adjacent or gap2 equal. However, note that if we have a length 5 subarray with dominating value appearing twice, spacing at least 3, and others distinct, it is dominated. So the shortest could be 4, maybe 5, maybe even longer. But we need the shortest overall.

    Thus the answer could be any length from 2 up to maybe O(n). So we need an efficient algorithm to find the minimum length subarray where some element occurs strictly more times than any other.

    We have constraints: total n across tests ≤ 2e5, T ≤ 1000. So O(n log n) or O(n) per test is okay.

    We need to compute min length L such that there exists a subarray with unique max frequency.

    Brute force O(n^2) too slow.

    We need to think about properties. This problem looks like a classic "Find shortest subarray with majority element" but here it's "strictly greater than each other frequency", not necessarily > half. However, note that if an element appears more than any other, it's the mode. For a subarray, the condition is that the mode is unique and its count > all others. This is equivalent to: there exists an element that appears at least twice, and its count is at least 2 more than any other? Not exactly; it's just strictly greater. So if it appears k times, every other appears at most k-1. So the second highest frequency is at most k-1. This is a kind of "strict majority" in terms of being the only top.

    Observation: Since the array values are between 1 and n, we could try to consider each value as the candidate dominating value, and find shortest subarray where that value appears more often than any other. That might be done by sliding window or two pointers.

    But maybe there is a simpler observation: The shortest dominated subarray length is either 2, 3, or something else? Let's test more examples.

    Given no adjacent equal and no gap2 equal, we had an example length 4 dominated. Could there be length 4 dominated without those patterns? Yes, we saw [1,2,1,3]. But is there always a dominated subarray of length 4 if any dominated subarray exists? Not necessarily. Consider an array with distances between equal elements at least 4? But condition of no adjacent/gap2 only ensures min distance >=3, but maybe the array might not have any subarray of length 4 with exactly two occurrences of some element and others distinct. However, if any dominated subarray exists, it must contain some element that appears at least twice. Let the dominating element be x. Consider the subarray from the first occurrence of x to the second occurrence of x (the two closest occurrences). Since the two occurrences are at distance d (difference in indices) with d≥3 (if no gap2). The subarray spanning from i to j inclusive has length d+1. Within this subarray, x appears at least twice. There may be other occurrences of other values inside. Could this subarray be dominated? Not necessarily, because there might be another value that also appears twice inside that span, or maybe x appears only twice but some other appears also twice? But if there is any dominated subarray, maybe the shortest one will involve two occurrences of the dominating element that are relatively close, such that no other value repeats.

    Intuitively, if there is any dominated subarray, then consider the two occurrences of the dominating element that are closest together in the entire array (the minimal distance between any two equal elements). Let that distance be d (i.e., j - i = d). Then the subarray from i to j inclusive has length d+1. In this subarray, x appears at least twice. Could there be a shorter dominated subarray? Possibly, but maybe the shortest dominated subarray length is either 2, 3, or something like the minimal value of (second occurrence - first occurrence + 1) for some element, but maybe also need to consider if that subarray itself is dominated. However, it's not guaranteed that the subarray between the two closest equal elements is dominated; there might be another element that appears twice as well, or x appears only twice but some other appears also twice? Actually if there is another element that appears twice within that interval, then x would not be strictly greater (since tie). But maybe a smaller subarray that excludes one of those other duplicates could be dominated. So we need to find the minimal length where some element appears strictly more times than any other.

    We can try to formulate as: Find minimal length L such that there exist indices l ≤ r with r-l+1 = L, and some value v with frequency f_v > max_{u≠v} f_u. Equivalent: f_v >= max_{u≠v} f_u + 1.

    Since values are small (1..n) but n up to 2e5, maybe we can use the fact that the total sum of frequencies is L. So if v appears k times, the sum of other frequencies is L-k. The condition that each other is at most k-1 is equivalent to L-k ≤ (k-1)*(#distinct others) but that's not simple.

    Alternative approach: Since we need the shortest subarray, maybe we can binary search L and check if there exists a dominated subarray of length ≤ L. But T up to 1000, total n 2e5, maybe O(n log n) overall is okay. But checking for existence of dominated subarray of length exactly ≤ something might be done with sliding window? For a fixed L, we need to determine if there is a subarray of length L where some element is strictly more frequent than others. We can compute frequencies in sliding window, and maintain the maximum frequency and maybe second max? But we need to know if max is unique and strictly greater than all others. Maintaining just max frequency might be insufficient because we need to know if it's strictly greater than the second max. However, we can track frequencies in O(1) update per slide if we maintain an array of counts and also a data structure for max and second max. Could use a segment tree or multiset? But L varies, and we'd need to do binary search with O(n log n) each test, might be okay? total n=2e5, log n ~18, so O(n log n) ~3.6e6 per test? But T can be 1000 but total n is limited, so overall O(total_n log n) ~ 2e5*18 = 3.6e6, which is fine. However, binary search over L from 2 to n would require O(log n) checks each O(n) maybe with sliding window using a data structure that supports O(1) update of max frequency? Actually sliding window with frequency counts: we need to check if any window of length L has a unique max frequency. We can maintain an array cnt[value] (value up to n) for current window. To check if max cnt is unique and > others, we could maintain the maximum count value and the number of values achieving that maximum. If the number of values with cnt == max is exactly 1, then we have a dominated window. The max count can be updated when we increment/decrement counts. However, we need to know the maximum count efficiently. Since we are sliding a window of fixed length L, we could maintain a multiset of counts? But values are up to n, and we have n elements. The count of a value changes by ±1. The maximum count across values can be maintained using a variable that tracks the current max, but it's tricky to update when the max decreases because we need to know the next max. However, we could use a frequency-of-counts array: let freqCount[c] be the number of values that have count c. Then the maximum count is the largest c with freqCount[c] > 0. When we increment a value from old count to old+1, we decrement freqCount[old] and increment freqCount[old+1]; similarly for decrement. Then we can maintain the max count by maybe using a variable that we update lazily: after each operation, if we increased count to newc and newc > currentMax, update currentMax = newc. If we decreased count from oldc where oldc == currentMax and freqCount[oldc] becomes 0, we need to decrement currentMax until we find a c with freqCount[c] > 0. Since counts only change by 1, currentMax can only change by at most 1 per step, so we can adjust by while freqCount[currentMax] == 0: currentMax--. That's O(1) amortized? Actually each decrement of currentMax is permanent, total decrements across all slides bounded by n. So sliding window with this technique would be O(n) per L check. So binary search with O(log n) checks would be O(n log n) per test. total n across tests 2e5, so O(2e5 log 2e5) ~ 3.6e6, which is fine.

    But binary search is for the minimal L such that a dominated subarray exists. However, note that it's possible that there is a dominated subarray of length 10 but none of length 9? But if there is a dominated subarray of length L, does there necessarily exist a dominated subarray of length L' for any L' > L? Not necessarily. For example, maybe the only dominated subarray is exactly length 4, and no length 5? But we can check: if we have a dominated subarray of length 4, could there be a shorter one? We are looking for minimal length. So we want the minimal L where any dominated subarray exists. So we need to find if there exists dominated subarray of length exactly some small number. Binary search for minimal L such that there exists at least one dominated subarray of length ≤ L is monotonic? If there is a dominated subarray of length L0, then for any L ≥ L0, there exists a dominated subarray of length ≤ L? That's trivially true because we already have one of length L0 ≤ L. But we need to check existence of dominated subarray of length exactly any value. For binary search on answer, we can define predicate P(L): there exists a dominated subarray of length at most L. This predicate is monotonic: if there is a dominated subarray of length L0, then for all L ≥ L0, P(L) is true. So we can binary search for the smallest L such that P(L) true. However, we need to compute P(L) efficiently for a given L. That is, check if any subarray of length ≤ L is dominated. But our sliding window technique works for fixed window length, not ≤ L. To check for existence of any subarray of length ≤ L, we could iterate over all possible lengths up to L, which is O(L*n) not feasible. So better to directly search for minimal length without binary search. Instead, we can attempt to find the minimal length directly by scanning.

    Maybe there's a more clever insight: The shortest dominated subarray length is at most some small constant? But we found length 4 works without adjacent/gap2. Could length 5 be needed? Possibly if for any pair of equal elements, the distance between them is at least 5? But if distance is 5, then the subarray covering those two has length 6. But maybe there is a shorter subarray where some element appears twice? If the minimum distance between any two equal elements is d (i.e., the smallest difference between indices where a[i]==a[j]), then the length of subarray from i to j inclusive is d+1. That subarray contains at least two of that element. Could there be a dominated subarray shorter than d+1? If d is the minimal distance among all equal pairs, then any subarray that contains two equal elements must have length at least d+1 (since it must span at least the distance between those two equal elements). However, a dominated subarray could have a dominating element that appears only once? No, must appear at least twice because with all distinct, there is no unique max (all ties). So any dominated subarray must contain at least one element that appears at least twice. Therefore, its length must be at least (minimum distance between any two equal elements) + 1? Not exactly: The dominating element could be one that appears twice; the subarray must include both occurrences. The distance between those two occurrences could be larger than the global min distance if we choose a different element. But the shortest dominated subarray overall will involve the two closest equal elements (maybe of some value). But as argued earlier, the subarray covering those two might not be dominated because other elements might also appear twice within that interval. However, we can consider subarrays that are subsets of that interval (i.e., we might shrink the interval to exclude the other duplicate occurrences). For example, suppose the two closest equal elements are at positions i and j (j-i = d), but within [i,j] there is another value that appears twice (say at positions k and l, with i<k<l<j). Then the subarray [i,j] is not dominated because that other value also appears twice, causing tie. But perhaps a shorter subarray that still contains the two equal elements of the dominating value and excludes the duplicate of the other could be dominated. For instance, if we take subarray from i to l-1 (excluding one occurrence of the other duplicate), then the other value might appear only once, and our dominating value appears twice? But if we exclude one occurrence of the other value, we might also need to ensure the dominating value still appears twice. We might be able to adjust endpoints to get a dominated subarray of length less than d+1. But maybe the minimal length is still limited.

    We need to design an algorithm that finds the shortest dominated subarray. Since n is up to 2e5, we could attempt O(n sqrt n) or O(n log n) by enumerating potential dominating values and considering occurrences.

    Observation: For a subarray to be dominated by value v, v must appear at least twice (or could appear once if all others appear 0? but subarray length≥2, so if v appears once and others appear zero? impossible because sum of counts = length, so if v appears once, there is at least one other element with count ≥1. So the other count(s) would be at least 1, tie not strictly greater). So indeed v appears at least 2.

    Thus we only need to consider elements that appear at least twice in the whole array. For each value v, we can look at its occurrence positions: pos1, pos2, ..., posm.

    For any subarray dominated by v, it must include at least two occurrences of v. Suppose we include occurrences from index i to index j (i-th and j-th occurrence, i<j). Then within that subarray, v appears at least j-i+1 times (if we include all occurrences between them, actually if we include the first and last, we may also include intermediate ones). But we could also include more occurrences on ends. However, for minimal length, we might consider subarrays that start at some occurrence of v (or just before) and end at another occurrence.

    Idea: For each value v, we can consider pairs of its occurrences (not necessarily consecutive) and try to find minimal subarray containing those two occurrences where v is dominant. However, that seems complex.

    Maybe there is a known result: The shortest subarray with a mode of frequency > all others has length at most something like the minimal distance between two equal elements plus a constant? But from example [1,2,1,3], the minimal distance between two equal elements (1's) is 2 (indices 1 and 3 difference 2) -> that yields length 3, but that subarray [1,2,1] is length 3 but is it dominated? Yes! Actually [1,2,1] is length 3 and dominated. Wait, [1,2,1] has two 1's with distance 2, which is gap2 equal. But our array [1,2,1,3] has a gap2 equal (a[1]==a[3]). So that would give length 3 dominated, not length 4. Indeed we missed that. In [1,2,1,3], there is a[1]==a[3] (distance 2). So there is a length 3 dominated subarray. So my earlier claim that there is no gap2 equal in that array was wrong. Let's check: indices 1 and 3: both 1? a[1]=1, a[3]=1? In [1,2,1,3], a[3] is 1, so yes, a[1]==a[3] difference 2 -> gap2 equal. So actually length 3 exists. So the example that we thought of as length 4 without gap2 equal actually had gap2. Let's find a concrete example where there is a dominated subarray but no adjacent equal and no gap2 equal.

    We need an array where the minimum distance between any two equal elements is at least 3. So all equal pairs are at distance >=3. Under that condition, can there be a dominated subarray? Let's try to construct one. We need a subarray where some element appears twice, with distance >=3 between its two occurrences, and all other elements in the subarray are distinct and not equal to that element, and no other element appears twice. The subarray length will be at least distance+1. If the two occurrences are at indices i and j with j-i >=3. The subarray from i to j inclusive has length j-i+1. Could there be a subarray shorter than that which still contains both occurrences? No, the minimal subarray containing both i and j is exactly from i to j. So the minimal possible length for a subarray that contains these two occurrences is j-i+1. But we could consider including more occurrences of v, but that would increase length.

    Thus, if there exists any pair of equal elements at distance d (d = j-i), then there is a subarray of length d+1 that contains exactly those two occurrences and possibly others between them. However, within that subarray, there might be other repeats. But we can also consider a subarray that starts at i+1 or ends at j-1? That would exclude one of the two occurrences, so v would appear only once. So we need both. So the minimal length for a subarray containing those two specific occurrences is d+1. So if we can find a pair of equal elements with distance d such that the subarray from i to j is dominated (i.e., v appears twice and all other values appear at most once, and no other value appears twice), then we have a candidate of length d+1. The shortest dominated subarray would be the minimum of such d+1 across all pairs (i,j) with a[i]=a[j] and such that the subarray [i,j] is dominated.

    But maybe there could be a dominated subarray where the dominating element appears more than twice, but with smaller length? For instance, if v appears three times, but the distances between consecutive occurrences are large, total length could be larger, so not shorter. So the shortest likely involves exactly two occurrences of the dominating element. So we can focus on pairs of equal elements.

    Thus, the problem reduces to: For each value v, consider its occurrence positions. For each pair of positions (i,j) with i<j, check if subarray a[i..j] is dominated by v. Find the minimum length (j-i+1) among such dominated subarrays. Also, we might consider subarrays where v appears more than twice but perhaps length could be smaller than some pair distance? That doesn't make sense because if you have three occurrences, the distance between the first and last is at least the distance between some pair, so length is at least that pair's distance+1. So the minimal length from v is at most the distance between its two closest occurrences (consecutive occurrences). But the subarray covering the two closest occurrences might not be dominated if there is another repeated value within that interval. However, perhaps there exists a subarray that includes those two occurrences but excludes some other repeat by adjusting endpoints slightly? But if we adjust endpoints to exclude the other repeat, we might also exclude one of the v occurrences or include only part of the interval. Let's analyze: Suppose we have two occurrences of v at positions p and q (p<q) and within (p,q) there is some other value w that appears twice? Actually if w appears twice within [p,q], then we might try to shrink the subarray to a smaller interval that still contains both p and q but excludes one occurrence of w. But if w appears at indices r and s (p<r<s<q), to exclude one of them, we would have to either start after r (which would exclude p if p<r) or end before s (which would exclude q). So we cannot exclude one of the w occurrences while still including both p and q, because the interval containing p and q must contain all indices between p and q. So if there is a duplicate of w inside, then any interval that contains both p and q will contain both r and s as well (since they lie between). Thus w would appear twice, causing tie unless v appears more than twice. Could v appear more than twice to beat w? Possibly, if v appears three times within [p,q] then its count could be 3 > 2. But to have three occurrences, we need at least two distances. The minimal length for three occurrences might be larger. So perhaps the shortest dominated subarray may involve three occurrences of v if the two-occurrence subarray is not dominated due to another duplicate, but three-occurrence subarray might be shorter than any two-occurrence dominated subarray from another value? However, the three-occurrence subarray length will be at least the distance between first and third occurrence, which is likely larger than the distance between the two closest occurrences of some value (maybe even the same value). So we should consider both possibilities.

    But maybe we can prove that if there is any dominated subarray, then there exists a dominated subarray of length at most 4? Let's test with a constructed array that has no adjacent equal, no gap2 equal, yet has a dominated subarray. We need an array where the minimum distance between any two equal elements is at least 3, but still there is a subarray containing two occurrences of some element and all other values distinct. That subarray's length would be the distance between those two occurrences +1. The distance must be at least 3. So the minimal possible length is 4 (if distance=3). So we need to find an array where two equal elements are exactly 3 apart (i.e., indices i and i+3 are equal), and the two elements between them are distinct from each other and from the equal element, and also there is no other repetition within the interval (i.e., the two middle values are distinct and also not equal to the outer ones). That would be [x, a, b, x] with a≠b, a≠x, b≠x. This is a subarray of length 4. Is it dominated? Counts: x appears twice, a and b each once. So dominated. And there is no adjacent equal (x!=a, a!=b, b!=x) and no gap2 equal? Check: i and i+2: x vs a? not equal; i+1 and i+3: a vs x? not equal; i and i+2? already; i+1 and i+3? not equal. So indeed no gap2 equal. So this is a valid pattern: a[i] == a[i+3] and a[i+1] != a[i+2], and both different from a[i]. But note that the condition for domination also requires that no other value appears twice. In this length-4 window, the only possible duplicate is the two x's. So it's dominated. So length 4 dominated subarray exists if there is any occurrence of a value at positions i and i+3 with the two middle elements distinct and not equal to that value. That is more specific than just any two occurrences at distance 3, because if the middle elements have some duplication, e.g., [x, y, y, x] then counts: x:2, y:2 -> tie, not dominated. Or [x, y, x, y]? That would be distance 2? Actually if positions i and i+2 equal, that's gap2, which we already covered. For distance 3, if middle elements are equal to each other, then we have y appearing twice, tie. So not dominated. Also if one middle equals x, e.g., [x, x, y, x]? That would have adjacent equal (x,x) which we already covered. So the minimal pattern for length 4 dominated without length2/3 is exactly "x a b x" with a,b distinct and not x.

    Could there be a length 5 dominated subarray without any length 2/3/4? That would require minimum distance between equal elements to be at least 4? Because if there is a pair at distance 3, we already have potential length 4. If there is a pair at distance 4, then we could have length 5 subarray [x, a, b, c, x] with a,b,c all distinct and not x, and also no other repeats. That would be dominated (x twice, others once each). So length 5 could be minimal if there is no pair at distance 2 or 3 that yields a dominated subarray. But does existence of a pair at distance 4 guarantee a dominated subarray of length 5? Not necessarily; the elements between might contain repeats, or the outer element may appear also inside? But if there is any pair at distance 4 (i and i+4 equal), then the subarray from i to i+4 has length 5. For it to be dominated, we need: x appears twice (at ends), and the three inner elements must all be distinct from each other and from x, and also must not contain another occurrence of x (obviously not, since only ends are x). However, they could contain a duplicate among themselves: e.g., [x, a, b, a, x] -> then a appears twice, tie with x (2 vs 2), not dominated. Or [x, a, a, b, x] -> a appears twice, tie. Or [x, a, b, b, x] tie. So the subarray [i,i+4] may not be dominated. But perhaps we could choose a different subarray containing those two x's that is shorter? The minimal subarray containing both x's is exactly [i,i+4] (length 5). So if that interval is not dominated, maybe there is another pair of x's with smaller distance? If not, maybe another value yields a shorter dominated subarray. So the minimal length may be larger.

    Thus, the problem is more involved. However, constraints are moderate; we might be able to compute the answer by scanning and maintaining for each value the last occurrence and perhaps checking certain conditions.

    Given that total n is 2e5, perhaps we can find the minimal length in O(n sqrt n) or O(n log n). Let's think of a direct algorithm.

    Idea: For each index as right endpoint, we can try to find the smallest left endpoint such that subarray [l,r] is dominated. That would give candidate length (r-l+1). If we can compute for each r the minimal l that yields domination, we could take global min. So we need to quickly determine, given current window expanding leftwards, when does it become dominated? But that's like two pointers moving rightwards? Could use sliding window with variable length: maintain left pointer L and right pointer R, and try to shrink L while condition holds? But the condition "exists v such that its count > all others" is not monotonic in the sense that if a window is dominated, does any subwindow (smaller) also dominated? Not necessarily. Example: [1,2,1] dominated; subwindow [1,2] not dominated; [2,1] not dominated. So property not monotonic. So two-pointer may not work directly.

    Alternate approach: Since we only need the shortest length, perhaps we can limit search to small lengths. Could the answer ever be larger than, say, 5? Let's test by constructing arrays with no dominated subarray of length ≤ k but having one at length k+1. For k=4, we need an array where no dominated subarray of length 2,3,4 exists, but there is one of length 5. Is that possible? Let's attempt to construct.

    We need to avoid any adjacent equal (otherwise length2). Avoid any gap2 equal (otherwise length3 dominated). Avoid any pattern giving length4 dominated: that is, avoid having any i such that a[i] == a[i+3] and the two middle are distinct and not equal to a[i] (and also avoid any other length4 dominated patterns like three occurrences? But three occurrences would require adjacent/gap2). So length4 dominated can only arise from a pair at distance 3 with inner distinct and no other repeats. So to avoid length4 dominated, we must ensure that for every i with a[i]==a[i+3], either a[i+1]==a[i+2] or one of them equals a[i] or some inner duplication causing tie. But if a[i]==a[i+3] and inner are distinct and not x, then that subarray is dominated. So to avoid length4 dominated, we must prevent such "clean" x a b x patterns.

    Now, could there be a length5 dominated subarray without having any length2/3/4 dominated? Let's try to construct. Example: Consider array of length 5: [1,2,3,4,1] -> check length2? no adj equal. Length3? check i=1:1,3? 1 vs 3? no; i=2:2,4? 2 vs 4? no; so no gap2 equal. Length4? i=1: compare a1 and a4? 1 vs 4? no; i=2: a2 and a5? 2 vs 1? no. So no length4. But is length5 dominated? Counts: 1 appears twice, others once each => dominated. So [1,2,3,4,1] is a dominated subarray of length5, and there are no shorter dominated subarrays. So answer could be 5. Let's verify: a[1]=1, a[5]=1 (distance 4). The inner elements are 2,3,4 all distinct and not 1. So yes. So length5 is possible.

    Now length6? Could we have array of length6: [1,2,3,4,5,1] with all distinct inner? That's distance 5, length6. Check shorter: any adjacent? no. gap2? check i=1:1 vs 3? 1 vs 3 no; i=2:2 vs 4? no; i=3:3 vs5? no; i=4:4 vs6?4 vs1 no. length4? check i=1: a1 vs a4? 1 vs4 no; i=2: a2 vs a5?2 vs5 no; i=3: a3 vs a6?3 vs1 no. length5? check i=1: a1 vs a5? 1 vs5? no; i=2: a2 vs a6? 2 vs1 no. So no length5. But length6 itself: counts: 1 appears twice, others once each -> dominated. So answer could be 6. So in principle, we can have arbitrarily long minimal dominated subarray by having two occurrences of a value at distance L-1, with all inner elements distinct and not equal to that value, and also ensuring no other pair of equal elements at smaller distance yields a dominated subarray. However, we also need to avoid that any other value appears twice within that interval. Since all inner are distinct, that's fine. But we also need to avoid that any other value appears twice due to overlapping with other parts? The subarray is exactly that interval; we only care about that interval being dominated. So as long as there is a pair of equal elements at distance d, and the elements between them are all distinct and also distinct from the endpoints, then the subarray of length d+1 is dominated. And if no smaller such "clean" pair exists, the answer could be d+1. So answer can be as large as O(n). So we need an algorithm that finds the minimal such "clean" pair.

    Thus, the problem reduces to: Find the minimum length L = j-i+1 such that a[i] = a[j] and in the subarray a[i..j], the value a[i] appears exactly twice? Actually it could appear more times, but if it appears more than twice, it's still dominated as long as its count > any other. However, if it appears more than twice, maybe we can find a smaller subarray using a subset of those occurrences. Typically, the minimal length involving that value would be the distance between the two closest occurrences of that value. So we can restrict to consecutive occurrences of each value. Because if we have occurrences pos1<pos2<pos3, then the subarray from pos1 to pos2 is shorter than from pos1 to pos3. So if any dominated subarray exists for that value, the minimal length using that value would be at most the distance between some consecutive occurrences (maybe not exactly the minimal consecutive if that subarray is not dominated, but a larger gap might yield domination while the smaller doesn't). However, if the smaller gap subarray is not dominated, maybe the larger gap subarray is dominated, but its length is larger, so it wouldn't be candidate for global min if there exists another value with smaller gap that yields domination. But it's possible that for a given value, the minimal gap (consecutive) does not yield domination, but a larger gap (say skipping one occurrence) yields domination, and that length might still be smaller than any other value's dominating subarray. Example: Suppose value x occurs at positions 1, 4, 5. The consecutive gaps: 1-4 (gap3 length4) and 4-5 (gap1 length2). Gap1 (adjacent) would give length2 dominated (since adjacent equal) unless? Actually if positions 4 and 5 are both x, then adjacent equal gives length2 dominated, so answer would be 2, regardless of other gaps. So if there is any adjacent equal, answer is 2. So if we are in the regime where no adjacent equal, then consecutive gaps are at least 2. If gap=2 (consecutive at distance 2), that yields length3 dominated, because two occurrences with one element between; as long as that middle element is not x (it isn't, because x only at those positions), then the subarray contains two x's and one other. Is it always dominated? If the middle element is some y, then counts: x:2, y:1 => dominated. So any consecutive gap of exactly 2 gives a length3 dominated subarray. So if there is any such gap, answer is 3. So if answer >3, we have no adjacent equal and no gap2 equal. Thus consecutive gaps are at least 3. So for each value, its consecutive occurrences are at distance ≥3.

    Now consider a value with consecutive occurrences at distance d ≥3. The subarray from first to second occurrence (consecutive) has length d+1. In this subarray, the value appears exactly twice (since they are consecutive occurrences, there is no other occurrence in between). The other positions (d-1 of them) contain other values. Are these other values all distinct? Not necessarily; there could be duplicates among them. If any of those other values appear twice within the subarray, then the subarray would not be dominated because that other value would have count at least 2, tying with x. Also, if any of those other values equals x (impossible because these are the only x's in between). So the subarray is dominated if and only if all the other values in between are distinct (i.e., no value repeats among them). Because then x appears twice, and every other appears at most once. That's exactly the condition: occ(x)=2, all others ≤1.

    Thus, for a value with consecutive occurrences at indices p and q (p<q), the subarray [p,q] is dominated iff the elements in positions (p+1 .. q-1) are all distinct (and none equal to a[p]). Since a[p] = x and a[q] = x, we just need that there is no duplicate among the inner elements. Also, we need that there is no other occurrence of x inside, which is true by consecutiveness. So condition: the subarray between p and q inclusive is dominated by x iff there are no repeated values in the open interval (p,q) (i.e., all elements in that range are distinct). This is because the only element that could have count >=2 is x (2) and any other would have at most 1 if all distinct. But what if there is a value that appears both inside and outside the subarray? That's irrelevant; we only consider inside the subarray.

    Thus, the problem becomes: Find the minimum value of (q - p + 1) over all pairs of indices (p,q) with a[p] = a[q] (and preferably consecutive occurrences? But what about non-consecutive? Suppose we have occurrences p, r, q with p<r<q. Then subarray [p,q] contains three x's. Could [p,q] be dominated while [p,r] is not? Possibly, if [p,r] inner had a duplicate causing tie, but [p,q] may have x count 3 which could beat any duplicate (if duplicate count is 2). However, length of [p,q] is larger than [p,r] (since r between), so if [p,r] is not dominated, maybe [p,q] is, but its length is larger. Could it be that the global minimum comes from a non-consecutive pair because for some value, the consecutive pair gives a subarray length L1 but not dominated, but the next pair (p,q) with an extra occurrence in between yields a dominated subarray of length L2 where L2 is less than some other value's dominated subarray? But L2 > L1, so if there exists any other value with a dominated subarray of length <= L1, that would be smaller. But if no value has a dominated subarray of length <= L1, maybe L2 is the minimal. However, note that if consecutive pair (p,r) is not dominated because inner has a duplicate, then we know that within (p,r) there is some value y that appears at least twice. But if we extend to include an additional x at q, we now have three x's. The duplicate y may still appear twice (or maybe more). For the new subarray [p,q] to be dominated, we need 3 > count(y) (and any other). If count(y)=2, then 3>2, so dominated. So length = q-p+1. Could this be smaller than some other value's dominated subarray? Possibly, but also there could be another value with a closer pair giving dominated subarray. But is it possible that for all values, the consecutive pairs are not dominated, but a non-consecutive pair yields a dominated subarray that is actually shorter than any other dominated subarray from any value? Since consecutive pairs are the shortest pairs for each value, if none of those are dominated, then for each value the shortest candidate length for that value is the length of the smallest gap between its occurrences. That is, the minimal length subarray containing two occurrences of that value is from consecutive occurrences. If that's not dominated, we may need to consider three occurrences. But three occurrences yields a longer subarray. So the global minimum could be from a non-consecutive pair only if for some value the consecutive pair length is L1 but not dominated, and the next pair (maybe with an extra occurrence) yields length L2, and there is no other value with a dominated subarray of length <= L2. But L2 > L1, so if there exists any other value with a consecutive pair of length <= L1 that is dominated, then that would beat L2. If for all values, all consecutive pairs are not dominated, then the answer might be from a non-consecutive pair or from a different value's non-consecutive pair. However, could it be that a non-consecutive pair yields a shorter length than another value's consecutive pair that is not dominated? No, because consecutive pair lengths are the minimal possible lengths for that value. So the minimal length among all pairs (consecutive or not) for any value is actually the minimal distance between any two equal elements overall. Let d_min be the minimal distance between two equal elements in the array (i.e., minimal j-i such that a[i]==a[j]). If the subarray corresponding to that minimal distance is dominated, then answer = d_min+1. If it's not dominated, maybe the answer could be larger than d_min+1. However, is it possible that for the pair achieving d_min, the subarray is not dominated, but there exists another pair (with slightly larger distance) that yields a dominated subarray, and that distance +1 is less than any other value's dominated subarray? Since d_min is the smallest distance, any other pair has distance >= d_min, so its length is >= d_min+1. So if the subarray for d_min is not dominated, then the minimal length for any dominated subarray must be > d_min+1, but could be equal to d_min+2 or more. However, it could be that the pair with distance d_min is not dominated, but the pair with distance d_min+1 (next smallest distance) yields a dominated subarray, and that length is d_min+2. That could be the answer.

    But note that d_min could be 1 (adjacent equal) => then answer 2 (dominated) because adjacent equal always gives dominated? Actually check: adjacent equal [x,x] always dominated. So if d_min=1, answer 2. d_min=2 gives length 3, also always dominated? For distance 2, subarray length 3: pattern x ? x. Could it be not dominated? If the middle element equals x? That would be three x's? Actually if a[i]=x, a[i+1]=x? That's adjacent equal, so d_min would be 1, not 2. So with d_min=2, we know there are no adjacent equals, so the middle element is not x. So subarray [i,i+2] contains two x's and one other element, so x appears twice, other once => dominated. So d_min=2 always gives dominated. So the only problematic case is when d_min >= 3. For d_min >= 3, the subarray corresponding to that minimal distance might not be dominated if there is a duplicate among the inner elements. For example, d_min=3: subarray length 4: pattern x a b x. If a and b are distinct and not x, it's dominated. But if a = b, then counts: x:2, a:2 => tie, not dominated. So the subarray for the minimal distance might not be dominated.

    Thus, the problem reduces to: Find the smallest distance d such that there exists a pair i<j with a[i]=a[j] and j-i = d, and the elements in (i,j) are all distinct (i.e., no repeats). Then answer = d+1. Because if the inner elements are all distinct, then the subarray is dominated. Conversely, any dominated subarray must contain some element v that appears at least twice. Consider the first and last occurrence of v within that subarray; let those indices be p and q (the extreme positions of v in that subarray). Then p and q are a pair of equal elements with q-p <= length-1. Moreover, within the subarray, v appears at least twice; there could be other occurrences of v between, but we can consider the subarray from p to q. That subarray is contained within the original and has length ≤ original length. Is it guaranteed to be dominated? Not necessarily; maybe the original subarray was dominated because v appears three times and some other appears twice, but in the subarray from p to q, v appears at least two (maybe exactly two if no other v inside) and the other might still appear twice. But if the original was dominated, it's possible that the subarray from p to q is not dominated because it might have tie. However, if we take the extreme occurrences, and if there is any other value that appears at least as many times as v within [p,q], then v would not be dominating in [p,q] but might be dominating in the larger window due to extra v occurrences outside p,q? But p and q are the extremes of v, so any occurrence of v is within [p,q]. So the total count of v in the original subarray equals the count of v in [p,q] (since all v are between p and q). The other values that appear in the original subarray may also appear partly outside [p,q]? Actually the original subarray may extend beyond p and q. But if we shrink to [p,q], we are removing elements outside [p,q]. Could that reduce the count of some competitor, making v dominate? Possibly. But we need the shortest subarray. So if the original dominated subarray is of length L, we can try to shrink it to [p,q] which is length L' ≤ L. If [p,q] is dominated, then we have an even shorter dominated subarray. If not, then the domination in the original came from elements outside [p,q] contributing extra v? But v count is same. Competitors' counts might be lower in [p,q] because some of their occurrences were outside. Actually the original subarray might include occurrences of some other value outside [p,q] that increased its count, making it harder for v to dominate. So removing those could only decrease competitors' counts, potentially making v dominate. Wait: Suppose original subarray is [l,r] with l ≤ p < q ≤ r. The count of v in [l,r] is the same as in [p,q] because all v's are within [p,q]. For any other value u, its count in [p,q] is ≤ its count in [l,r] because [p,q] is a subset (we might remove some occurrences that are outside). So if v dominated in [l,r] (i.e., count(v) > count(u) for all u), then in [p,q], count(v) remains the same, count(u) possibly decreases, so the inequality still holds: count(v) > count(u) for all u, because if it was strictly greater before, and we decrease the right side or keep same, it remains strictly greater. Actually careful: If we remove occurrences of u that are outside [p,q], count(u) decreases, so if count(v) > count(u) originally, after decreasing count(u) it's still >. However, there is a corner case: what if u had count equal to count(v) originally? Then it wouldn't be dominated. So originally count(v) > count(u) for all u. So after shrinking, it remains > because count(v) unchanged and count(u) nonincreasing. So [p,q] would also be dominated. Therefore, for any dominated subarray, the subarray formed by the first and last occurrence of the dominating value within it is also dominated and its length is ≤ the original length. Moreover, within [p,q], the dominating value appears at least twice (since p and q are occurrences). Also, by construction, there are no other occurrences of the same value outside p,q? Actually there could be other occurrences between p and q, but they are inside. So the extreme occurrences pair yields a dominated subarray. And importantly, within this subarray, the dominating value appears at least twice, and all its occurrences are inside; but could there be other values that appear twice? If there were, then v's count must exceed them; but that's okay. However, we can further note that if v's count is > all others, then in particular it is > any other count. In the minimal such subarray (shortest), it's likely that v appears exactly twice and all others appear at most once. Because if v appears more than twice, we could possibly remove some inner occurrence(s) to get a shorter subarray? But careful: If v appears three times, say at positions p<r<q. Then consider the subarray from p to q, which contains all three. If we remove the middle occurrence by taking a subarray that excludes r, we might still have two occurrences (p and q). But would that subarray be dominated? Possibly, if after removal, the count of v becomes 2, and we must ensure that no other value appears twice. Since the original subarray [p,q] was dominated with v count 3 > others, after removing r, v count becomes 2. If there was a competitor with count 2 originally, then after removal it might still be 2, causing tie. But if no competitor had count >=2, then after removal v count 2 > others 1, still dominated. So we could potentially get a shorter subarray by dropping some inner v occurrence, as long as the resulting subarray still contains at least two v's and no other duplicate. However, the new subarray would be from p to q (still same length) if we just remove r from consideration? To exclude r, we would need to adjust endpoints so that r is not included. For instance, if we take subarray from p to r-1 (if r-1 >= p) that includes p but not r nor q; that would contain only one v. So not good. To have two v's, we need both p and q, but to exclude r, we would need to have a subarray that includes p and q but not r. That's impossible because if p < r < q, any interval containing p and q will automatically include r (since it's continuous). So you cannot exclude an interior point while including both ends. Therefore, if v appears three times, the minimal subarray containing two of them might be between two consecutive occurrences (which are closer). So the globally shortest dominated subarray for that value would be from the two closest occurrences (consecutive) rather than from extremes. Because if consecutive occurrences are at distance d, that yields length d+1, which is less than or equal to the distance between non-consecutive extremes. So for a given value, the shortest possible subarray that contains at least two occurrences of that value is exactly the subarray between two consecutive occurrences (the ones with minimal distance). So we can restrict to consecutive occurrences.

    Thus, any dominated subarray can be shrunk to one that is exactly the interval between two consecutive occurrences of the dominating value, and which remains dominated. Let's prove: Let subarray S be dominated by v. Let p and q be the first and last occurrences of v in S. If there is another occurrence r between p and q, then consider the consecutive pair among v's occurrences that are closest. Specifically, let i and j be two occurrences of v that are consecutive in the whole array (not necessarily in S) but both lie in S. Their distance is ≤ q-p. The subarray T from i to j is contained in S (since i and j inside S). Since S is dominated by v, and T is a subinterval of S, is T necessarily dominated? Not necessarily, as argued earlier because the counts of competitors might decrease, but v's count in T might be only 2 (if i and j are consecutive and no other v between). The condition in S: count_S(v) > count_S(u) for all u. For T, count_T(v) = 2 (or possibly more if there are more v's between i and j, but if they are consecutive then exactly 2). count_T(u) ≤ count_S(u). So we have 2 ≤ count_S(v) (since at least 2) but we only know count_S(v) > count_S(u). It does not guarantee 2 > count_T(u) because count_T(u) could be 2 as well. For example, S = [v, a, v, a] length 4, v appears twice, a appears twice -> not dominated. So not a good example. Need S dominated. Suppose S = [v, a, b, v, a] length 5? v:2, a:2 -> tie. Not dominated. Need a case where S dominated but T not. Let's try: S = [v, a, b, v, c, v] length 6? v:3, a:1, b:1, c:1 -> dominated. The consecutive occurrences of v: positions 1 and 4 distance 3, subarray T = [v,a,b,v] length 4. In T, v:2, a:1, b:1 => dominated. Actually T dominated. Another attempt: S = [v, a, a, v, b] length5, v:2, a:2 -> tie, not dominated. So S not dominated. Maybe we can construct where S dominated but T not: Suppose v appears 3 times, and some other u appears 2 times, but those u's are both outside the interval between the two closest v's? Let's think: v at positions 1, 5, 6. S from 1 to 6: [v, x, y, z, v, v]. v count=3. Suppose u appears at positions 2 and 4: that's two u's. Then S: v:3, u:2, others: x,y,z? Actually positions: 1:v,2:u,3:x,4:u,5:v,6:v. So counts: v=3, u=2, x=1, y? Actually 3 is x, 4 is u, 5 v,6 v. So dominated. Now consecutive v pairs: positions 1 and 5 (distance 4) subarray [1..5] = v,u,x,u,v => v:2, u:2 => tie, not dominated. positions 5 and 6 distance1 (adjacent) -> [5,6] = v,v -> dominated, length2. So there is a shorter dominated subarray (length2) from the adjacent v's. So if there is any adjacent equal, answer 2. So in scenario where answer >2, no adjacent equal. So v's occurrences are at least distance 2. If there are three occurrences, the minimal distance between any two is at least 2. If there is distance 2 (gap2), then that gives length3 dominated. So answer would be 3. So if answer >3, then all distances are at least 3. So for answer >3, we have no adjacent equal and no gap2 equal. In such arrays, v's consecutive occurrences are at distance >=3. In the above example with v at 1,5,6, there is a distance 1 between 5 and 6 (adjacent) which would give answer 2. So that contradicts >3. So if answer >3, there can't be any adjacent or gap2. So for answer >3, all consecutive distances are >=3. In that case, consider a dominated subarray S by v. Let i and j be two consecutive occurrences of v that lie in S (there must be at least two). Since distances >=3, i and j are at least 3 apart. The subarray T = [i,j] has v count exactly 2 (since consecutive). We need to check whether T is dominated. In S, v count could be >2, and maybe some other value u appears twice within S, but those u occurrences might lie partly outside [i,j]. In T, u could appear at most twice (if both occurrences lie inside [i,j]). Since v count in T is 2, for T to be not dominated, there must exist some u with count 2 in T (i.e., both occurrences inside [i,j]). That would make tie. But in S, v count is >2 (at least 3) because otherwise if v count=2 in S, then S would be at least as large as T, and if T not dominated, S would not be dominated either (since adding extra elements outside could only increase competitors, not help). Actually if v count=2 in S and there is a u with count 2 in T (and thus in S as well, because both u occurrences are inside T which is inside S, so u count in S is at least 2), then v count=2, u count≥2, so S not dominated (since v not > u). So for S to be dominated with v count=2, there cannot be any other value with count >=2 in S. In particular, T would also be dominated because any u with count in T is ≤ its count in S, which is ≤1 if S dominated? Actually if v count=2 and S dominated, then for any u, count_S(u) ≤1. Therefore, count_T(u) ≤1 as well. So T dominated. So if v count=2 in S, then T (the consecutive pair) is dominated. If v count >2 in S, then there are at least three v's. The consecutive pair i,j might have other v's between? Actually if there are three v's, then there exist consecutive pairs with smaller distances. The minimal distance among v's would be between some consecutive pair. That distance could be less than the distance between the extreme pair used in S. If that minimal distance is d, then the subarray of length d+1 would be candidate. If d=1 or 2, answer would be 2 or 3. So if answer >3, then minimal distance for v must be >=3. But if v has at least three occurrences, then there are at least two consecutive pairs; one of them is the minimal distance. That minimal distance is at least 3, so that pair yields a subarray of length >=4. Could that subarray be not dominated? Possibly, if inner has duplicate. But then S might be dominated because extra v occurrences overcome the duplicate. However, the subarray from that minimal pair is shorter than S (since it uses only two v's). If that minimal pair's subarray is not dominated due to duplicate, then S might be dominated because it includes an extra v raising count to 3, beating duplicate's 2. But then the length of S is at least the distance from first to third occurrence, which is larger than the length of the minimal pair. However, could there be a scenario where for some value v, the minimal distance pair yields a subarray that is not dominated (due to a duplicate inside), but the next pair (first and third) yields a dominated subarray of length L, and there is no other value giving a shorter dominated subarray? And this L might be the global minimum. But note that the subarray from first to third includes the minimal pair subarray as a subset? Actually first to third includes the second occurrence in between. The length L = pos3 - pos1 + 1. Since pos2 lies between, we have L > (pos2 - pos1 + 1) which is the length of the minimal pair. So L is longer. So the global minimum could be such an L if no other value gives a dominated subarray of shorter length. So we cannot restrict only to consecutive occurrences; we may need to consider triples as well. However, maybe we can argue that if the minimal pair is not dominated, then there exists a duplicate value w that appears twice within that interval. That duplicate w might itself have a pair of occurrences (the two w's) that are closer than L? Let's explore.

    Suppose we have a value v with consecutive occurrences at p and q (p<q) where the subarray [p,q] is not dominated because there is a value w that appears twice in (p,q) (or maybe w appears twice with one at p+1 and another at q-1, etc.). Then w's two occurrences are within [p,q] and their distance is ≤ q-p-1 (since they are inside). So the distance between the two w's is at most d-1 where d = q-p. Hence, the subarray from the first w to the second w has length at most d. Since d ≥3 (as no adjacent/gap2), d-1 ≥2. But could that w-subarray be dominated? Possibly, if w appears exactly twice and all inner elements distinct. But we don't know. However, it gives us a candidate with length potentially smaller than d+1. This suggests that if the minimal v-pair fails, there might be an even shorter candidate from the duplicate w inside. So perhaps the global minimum dominated subarray will always be realized by some pair of equal elements that are consecutive in the sense that there is no other equal element between them? Actually w's pair might be consecutive occurrences of w, or not. But the minimal distance between equal elements overall is d_min. If the subarray for d_min is not dominated, it means that within that minimal interval, there is some other value that repeats (since otherwise all inner distinct would make it dominated). That repeating value gives a pair with distance less than d_min? Not necessarily less; if the repeating value appears twice within that interval, the distance between its two occurrences could be less than d_min, because d_min is the minimum over all pairs of equal elements. If that pair of w has distance < d_min, then d_min wouldn't be the minimal distance. Contradiction. Therefore, if d_min is the minimal distance between any two equal elements, then for the pair achieving d_min, there cannot be any other pair of equal elements with smaller distance. But could there be another pair with the same distance? Possibly. But if the minimal distance is d_min, then any other pair has distance >= d_min. So for the pair (p,q) with q-p = d_min, the inner elements cannot contain any other pair of equal elements with distance < d_min; but they could contain a pair with distance exactly d_min? That would also be d_min, but then that pair would also be minimal. However, could the inner contain a pair with distance exactly d_min? That would require two equal elements at positions r and s such that s-r = d_min and both lie inside (p,q). But then q-p = d_min, so the interval length is d_min, so (p,q) contains d_min+1 elements? Actually if q-p = d_min, then the number of positions between p and q is d_min-1. For there to be two equal elements inside with distance d_min, they would need to span a distance d_min, which would require them to be at the extremes p+? and q-? such that difference = d_min. But since the total span from p to q is d_min, the only way to have distance d_min inside is if the pair is exactly (p,q) themselves, but they are v, not w. So impossible. Therefore, any pair inside would have distance less than d_min. So by definition of d_min, there cannot be any pair of equal elements with distance less than d_min. Thus, for the minimal distance pair (p,q), the inner elements must all be distinct (no repeats). Because if any value appears twice inside, those two occurrences would have distance < d_min, contradicting minimality. Therefore, the subarray for the minimal distance pair is always dominated! This is a key insight.

    Let's verify: d_min is the smallest j-i such that a[i]==a[j]. For that pair, consider the open interval (i,j). If there were indices r < s in (i,j) with a[r]==a[s], then s-r < j-i = d_min, contradicting minimality. So all elements between i and j are distinct (and also not equal to a[i] because that would give an occurrence of a[i] at r, but then i and r would be a pair with distance r-i < d_min, contradiction). So indeed, for the pair achieving the global minimum distance, the inner elements are all distinct. Then the subarray from i to j has a[i] appearing twice and all other values appearing at most once. Therefore, it is dominated. And its length is d_min+1.

    Thus, the answer is simply d_min+1, where d_min is the minimum distance between two equal elements in the array, provided that d_min+1 >=2 (i.e., d_min >=1). If there is no pair of equal elements (all elements distinct), then there is no dominated subarray because any subarray of length >=2 would have all distinct? Actually if all elements are distinct, then any subarray of length >=2 has all frequencies 1, so no unique maximum (they are all equal). So not dominated. So answer -1.

    But wait, what about arrays where the minimal distance is 1 (adjacent equal)? Then the subarray of length 2 is dominated, as we know. That matches d_min+1=2. If minimal distance is 2, subarray length 3 is dominated, as we argued. If minimal distance is 3, subarray length 4 is dominated because inner distinct (by minimality argument). Indeed, because if minimal distance is 3, then there is no equal pair with distance 1 or 2. So the inner elements (two of them) must be distinct and also not equal to the endpoints (otherwise would create a pair with distance 1 or 2). So [x,a,b,x] with a≠b and a≠x,b≠x. So dominated. So answer = d_min+1.

    But is it always true that the subarray corresponding to the minimal distance is dominated? We argued that due to minimality, there cannot be any repeated value inside, because that would give a smaller distance. That holds. However, we must also consider the possibility that the dominating element could be not the one at the endpoints, but some other element that appears more than twice? But the subarray we get from the minimal distance pair is guaranteed to be dominated by that endpoint value. So that gives a valid dominated subarray. So the shortest possible length is at most d_min+1. Could there be an even shorter dominated subarray? No, because any dominated subarray must contain at least two equal elements, and the distance between those two equal elements is at least d_min. Therefore, its length is at least d_min+1 (since it must span at least from the first to the last of those two equal elements). So the minimal length of any dominated subarray is exactly d_min+1. Wait, careful: A dominated subarray could have its two equal elements not be the ones achieving the global minimal distance. But then the distance between those two equal elements is at least d_min, so the subarray length is at least that distance+1, which is ≥ d_min+1. So d_min+1 is a lower bound. And we have exhibited a dominated subarray of length d_min+1 (the one from the minimal distance pair). So the answer is exactly d_min+1, provided d_min exists (i.e., there is at least one pair of equal elements). If no pair exists, answer -1.

    Thus, the problem reduces to: For each test case, compute the minimum distance between any two equal elements in the array. If none, output -1; else output min_distance+1.

    But is there any corner case where the subarray from the minimal distance pair is not dominated because the dominating element might not be the endpoint value? It is dominated by that endpoint value as argued. Let's double-check condition: For subarray [i,j] with a[i]==a[j]=x, and all other elements in between are distinct and not equal to x. Then occ(x)=2, any other element appears at most 1. So x is the unique maximum. So dominated. Good.

    What about the case where the minimal distance is 0? Not possible.

    Thus, the answer is simply: find the smallest gap between two equal numbers.

    Now, we need to ensure this holds even if there is a scenario where the minimal distance pair yields a subarray that is not dominated because the inner elements might contain an element equal to x? But if inner contains x, then there would be another occurrence of x between i and j, making i and that occurrence a pair with smaller distance (< j-i). That would contradict minimality. So cannot happen. Similarly, if inner elements contain a duplicate (two equal non-x), that would be a pair with distance less than j-i, also contradiction. So safe.

    Therefore, the problem is simpler than originally thought. The answer is min distance + 1, or -1 if all distinct.

    We should verify with provided examples:

    Example 1: n=1, array [1] -> no equal pair -> -1. matches.

    Example 2: n=6, [1 2 3 4 5 1]. Equal pairs: 1 at positions 1 and 6, distance 5. Also any other? Only that pair. So min distance =5 -> answer =6. matches.

    Example 3: n=9, [4 1 2 4 5 4 3 2 1]. Let's compute equal pairs:

    • 4 at positions 1,4,6. Distances: 4-1=3, 6-4=2, 6-1=5 -> min=2? Actually 6-4=2. Also 2 at positions 3 and 8: distance 5. 1 at positions 2 and 9: distance 7. So minimal distance is 2 (positions 4 and 6). Answer = 2+1=3. matches.

    Example 4: [3 3 3 3]. Equal pairs: many. Minimal distance =1 (adjacent). Answer=2. matches.

    So this fits all examples.

    Thus, solution: For each test case, iterate through array, for each value record its last occurrence index. Keep a variable min_gap = INF. For i from 1 to n, if value a[i] has been seen before, compute i - last_occurrence[a[i]], update min_gap = min(min_gap, i - last[val]), then update last[val] = i. At the end, if min_gap == INF, output -1 else output min_gap+1.

    Time O(n) per test, total O(total n). Memory O(n) for last occurrences, but we can use array of size n+1 (since a_i ≤ n) and resetting for each test case efficiently. Since total n across tests ≤ 2e5, we can allocate a global array of size 200001 and for each test, we need to clear only the used indices (the values that appear). Since we only set last[val] when we encounter, we can store the values that appear in a list and after test, set last[val]=0 for those values. Or we can use a map but array is fine as n ≤ 2e5 per array and a_i ≤ n, but the values across tests may be up to the max n of that test, which could be as high as 2e5. Since total n across tests is 2e5, we can allocate array of size 200001 and for each test, we can simply iterate over the used values to reset them. However, that might be O(n) extra which is fine. Simpler: use unordered_map or map? But array is faster. Since a_i ≤ n and sum n ≤ 2e5, we can just allocate array of size (max_n+1) per test; but max_n may vary, but we can allocate global array of size 200001 and for each test, we iterate over indices of a and set last[val] to -1 before processing? That would be O(n) to reset but that's okay as we already O(n). Actually we can do: initialize last array with -1 for all indices from 1 to n (or up to the max value in that test). Since n ≤ 2e5, we can set last[1..n] = 0 or -1 for each test, which is O(n) which is fine because we already reading the array of length n. So overall O(n) per test, total O(total n). So we can do:

    for each test: read n, read array a (1-indexed) initialize vector last(n+1, -1) // since a_i ≤ n min_gap = INF for i from 0 to n-1: val = a[i] if last[val] != -1: gap = i - last[val] // since last[val] stores previous index (0-based) min_gap = min(min_gap, gap) last[val] = i if min_gap == INF: print -1 else: print min_gap+1

    But careful: The distance between positions i and j is j - i. The subarray length is j - i + 1. So answer = min_gap + 1.

    Now we need to confirm this works for edge cases. Let's test with array where there are multiple equal pairs, e.g., [1,2,1,2,1]. The equal pairs: 1 at (0,2): gap=2; (2,4): gap=2; (0,4): gap=4. So min_gap=2, answer=3. Is there a dominated subarray of length 3? Yes, [1,2,1] or [2,1,2] etc. Good.

    Now test with array where minimal distance is 3 but inner has duplicate? As argued, that can't happen because duplicate would give a smaller distance. Let's try to construct a counterexample: Suppose we try to have a minimal distance 3 but inner duplicate: e.g., positions 1 and 4 equal (gap 3). Inside positions 2 and 3, we want them equal to each other, say both 'y'. Then we have y at positions 2 and 3, distance 1, which is smaller than 3. So min_gap would be 1, not 3. So indeed minimal distance would be 1. So the pair with gap 3 wouldn't be minimal. So our algorithm picks the smallest gap, which is 1, answer 2. So correct.

    Thus, the solution is indeed to find the minimum distance between two equal elements.

    Now, we must confirm that the condition "dominated subarray must have at least 2 elements" is satisfied automatically because min_gap >=1 yields length at least 2.

    Also, note that if min_gap = 0? Not possible.

    One more subtlety: What if the minimal distance is from a pair that are not the closest occurrences of that value? That's fine; we track all pairs, but by iterating and updating last occurrence, we compute the distance to the most recent previous occurrence. This gives the minimal distance for that value considering only consecutive occurrences? Actually, when we compute gap = i - last[val], we are computing distance to the previous occurrence. This may not be the global minimal distance for that value if there is a smaller distance that is not between consecutive occurrences? But for a given value, the minimal distance is always achieved by some consecutive occurrences, because if you have positions p<q<r, then q-p and r-q are smaller than r-p. So the minimal distance for that value is the minimum over consecutive differences. And our method of comparing with the last occurrence (which is the most recent previous) will capture the distance to the immediately preceding occurrence for each i. As we iterate, we will compute all consecutive gaps. So min_gap across all values will be the global minimum distance between any two equal elements. Good.

    Thus, solution is straightforward.

    Now, double-check with a custom test: array [1,2,3,1,2]. Equal pairs: 1: positions 0 and 3 gap=3; 2: positions 1 and 4 gap=3. min_gap=3, answer=4. Is there a dominated subarray of length 4? Let's check subarray from index 0 to 3: [1,2,3,1] -> counts: 1:2, 2:1, 3:1 => dominated. So answer 4. And there is no shorter dominated subarray because no adjacent equal and no gap2 equal (check: any pair distance 2? 0-2:1 vs 3? no; 1-3:2 vs1? no; 2-4:3 vs2? no). So correct.

    Another: [1,2,3,4,5,6,1] min_gap=6 answer=7. Subarray length 7 dominated. Is there any shorter? Possibly a different value? None. So answer 7.

    Now, check if there could be a dominated subarray where the dominating element appears more than twice and the subarray length is less than min_gap+1? Not possible because any such subarray must contain at least two equal elements, and distance between those two is at least min_gap, so length at least min_gap+1.

    Thus, algorithm stands.

    Edge case: n=2, array [1,2] -> no equal pair, min_gap INF -> answer -1. Indeed [1,2] not dominated.

    n=2, array [1,1] -> min_gap=1 -> answer=2. dominated.

    n=3, array [1,2,2] -> equal pair at positions 2 and 3? Actually indices: 1:1, 2:2, 3:2 => equal pair (2,3) gap=1 => answer=2. But is there a dominated subarray of length 2? [2,2] yes. So answer 2, not 3. Good.

    Now, we should also consider if there could be a dominated subarray where the two equal elements are not the same as the ones giving min_gap, but the length could still be min_gap+1 if the min_gap is from some other pair? But we already have a candidate of that length, so answer is min_gap+1.

    Thus, the solution is O(n) per test.

    Implementation details:

    • Use 0-indexing for simplicity.
    • For each test, we need to reset last occurrence array for values that appear. Since a_i ≤ n, we can allocate vector last(n+1, -1) each test. That's O(n) memory per test, but total memory across tests not an issue as each test we allocate new vector and it gets freed after test. But with T up to 1000 and total n 2e5, allocating many vectors of size up to n each time might sum to O(∑ n) memory allocation, which is okay. Alternatively, we can use a global array of size max_n (200000+5) and use a timestamp technique to avoid resetting entire array. But given constraints, resetting a vector of size n per test is acceptable because total sum of n is 2e5, so total allocated size across tests (if we allocate fresh each test) might be sum of n_i, which is 2e5, but actually each vector is allocated and then deallocated; the peak might be the maximum n_i across tests, up to 2e5. So memory fine.

    Thus, implement as:

    int T; cin >> T; while (T--) { int n; cin >> n; vector a(n); for (int i=0;i<n;i++) cin >> a[i]; const int INF = n+5; // or large int min_gap = INF; vector last(n+1, -1); // values from 1..n for (int i=0;i<n;i++) { int val = a[i]; if (last[val] != -1) { min_gap = min(min_gap, i - last[val]); } last[val] = i; } if (min_gap == INF) cout << -1 << endl; else cout << min_gap + 1 << endl; }

    But careful: a_i can be up to n, but if n is up to 2e5, that's fine. However, for test where n=1, we allocate last size 2, that's fine.

    Now, think about correctness for large values: a_i <= n, so index range okay.

    Now, let's double-check with a tricky case: Suppose array [1,2,1,2,1] as before. The pairs: (0,2) gap=2, (2,4) gap=2, (0,4) gap=4. Our method: at i=0, last[1]= -1; set last[1]=0. i=1: last[2]=-1; set last[2]=1. i=2: val=1, last[1]=0 -> gap=2, min_gap=2; set last[1]=2. i=3: val=2, last[2]=1 -> gap=2, min_gap stays 2; set last[2]=3. i=4: val=1, last[1]=2 -> gap=2, min_gap still 2. So answer=3. Good.

    Now, test where there is a value that appears many times but the minimal gap is from non-consecutive? Not possible as argued. So ok.

    Thus, solution is very simple.

    But wait: Is it always true that the subarray from the minimal distance pair is dominated? Let's consider a case where the minimal distance is 1: [x,x] obviously dominated. Distance 2: [x,y,x] dominated because x appears twice, y once, and y distinct from x. However, what if the array is like [x, x, y]? That's distance 1 (adjacent) so min_gap=1, answer=2. So fine.

    Now consider distance 3: as argued, inner distinct due to minimality, so dominated. So yes.

    Thus, answer is always min_gap+1.

    But we need to be absolutely certain that there is no scenario where min_gap exists but the subarray of that length is not dominated because maybe the two equal elements are not the ones that dominate? For example, suppose we have a pair (i,j) with minimal distance d, but the dominating element in that subarray might be some other element that appears more times? But we only need existence of any dominated subarray of that length, not necessarily that the same element that gave the pair dominates. Actually the pair (i,j) itself gives a candidate subarray. We claim that subarray is dominated. So it's enough. Even if some other element appears more than twice within that subarray, could it prevent domination? But we argued that can't happen because that would create a smaller pair. Let's rigorously prove: In subarray S = [i,j] with a[i]=a[j]=v and j-i = d_min. Suppose there is some other element u that appears at least twice in S. Then there exist indices r,s in [i,j] with u at r and s, r<s. Since r,s are in the interval, s-r ≤ j-i = d_min. If s-r < d_min, that contradicts d_min being the minimal distance between any equal elements (since u is equal). If s-r = d_min, then we have r = i and s = j? Because the only way to have distance exactly d_min within [i,j] is to be at the boundaries. But r and s are for u, not v. Could r = i and s = j? That would mean u = v (since a[i]=v, a[r]=u => if r=i then u=v). Then u=v, but then we would have v appearing at i and j and also at r=i (same) and s=j (same) — that's the same pair. But u is v? Actually if u=v then the pair is the same as the v pair, not a different element. So if u is different, then r and s cannot be exactly i and j because those positions hold v, not u. So s-r < d_min. Therefore, contradiction. So u cannot appear twice. Also, can any element appear more than twice? That would imply at least two pairs, each with distance ≤ d_min, and at least one would be < d_min. So impossible. Thus, all other elements appear at most once. Also, v appears exactly twice? Could v appear more than twice? If there is a third occurrence of v in (i,j), then there would be a pair of v with distance less than d_min (between i and that occurrence), contradiction. So v appears exactly twice. Therefore, S is dominated by v. So proof holds.

    Hence, solution is correct.

    Now implement accordingly.

    Potential pitfalls: Since a_i can be up to n, but n can be as high as 2e5, allocate vector of size n+1. For each test, n may be up to 2e5, and sum of n across tests ≤ 2e5, so overall time O(total n).

    Double-check sample input:

    4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3

    Our algorithm yields: Test1: no pair -> -1 Test2: pair 1 at positions 0 and 5 gap=5 -> 6 Test3: we compute min_gap: i=0 val4 last[-1] -> last4=0 i=1 val1 -> last1= -1 -> last1=1 i=2 val2 -> last2=-1 -> last2=2 i=3 val4 -> last4=0 -> gap=3 -> min=3 -> last4=3 i=4 val5 -> last5=-1 -> last5=4 i=5 val4 -> last4=3 -> gap=2 -> min=2 -> last4=5 i=6 val3 -> last3=-1 -> last3=6 i=7 val2 -> last2=2 -> gap=5 -> min stays2 -> last2=7 i=8 val1 -> last1=1 -> gap=7 -> min stays2 -> last1=8 answer = 3. Test4: positions: 0:3 last3=-1 -> last3=0; 1:3 last3=0 gap=1 min=1; etc. answer=2.

    Matches.

    Thus, solution is straightforward.

    Now we can produce final answer.

    I will write the solution in C++.

    But caution: The problem statement says "1 ≤ a_i ≤ n", so values are within range, good.

    Edge case: n=0? Not allowed.

    Time complexity: O(∑ n). Memory: O(max n) per test.

    Implementation details:

    • Use fast I/O (ios_base::sync_with_stdio(false); cin.tie(NULL);) because T up to 1000 and total input up to 2e5, but it's fine.

    Potential improvement: For each test, we could also compute answer by scanning and using map for values if a_i could be larger, but it's fine.

    Now, I'll produce final solution.

    But wait, could there be a scenario where the array has a dominated subarray but no two equal elements? Consider length 2 with distinct elements: [1,2] not dominated. Length 3 all distinct: not dominated. Length 4 all distinct: all frequencies 1 -> no unique max. So indeed, a dominated subarray must have at least one element appearing at least twice. So our condition that there must be a pair of equal elements is necessary. So answer -1 when no equal pairs.

    Thus final.

    Now produce final answer. ```cpp #include <bits/stdc++.h> using namespace std;

    int main() { ios::sync_with_stdio(false); cin.tie(nullptr);

    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; ++i) cin >> a[i];
    
        const int INF = n + 5;
        int min_gap = INF;
        vector<int> last(n + 1, -1);   // values are in [1, n]
    
        for (int i = 0; i < n; ++i) {
            int x = a[i];
            if (last[x] != -1) {
                min_gap = min(min_gap, i - last[x]);
            }
            last[x] = i;
        }
    
        if (min_gap == INF) cout << "-1\n";
        else cout << min_gap + 1 << '\n';
    }
    
    return 0;
    

    }