You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is -1.
For each k from 1 to n calculate the k-amazing number of the array a.
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 ≤ n ≤ 3 ⋅ 10^5) — the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3 ⋅ 10^5.
Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve problem: For each k from 1 to n, find the minimum number that appears in every subsegment of length k. If no such number exists, output -1.
Observations:
Given array a of length n. For each value x (1..n), we can consider its positions in the array. Let pos[x] = list of indices where a_i = x.
We need to know for which k does x occur in every subsegment of length k? That condition is: The maximum gap between consecutive occurrences of x (including before first and after last) must be <= k. Because consider any subsegment of length k; it will contain x iff the gap between occurrences is at most k, i.e., there is no interval of length k with no x. More precisely, to ensure that every length-k subsegment contains at least one x, the distance between consecutive occurrences (including from start to first occurrence and from last occurrence to end+1) must be at most k. Actually the condition: For any starting index i, the subsegment [i, i+k-1] must contain x. Equivalent to: For all i, there exists j in [i, i+k-1] with a_j = x. So the gaps where x is absent cannot be longer than k-1? Let's define gaps properly.
Let positions where x occurs: p1 < p2 < ... < pm. Consider the intervals without x: before p1, after pm, and between pi and p_{i+1}. The longest interval without x (in terms of consecutive indices with no x) is: max( p1 - 1, n - pm, p_{i+1} - p_i - 1 for i). Actually the length of a maximal consecutive block of indices not containing x. For a subsegment of length k to always contain x, we need that any interval of length k contains at least one x. So the maximal length of a consecutive set of indices with no x must be < k. If there is a block of indices of length k without x, then the subsegment consisting exactly of that block would not contain x. So we need that the maximum gap (of indices without x) is less than k. In terms of occurrences, the gaps between positions: distance from start to first occurrence: p1 - 1 (count of indices before first occurrence). Similarly, after last: n - pm. And between pi and p_{i+1}: p_{i+1} - pi - 1. So define gap_max(x) = maximum of these numbers. Then for k > gap_max(x), any segment of length k will cover at least one occurrence of x. Actually careful: If gap_max(x) = g, that means there exists a contiguous block of length g with no x. For any k > g, a segment of length k cannot be entirely inside that block because the block is only length g. However, if k = g+1, then any segment of length k will always include at least one x? Not exactly: consider a block of length g without x; then a segment that exactly matches that block would have length g and contain no x. So for k = g+1, the segment could start at the beginning of the block and end one element after the block; that extended segment might include x if the element after the block is x. But could there be a segment of length g+1 that lies entirely within the block? The block length is g, so any segment longer than g must extend beyond the block, thus might include x at the boundaries. But we need to ensure that every possible segment of length k contains at least one x. If there is a gap of length g (no x), then there exists a segment of length g that lies entirely in that gap, which fails for k = g. For k = g+1, can we find a segment of length g+1 that contains no x? Possibly if the gap is at the boundaries? Consider the start: indices 1..p1-1 are a gap of length L = p1-1. For k > L, a segment starting at 1 and length k will extend into the region that includes p1 (since it starts at 1 and goes to k). But could there be a segment that starts at index 2? Then it goes from 2 to k+1. If k+1 <= p1-1? That would require k <= p1-2. So if k > L, then any segment that starts at or before the gap might still be contained entirely within the gap? Actually if gap length L, the largest segment that can be entirely inside the gap is of length L. For any length > L, any segment of that length that is placed such that it is entirely within the gap is impossible because the gap is only length L. However, a segment could start before the gap and end after the gap, but that's okay because it will contain x at the boundaries? Wait, it may contain x if it includes the occurrence at the edges. But what if the gap is in the middle? For a gap of length L between p_i and p_{i+1}, any segment of length > L that tries to stay within the gap would be impossible; the segment must extend beyond the gap, thus includes either p_i or p_{i+1} if it extends enough. However, consider a segment that starts after p_i but before the gap? Actually the gap is exactly the set of indices with no x. Suppose we have two occurrences at positions 2 and 10, so gap length = 10-2-1 = 7. For k=8, could we find a segment of length 8 that contains no x? That would require a block of 8 consecutive indices without x. But the maximum block is 7, so no. So indeed, condition: x appears in every subsegment of length k iff k > gap_max(x). But also need to consider k such that the array length is exactly k? But condition should hold for all subsegments of that length. So for k > gap_max(x), it's satisfied. For k = gap_max(x), there exists a gap of that length, so a segment exactly covering that gap (which length = gap_max) would contain no x, so fails. So x appears in all subsegments of length k if and only if k > gap_max(x). Additionally, if x does not appear at all? But values a_i are between 1 and n. There might be values that never appear? Since a_i are from 1 to n, some numbers may be missing. For those numbers, gap_max would be n (since from start to end no occurrence? Actually if x never appears, then the whole array is a gap of length n. So gap_max(x) = n. Then condition k > n, which never happens for k≤n, so they never become amazing numbers. So we can ignore missing numbers.
Thus, for each value x, we can compute the minimal k for which x is amazing: k_min(x) = gap_max(x) + 1. For any k >= k_min(x), x will appear in all subsegments of length k? Let's verify: If k > gap_max(x), condition holds. But what about k larger than gap_max(x)+1? Yes, if k is larger, then any segment of length k definitely contains at least one x because even larger segments are harder to avoid x? Actually if you increase k, it becomes easier to include an occurrence? Wait: The condition is that every segment of length k must contain x. As k increases, the segments become longer, covering more of the array. It could become more difficult because you need x in all longer segments? Consider a scenario: x occurs only once at position 1. Then gap_max(x) = max(p1-1, n-p1) = max(0, n-1) = n-1. So k_min(x) = n. For k=n, the only segment of length n is the whole array, which contains x (since x at pos1). So that works. For k=n-1, there are two segments: [1,n-1] and [2,n]. The segment [2,n] does not contain x (since only at 1), so fails. So indeed, k must be >= n. So larger k makes condition easier? Actually as k increases, the number of segments decreases, and the segments cover more of the array; but they might also include boundaries that miss the occurrence. In this example, for k=n-1, one segment misses x; for k=n, the only segment includes x. So condition can be satisfied for larger k but not for smaller k. So monotonic: if x appears in all subsegments of length k, does it also appear for length k+1? Not necessarily? Let's think: Suppose we have x that appears frequently. For small k, it's easy to have all segments contain x? Actually for very small k (e.g., k=1), condition is that every element is x, which is false unless array is all x. So as k increases, the condition becomes easier? Wait: For k=1, every subsegment of length 1 is just a single element. For x to be in all of them, every element must equal x. So condition is very strict. For larger k, we are requiring that any block of length k contains at least one x. That's less strict because you only need at least one x per block. So the condition becomes easier as k increases. So if x appears in all subsegments of length k, then for any larger k' > k, does it also hold? Not necessarily: Could there be a case where every block of length k has an x, but some block of length k+1 lacks x? Let's try to find counterexample. Suppose array: [x, not x, x]. n=3. For k=2, subsegments: [1,2] contains x, [2,3] contains x. So condition holds for k=2. For k=3, the only subsegment is whole array, contains x, so holds. So it holds. Need to see if condition can fail for larger k but hold for smaller k. Consider array: [x, not x, not x, x]. n=4. For k=2: subsegments: [1,2] (x), [2,3] (no x), fails. So not that. For k=3: subsegments: [1,3] (x), [2,4] (x), holds. So condition holds for k=3 but not for k=2. That's increasing k makes it hold. So if it holds for k=3, does it hold for k=4? k=4 whole array contains x, holds. So seems monotonic: once it holds for some k, it holds for all larger k. Is it always true? Suppose it holds for k, i.e., any block of length k contains x. Then consider a block of length k+1. Any such block can be partitioned into a block of length k (starting at same index) and an extra element. Since the block of length k contains x, the block of length k+1 also contains x. However, careful: The block of length k+1 is not exactly a superset of a length-k block? For block [i, i+k], it contains the subblock [i, i+k-1] (length k) which by hypothesis contains x. So yes, if all length-k blocks contain x, then all length-(k+1) blocks contain x as they contain a length-k subblock. Therefore the property is monotonic non-decreasing: if x is k-amazing, then it is also (k+1)-amazing. So for each x, there is a threshold k0 = gap_max(x) + 1 such that for all k >= k0, x is amazing; for k < k0, it is not. And k0 can be > n? If gap_max(x) >= n, then k0 > n, meaning x is never amazing for k ≤ n. That's fine.
Now, we need for each k from 1 to n, the minimum x (value) that is amazing at that k. Since multiple values might be amazing at that k, we want the minimum number (value) among those.
Thus, we can compute for each value x, its threshold k0(x) = gap_max(x) + 1, provided x appears at least once. If k0(x) <= n, then for all k >= k0(x), x is amazing. Then for each k, answer ans[k] = min{ x | k0(x) <= k }. If no such x, ans[k] = -1.
This is like: we have thresholds, and we want for each k the minimum value among those whose threshold <= k. So we can process values sorted by x, and maintain the minimum threshold? Actually we want for each k the minimum x that qualifies. Since x's are numbers 1..n, we can iterate x from 1 to n, compute k0(x). Then we need to fill ans[k] = min over x with k0(x) <= k. Equivalent: for each k, we can consider all x with k0(x) <= k and take min x. But we can compute answer array by scanning k from 1 to n and maintaining the smallest x whose threshold <= current k. But thresholds are static, so we can sort events: for each x, at k = k0(x), x becomes eligible. Then ans[k] = min of all eligible x up to k. Since we need minimum x, we can keep a variable current_min = INF, and for k from 1 to n, add any x that become eligible at this k (i.e., k0(x) == k), update current_min = min(current_min, x). Then ans[k] = current_min if current_min != INF else -1. But careful: what about x whose k0(x) = 0? That would mean gap_max(x) = -1, impossible; minimum k is 1? For gap_max = 0 (meaning no gaps? Actually if x appears in every position? Then gap_max = 0? Let's compute: positions: all indices. Then p1=1, pm=n. Gaps: before first: 0, between consecutive: 0 (since p_{i+1}-p_i=1 => gap 0), after last: 0. So gap_max=0, so k0=1. So x is amazing for all k>=1. So that's fine.
Also note that k0(x) could be 1, meaning x appears in all length-1 subsegments => array is all x. But also if gap_max=0, that yields k0=1. So we treat as event at k=1.
Thus algorithm:
For each test case:
Input n, array a[1..n].
For each value v from 1..n, initialize last occurrence index = 0, and also store list of positions or just compute maximum gap. Since we need gap_max, we can iterate through array and for each value track previous occurrence. Also need to compute gap before first and after last. So we can:
Iterate i from 1..n:
val = a[i]
if first[val] == -1: first[val] = i
else: update max_gap[val] = max(max_gap[val], i - last[val] - 1) // gap between consecutive occurrences
last[val] = i
After loop, for each v with first[v]!=-1:
max_gap[v] = max(max_gap[v], first[v] - 1) // before first
max_gap[v] = max(max_gap[v], n - last[v]) // after last
k0 = max_gap[v] + 1
// store event at k0: v becomes eligible
For v that never appear, ignore.
But careful: For values that appear, we need to compute gaps correctly. The gap between consecutive occurrences: difference in indices minus 1 (the count of positions strictly between). Then gap before first is first-1, after last is n-last. The max of these is the maximum length of a contiguous segment without v.
We also need to consider that if a value appears only once, then max_gap becomes max(first-1, n-last). The gap between consecutive is not applicable (since no two). So that's fine.
Now we have events: for each value v that appears, at k = k0(v) (if k0 <= n), we want to consider v as candidate. But note: It could be that multiple values have same k0. We'll just keep a list of values for each k, or update a min candidate per k? Since we need for each k the minimum eligible v, we can maintain an array best[k] initially INF. For each v, if k0 <= n, set best[k0] = min(best[k0], v). But careful: Even if multiple values become eligible at the same k, we only need the minimum among them. However, there is also the possibility that a value becomes eligible at a certain k, but for larger k, the minimum among eligible values might be that same v or a smaller v that became eligible earlier. So we can do:
Initialize ans array of length n+1 (1-indexed) with INF.
For each v, if k0 <= n: ans_at_k0 = min(ans_at_k0, v) ??? Actually we need to fill ans[k] = minimum v with k0(v) <= k. That's not the same as just taking min at exact k. But we can process as described: let events[k] store values that become eligible at k (maybe just the v). Then we iterate k from 1 to n, maintain current_min = INF. For each k, we look at events[k], update current_min = min(current_min, min among those v). Then ans[k] = current_min if current_min != INF else -1.
But we don't need to store all v in events; we can store the minimum v for each k among those with k0=k. Because if we have two values becoming eligible at same k, only the smallest matters for future minima? Actually we need to maintain the overall minimum among all eligible so far. So we can store for each k, the minimum v among those whose threshold is exactly k. Let's call add[k] = INF initially. For each v, if k0 <= n, add[k0] = min(add[k0], v). Then we iterate k from 1 to n: cur_min = min(cur_min, add[k]); ans[k] = cur_min if cur_min != INF else -1.
But is this correct? Suppose we have v1=2 with k0=3, v2=1 with k0=5. At k=3, cur_min becomes min(INF, add[3]=2) => 2, ans[3]=2. At k=4, cur_min stays 2, ans[4]=2. At k=5, add[5]=1, cur_min becomes min(2,1)=1, ans[5]=1. That matches: at k=5, both 2 and 1 are eligible, min is 1. So correct.
Now we must ensure that if there are values that never appear, they are not considered. Also, if no value has k0 <= k, answer is -1.
Example 2:
n=5, a=[4,4,4,4,2]
Values: v=4 positions 1,2,3,4. Compute gaps: consecutive gaps 0; before first:0; after last: n - last = 5-4=1 => max_gap=1 => k0=2.
v=2 position 5. gaps: before first: 4; after last:0; max_gap=4 => k0=5.
So events: k0=2: v=4; k0=5: v=2.
Then:
k=1: -1
k=2: cur_min=4 => ans2=4
k=3: cur_min=4 => ans3=4
k=4: cur_min=4 => ans4=4
k=5: cur_min=min(4,2)=2 => ans5=2
Output: -1 4 4 4 2 matches.
Example 3:
n=6, a=[1,3,1,5,3,1]
Compute:
v=1: positions 1,3,6. Gaps: between 1&3: gap=1 (index2) => length=1; between 3&6: gap=2 (indices4,5) => length=2. Before first:0, after last:0 (since last=6). max_gap = max(1,2,0,0) = 2 => k0=3.
v=3: positions 2,5. Gaps: between: indices3,4? Actually positions 2 and 5: gap = 5-2-1=2 (indices3,4) length 2; before first:1; after last: n - 5 =1; max_gap = max(2,1,1)=2 => k0=3.
v=5: positions 4. gaps: before=3, after=2 => max_gap=3? Actually first=4, last=4: before = 3 (indices1-3), after = 2 (indices5-6) => max_gap=3 => k0=4.
Also v=2,4,6 missing.
Events:
k0=3: v=1 and v=3 -> min=1
k0=4: v=5 -> min=5
Now process:
k=1: -1
k=2: -1
k=3: cur_min=1 => ans3=1
k=4: cur_min=min(1,5)=1 => ans4=1
k=5: cur_min=1 => ans5=1? Wait expected output for example: -1 -1 1 1 1 1. So for k=5 and k=6, answer should be 1. Our algorithm gives: at k=5, cur_min still 1, ans5=1; k=6, ans6=1. That matches. But we need to check: are there any other values with k0 <=5? v=1 and v=3 have k0=3, v=5 has k0=4, so yes. So min is 1. Good.
So algorithm seems correct.
Now we need to implement efficiently for total n up to 3e5 across test cases. We'll allocate arrays of size n+2 per test case (values from 1 to n). For each test case, we do:
read n, array a.
initialize arrays: first = vector(n+1, -1), last = vector(n+1, -1), max_gap = vector(n+1, 0) or maybe compute on fly.
loop i=1..n:
val = a[i-1] (if 0-index)
if first[val] == -1: first[val] = i
else: max_gap[val] = max(max_gap[val], i - last[val] - 1)
last[val] = i
after loop:
for v in 1..n:
if first[v] != -1:
// compute final gaps
max_gap[v] = max(max_gap[v], first[v] - 1)
max_gap[v] = max(max_gap[v], n - last[v])
k0 = max_gap[v] + 1
if k0 <= n:
add[k0] = min(add[k0], v)
else: skip.
But we also need to consider values that appear only once: our loop inside would not update max_gap from consecutive gaps because we only update when we see a second occurrence. That's fine; max_gap initially 0, then we update with before and after.
Potential issues: The gap between consecutive occurrences is computed using last. For each occurrence after the first, we compute gap = i - last[val] - 1? Wait, last[val] is the previous occurrence index. At point i, we have a new occurrence. The indices without val between last[val] and i are those from last[val]+1 to i-1, count = i - last[val] - 1. So yes we update max_gap[val] = max(max_gap[val], i - last[val] - 1). That works.
Now we also need to handle the gap before first and after last. After processing all, we compute those. But careful: If a value appears only once, then we have not updated max_gap from consecutive gaps, so max_gap remains 0. Then we set before = first-1, after = n-last, take max. That's correct.
Edge Cases:
n=1. Example: a=[1]. Then v=1: positions [1]; gaps: before=0, after=0, max_gap=0 => k0=1. So add[1] = 1. Then for k=1, ans=1. So output "1". That matches expectation? Let's test: For k=1, subsegments of length 1 are just [1], contains 1, so minimum number that occurs in all subsegments is 1. So correct.
If array has all same number, say all 2. n=5, a=[2,2,2,2,2]. v=2: positions all; gaps: before=0, after=0, consecutive gaps: 0, max_gap=0 => k0=1. So add[1]=2. So ans[1..5] = 2,2,2,2,2. But is that correct? For k=1, every subsegment of length 1 is a single element, all are 2, so 2 occurs in all, and it's the minimum (only 2). For larger k, obviously 2 appears in all subsegments. So output 2 2 2 2 2.
If array has no number that appears in all subsegments for some k? But according to our logic, for k large enough, there might be at least one? Not always: consider n=2, a=[1,2]. Compute: v=1: positions [1], gaps: before=0, after=1 => max_gap=1 => k0=2; v=2: pos[2], before=1, after=0 => max_gap=1 => k0=2. So add[2]= min(v with k0=2) = min(1,2)=1? Actually we store add[2]=1 (since v=1 k0=2, v=2 k0=2, min is 1). Then process: k=1: -1; k=2: cur_min = min(INF, add[2]=1) =>1, ans[2]=1. Check: For k=2, the only subsegment is whole array, contains both 1 and 2. So both 1 and 2 occur in it. Minimum is 1. So correct. For k=1, there is no number that appears in both subsegments [1] and [2] because they are different. So -1 correct.
Consider array where some number has very large k0 > n, e.g., value appears only once at position 1 in n=5. Then gap_max = max(0,4)=4 => k0=5, within n. Actually max gap = max(0, n-1) = n-1, so k0 = n. So it's within n. If value appears only once at position i, gap_max = max(i-1, n-i). This is at most n-1, so k0 <= n. So all appearing values have k0 <= n. Only missing values have gap_max = n, k0 = n+1 > n. So all appearing values will be considered at some k0 <= n. So there will always be at least one appearing value? Yes, array has n numbers, each between 1 and n, so some values appear, some may not. But at least one value appears. However, it's possible that for k=1, no value qualifies if not all elements are same. But for k=n, all values that appear at least once? Actually for k=n, the only subsegment is whole array, contains all values that appear at least once? Wait, the whole array contains all elements, so any value that appears in the array appears in that subsegment. So for k=n, every value that appears is present in the whole array. So the condition "occurs in all subsegments of length k" for k=n means: The value must occur in the whole array (only one subsegment). Since the whole array contains the value, yes. So for k=n, any value that appears at least once qualifies. So the answer at k=n should be the minimum value that appears in the array. That matches our algorithm: for each appearing v, k0(v) <= n, so at k=n, the min among all appearing values is the answer. Indeed, check with examples: Example1: min appearing is 1, answer at k=5 is 1. Example2: min appearing is 2? Actually values: 4 and 2, min is 2, answer at k=5 is 2. Example3: values: 1,3,5, min=1, answer at k=6 is 1. So correct.
Now, we need to output for each k from 1 to n, space-separated. Ensure to handle multiple test cases.
Time Complexity: For each test case, O(n) time to compute gaps and then O(n) to fill ans. Memory: O(n) arrays.
Implementation details:
We need to be careful about large t and sum n up to 3e5. Use fast I/O.
We'll store first, last, max_gap as vectors of ints with size n+1.
But we could also compute k0 without storing all three arrays per value, by using a single pass and updating max_gap as described, but we need to know first and last for each value, so we need arrays.
Alternatively, we can create an array of vectors of positions, but that would be O(n) memory anyway but maybe more overhead. Simpler: use arrays.
Plan:
For each test case:
Read n.
Create vector a(n);
for i in 0..n-1: read a[i].
for i from 0 to n-1:
int x = a[i];
int idx = i+1; // 1-indexed for convenience
if (first[x] == -1) {
first[x] = idx;
} else {
int gap = idx - last[x] - 1;
if (gap > max_gap[x]) max_gap[x] = gap;
}
last[x] = idx;
Now after loop:
vector add(n+2, INF); // indices 1..n, maybe also n+1 but we use <=n
const int INF = n+5; // since values up to n
for int v = 1; v <= n; ++v:
if (first[v] != -1) {
// update max_gap with before and after
int before_gap = first[v] - 1;
if (before_gap > max_gap[v]) max_gap[v] = before_gap;
int after_gap = n - last[v];
if (after_gap > max_gap[v]) max_gap[v] = after_gap;
int k0 = max_gap[v] + 1;
if (k0 <= n) {
if (v < add[k0]) add[k0] = v;
}
}
Now compute ans:
vector ans(n+1, -1);
int cur_min = INF;
for int k = 1; k <= n; ++k:
if (add[k] < cur_min) cur_min = add[k];
if (cur_min != INF) ans[k] = cur_min;
else ans[k] = -1;
Now output ans[1] .. ans[n] separated by spaces.
We need to ensure INF large enough: n+1 works because v <= n.
Potential pitfalls:
For values that appear, we updated max_gap with before and after. But we need to be careful: In case a value appears multiple times, we might have already updated max_gap with gaps between occurrences. That's fine. However, we must also consider the gap before the first occurrence and after the last. So we do that after the loop.
What about values that appear exactly once? In the loop, first[x] set, last[x] set to idx, but we never update max_gap from consecutive gaps because else branch not executed. So max_gap remains 0. Then we set before_gap = idx-1, after_gap = n-idx, and max_gap becomes max(0, before, after) which is correct.
However, there is nuance: The gap between consecutive occurrences is computed using last[x]. But what about the very first occurrence? When we first see x, we set first[x] and also set last[x] to idx. Then for subsequent occurrences, we compute gap using last[x] (previous occurrence). So that's correct.
But what about the gap after the last occurrence? We compute after_gap = n - last[x], where last[x] is the last occurrence index after processing all elements. That's correct.
But is there a subtlety: For a value that appears multiple times, the max_gap we compute from consecutive gaps might miss the gap between the last occurrence and the end? We'll include after_gap separately. Similarly before first.
Thus algorithm is correct.
Now we need to test with some random small cases to be sure.
Let's test some scenarios.
Case: n=7, a = [2,2,1,2,1,3,3]
Compute manually maybe.
Better to code and test, but as we are reasoning, we can reason.
But we should also consider that the condition "minimum number that occurs in all subsegments of length k" means the smallest value that appears in every length-k subsegment. According to our derivation, that value must satisfy k > gap_max(x). And we determined that if k > gap_max(x), then x appears in all length-k subsegments. This seems solid. Let's double-check with a tricky case: x appears with gaps exactly length g. For k = g+1, we argued any segment of length g+1 contains x. Consider gap of length g (positions without x). For a segment of length g+1, could it be placed such that it avoids x? Suppose the gap is from L+1 to L+g (inclusive) without x. Then to have a segment of length g+1 without x, it would need to be contained within that gap, but gap length is only g, so impossible. However, could the segment start before the gap and end after the gap, yet still avoid x? That would require that the segment excludes the x at both ends. For the segment to avoid x entirely, it must be entirely contained in the gap, because if it extends beyond the gap, it will include either the x before the gap (at position L) or the x after the gap (at position L+g+1) if those exist. But what if the gap is at the beginning? Positions 1..g have no x. Then a segment of length g+1 starting at 1 would go from 1 to g+1. Index g+1 contains x? If first occurrence is at g+1, then the segment includes x. If there is no x after? But after gap at beginning, there must be an occurrence at position g+1 (since x appears somewhere). So the segment includes x. If the segment starts at 2? That would be from 2 to g+2. Does it include x? Possibly position g+1 is within 2..g+2 as long as g+1 >=2, which is true, and g+1 <= g+2, so includes x. Actually need to check if there is a scenario where a segment of length > gap can avoid x if it straddles the gap but also avoids the x at both ends? For it to avoid x, it must not include any index where x occurs. The gap is a maximal contiguous block of indices without x. The x's are at the boundaries of that gap. So if the segment extends beyond the gap, it necessarily includes one of those boundary indices, because the gap is maximal. For example, if the gap is between positions L (x) and R (x) where L < R, and the gap indices are L+1,...,R-1. Then any segment that includes indices beyond L+1,...,R-1 must include either L or R if it extends left beyond L+1 or right beyond R-1. But what if the segment starts at L+2 and ends at R+1? Then it includes R (since R is <= R+1 and > L+2?). Actually R is > L+2? Not necessarily. Let's be systematic: Let positions of x: ... , p, q, ... with p < q. Gap length = q-p-1. Consider a segment [i, i+k-1] with k > gap. Suppose it contains no x. Then it must be entirely within the gap, because if it includes any index ≤ p, then it includes p if p is in the segment? But could it include indices before p but not include p? If p is not in the segment, but the segment includes indices less than p, then since p is the nearest x to the left, all indices ≤ p-1 are also without x? Actually if p is the nearest x to the left, then indices less than p could be part of another gap that ends at p. But that region also has no x until perhaps some other x further left. In any case, any contiguous block without x is at most length gap_max. Because we defined gap_max as the maximum length of a contiguous block of indices without x. So if a segment contains no x, it must be contained in a maximal block without x, and its length ≤ gap_max. So if k > gap_max, any segment of length k cannot be entirely inside any block without x, so must contain at least one x. So condition holds. So our logic is solid.
Thus, solution stands.
Now we need to code efficiently.
But we can further optimize memory: Since sum n ≤ 3e5, we can allocate fresh vectors per test case.
Now we should think about potential pitfalls: Indexing, especially when computing gaps using 1-indexing. Ensure that after loop, for each v, we compute before_gap = first[v] - 1 (since positions start at 1). That's number of indices before the first occurrence. For after_gap = n - last[v]. Good.
What about values that appear multiple times but there is also a gap at start/end larger than internal gaps? We handle that.
Now we should also consider that some values may have first and last set but max_gap not updated with internal gaps correctly. The internal gaps: we update when we see a new occurrence: gap = i - last[v] - 1. This includes gaps between consecutive occurrences. For the first occurrence, we don't compute a gap because there is no previous. So that's fine.
Edge: When a value appears many times, the maximum gap might be after processing all occurrences, but we might also need to consider gap before first and after last; we do that after loop.
Now what about a value that appears but the array length is 0? Not possible.
Now complexity: O(n) per test.
Now we output answer for each test case.
But one more nuance: For each k, we need the minimum number that occurs in all subsegments of length k. Could it be that some number appears in all subsegments of length k but does not satisfy k > gap_max(x)? Let's double-check if condition is indeed necessary and sufficient. The condition for x to appear in all length-k subsegments is equivalent to that there is no length-k subsegment without x. This is equivalent to saying that the maximum length of a contiguous block without x is less than k. Because if there is a block of length at least k, then that block itself (or a subsegment of length k within it) contains no x, so fails. Conversely, if all blocks without x have length < k, then any length-k segment must span across at least one x because it's longer than any block without x. However, consider a segment that starts near the boundary: Suppose the array starts with a block of length L < k without x, then after that block there is an x at position L+1. A segment of length k starting at position 1 will include indices 1..k. Since k > L, it includes indices up to at least L+1, so includes the x at L+1. Similarly for any start, the segment will always cover at least one x because the segment length exceeds the maximal gap. This is standard. So condition is exactly that the maximum gap length < k, i.e., k > gap_max. So yes.
Thus threshold k0 = gap_max+1.
Now compute for each value.
Now, we need to be careful: The answer is the minimum number (value) that satisfies condition. But could there be a scenario where a number with smaller value has a larger k0, and a larger value has smaller k0, and for some k, the minimum value among those with k0 <= k might be not the smallest value overall, but we need the minimum value that qualifies. So our method of scanning k and taking min among eligible yields exactly that.
Now, we can also compute answer by sorting values by their k0, but scanning is fine.
Now let's think about memory usage: We have arrays of size n+1 for first, last, max_gap, add. That's acceptable.
Implementation:
We'll read t, loop test cases.
Inside each test case:
Read n.
vector a(n);
for i in 0..n-1: cin >> a[i].
for i in 0..n-1:
int x = a[i];
int idx = i+1;
if (first[x] == -1) first[x] = idx;
else {
int gap = idx - last[x] - 1;
if (gap > max_gap[x]) max_gap[x] = gap;
}
last[x] = idx;
Initialize vector add(n+1, INF); // INF could be n+1
for v in 1..n:
if (first[v] != -1) {
int before = first[v] - 1;
if (before > max_gap[v]) max_gap[v] = before;
int after = n - last[v];
if (after > max_gap[v]) max_gap[v] = after;
int k0 = max_gap[v] + 1;
if (k0 <= n) {
if (v < add[k0]) add[k0] = v;
}
}
vector ans(n+1, -1);
int cur_min = INF;
for k in 1..n:
if (add[k] < cur_min) cur_min = add[k];
if (cur_min != INF) ans[k] = cur_min;
Print ans[1..n] separated by spaces, with newline after each test case.
Now test with provided examples.
We should test with n=5, a=[1,1,1,1,1]. first[1]=1, last[1]=5, internal gaps: when i=2: gap=0 => max_gap=0; i=3:0; i=4:0; i=5:0. after loop: before=0, after=0, max_gap=0 => k0=1. add[1]=1. ans: k=1: cur_min=1 => 1; k=2..5: cur_min remains 1 => 1. Output: 1 1 1 1 1. Good.
Test with n=5, a=[1,2,1,2,1]. Let's compute manually.
So add[2] = min(1,2)=1. Then ans: k=1: -1; k=2..5: cur_min=1 => ans[2]=1, ans[3]=1, ans[4]=1, ans[5]=1. Check: For k=3, are all subsegments of length 3 contain 1? Subsegments: [1,3]: yes; [2,4]: contains 1? indices 2,3,4: a[2]=2, a[3]=1, a[4]=2 => yes; [3,5]: yes. So 1 works. And 2 also works? Check: [1,3] contains 2? a[1]=1,2,1 -> contains 2? yes at index2; [2,4] contains 2; [3,5] contains? 1,2,1 -> contains 2 at index4? Wait [3,5] indices 3=1,4=2,5=1 => yes. So both 1 and 2 work for k=3. Minimum is 1. So ans[3]=1 correct. For k=2, subsegments: [1,2]: 1,2 (both present); [2,3]: 2,1; [3,4]:1,2; [4,5]:2,1. All contain both 1 and 2? Actually each contains at least one 1 and one 2? The condition is "the minimum number that occurs in all of the subsegments". So for a number to be a k-amazing number, it must occur in every subsegment. So for k=2, does 1 occur in every subsegment? Subsegment [2,3] contains 1? Yes at index3. [4,5] contains 1? Yes at index5. So yes, 1 occurs in all length-2 subsegments. Similarly 2 occurs as well. So minimum is 1. So ans[2]=1, not -1. So our algorithm gives 1 at k=2, which is correct. However, earlier I thought k=2 gave -1? Let's check again: For k=2, is there any length-2 subsegment that does NOT contain 1? The subsegments are: (1,2) contains 1; (2,3) contains 1; (3,4) contains 1; (4,5) contains 1. So yes, all contain 1. So condition holds. Good. Our computed k0 for 1 is 2, so indeed for k>=2, 1 is amazing. So correct.
Now test with array [1,2,2,2,1] n=5. Compute:
v=1: positions 1,5. gaps: between: 5-1-1=3; before:0; after:0 => max_gap=3 => k0=4.
v=2: positions 2,3,4. gaps: between: between 2&3 gap 0, 3&4 gap 0; before:1; after:1 => max_gap=1 => k0=2.
v=3? none.
So add[2] = 2; add[4] = 1? Actually add[4] = 1 (since v=1 k0=4). Then ans: k=1: -1; k=2: cur_min=2 => ans2=2; k=3: cur_min=2 => ans3=2; k=4: cur_min=min(2,1)=1 => ans4=1; k=5: ans5=1.
Check manually:
k=2: subsegments: [1,2]: 1,2 -> contains 2? yes; [2,3]:2,2 -> contains 2; [3,4]:2,2 -> contains 2; [4,5]:2,1 -> contains 2. So 2 works. Does 1 work? [4,5] contains 1, but [2,3] does not contain 1, so 1 fails. So min is 2. Good.
k=3: subsegments: [1,3]:1,2,2 -> contains 2; [2,4]:2,2,2 -> 2; [3,5]:2,2,1 -> 2. So 2 works. Does 1 work? [2,4] lacks 1. So min is 2. Good.
k=4: subsegments: [1,4]:1,2,2,2 -> contains 2; [2,5]:2,2,2,1 -> contains both 1 and 2? Wait, need number that occurs in all subsegments. For k=4, does 1 occur in both? [1,4] does not have 1? Actually [1,4] includes indices 1-4: 1,2,2,2 -> contains 1? Yes at index1. So [1,4] has 1. [2,5] has 2,2,2,1 -> contains 1 at index5. So both subsegments have 1, so 1 works. Does 2 also work? [1,4] contains 2, [2,5] contains 2. So both 1 and 2 work. Minimum is 1. So ans4=1, correct.
k=5: whole array: both 1 and 2 present, min=1.
So algorithm matches.
Now, could there be a scenario where for some k, no number qualifies? This happens when for all x, k <= gap_max(x), i.e., k < min over x of (gap_max(x)+1) = min_k0. So for k less than the smallest k0 among all values, answer is -1. That's captured by cur_min being INF until the first k where some add[k] is set.
Now ensure we handle cases where some value has k0 = n+1? Actually for missing values, gap_max = n, k0 = n+1 > n, so they are not added. So only appearing values considered.
Now, consider case where there is a value with k0 = 1, then cur_min becomes that value at k=1, so ans[1] = that value. That would be if the whole array is constant.
Now, consider a tricky case: Suppose n=3, a=[2,1,2]. Compute:
v=1: pos 2; gaps: before=1, after=1 => max_gap=1 => k0=2.
v=2: pos 1,3; gaps: between: 3-1-1=1; before:0; after:0 => max_gap=1 => k0=2.
So add[2]= min(1,2)=1. Then ans: k=1:-1; k=2:1; k=3:1. Check: k=2: subsegments [1,2]:2,1 contains 1; [2,3]:1,2 contains 1. So 1 works, 2 also? [1,2] contains 2, [2,3] contains 2, so both work. Min is 1. Good. k=3: whole array contains both, min is 1.
Now, what about k=1? Subsegments are individual elements; 1 appears only at index2, so not in all; 2 appears at indices1 and3, not in all; so -1. Good.
Thus algorithm seems consistent.
Now we need to implement.
Potential pitfalls: The input values are up to n, but n can be as large as 3e5. Use vectors. For each test case, we need to reset arrays. Since we use dynamic allocation per test case, it's fine.
Time complexity: O(n) per test case, total O(3e5). Should be fine.
Now let's think about possible alternative: We could compute k0 for each value and then fill ans by iterating k from n down to 1? Not necessary.
Now, edge: For values that appear, we compute before_gap and after_gap. What about the gap between the last and first? That's not a contiguous gap because array is linear. So okay.
Now, another nuance: The max_gap computation for values that appear many times could be done in O(m) where m is number of occurrences. Our method uses O(1) per occurrence.
Now, confirm with large test.
Now we can implement.
But before finalizing, we might consider if there is any off-by-one in the condition that x appears in all subsegments of length k iff k > gap_max(x). Let's test a borderline case:
Suppose array: [1,2,1] n=3. For x=1: positions 1,3. Gaps: between: 3-1-1=1; before:0; after:0 => gap_max=1 => k0=2. So for k=2, 1 should be amazing? Check subsegments of length 2: [1,2]:1,2 contains 1; [2,3]:2,1 contains 1. So yes. For k=1: no. So threshold 2 correct.
Now suppose array: [1,2,2,1] n=4. x=1: positions 1,4. Gaps: between: 4-1-1=2; before:0; after:0 => gap_max=2 => k0=3. Check: k=3 subsegments: [1,3]:1,2,2 contains 1; [2,4]:2,2,1 contains 1. So 1 amazing at k=3. k=2: subsegments: [1,2]:1,2 contains 1; [2,3]:2,2 no 1; [3,4]:2,1 contains 1 -> not all contain 1. So fails. So threshold correct.
Now test x=2: positions 2,3. Gaps: between: 3-2-1=0; before:1; after:1 => gap_max=1 => k0=2. For k=2: [1,2] contains 2, [2,3] contains 2, [3,4] contains 2? [3,4] indices 3=2,4=1 => yes contains 2. So 2 amazing at k=2. k=1: [2] contains 2, but [1] does not; fails. So correct.
Now, does x=2 become amazing at k=3? Since condition is monotonic, yes. Check: [1,3] contains 2, [2,4] contains 2; so yes.
Now check the condition for k>gap_max: For x=2, gap_max=1, k=2 >1, so works.
Thus correct.
Now, we need to be careful with the definition of "subsegment" – contiguous part of a containing exactly k elements. So length k subsegments are exactly k consecutive indices. So our gap reasoning is correct.
Now, we might also consider an alternative approach: For each value, compute the maximum distance between consecutive appearances (including boundaries) as d, then the value is valid for all k > d. So threshold = d+1.
Now, let's implement.
But wait: The problem statement: "minimum number that occurs in all of the subsegments of the array having length k". That is, among numbers that occur in every subsegment of length k, pick the smallest one. If none, output -1.
Our algorithm picks the minimum value among those with k0 <= k. However, note that it's possible that a number satisfies the condition for a given k but does not satisfy that k > gap_max? According to our reasoning, condition is equivalent to k > gap_max. So k0 = gap_max+1 is indeed the smallest k where it becomes amazing. So if it is amazing at k, then k >= k0. So if k0 <= k, it qualifies. So our condition is correct.
But what if a number appears in all subsegments of length k, but its gap_max is actually larger than k? That would be a contradiction because if gap_max >= k, then there exists a gap of length at least k, which contains a subsegment of length k without x. So it cannot be amazing. So not possible.
Thus threshold is exact.
Now consider an alternative: Could there be a number that appears in all subsegments of length k but its gap_max is exactly k? That would mean there is a gap of length k, but then the subsegment exactly covering that gap (which length k) would have no x, contradiction. So not possible. So gap_max must be < k.
Therefore threshold k0 = gap_max+1 is exact.
Now, we've to ensure that our gap_max computation includes all possible gaps. The maximum contiguous block without x is max of (first-1, n-last, and differences between consecutive positions minus 1). This is correct.
Now, we need to think about potential integer overflow? Not.
Now, we should implement with fast IO.
Let's also consider an optimization: Instead of separate add array, we could directly fill ans by iterating values in increasing order and setting ans[k] for k from k0 to n to be the minimum value seen so far? But that would be O(n^2) if we do for each value. Better to use the event approach O(n).
But we could also compute the answer by: For each value v, we know its k0. Then for each k, ans[k] = min_{v: k0(v) <= k} v. So we can sort values by k0 and then process. But our event method is essentially that: we sort implicitly by scanning k and adding values when their k0 is reached, but we need to know which values have a given k0. Since values are up to n, we can create a vector of lists for each k0: for each v, push v into bucket[k0]. But that would require storing all v, which is O(n) memory anyway. But we can avoid storing all v by just keeping the minimum v per k0, because for ans we only need the minimum among v with that k0 when updating the running min. However, what if there are multiple values with same k0, and a larger v becomes eligible later but we only kept the smallest? Since we only care about the minimum of all eligible, it's sufficient to know the smallest v for each k0. Because when we process k0, we will update cur_min = min(cur_min, add[k0]) where add[k0] is the smallest v for that k0. If we only keep the smallest, it's correct because any larger v would not affect the running minimum (since they are larger than the smallest at that k0, and the running minimum already ≤ that). However, consider a scenario: k0 values: at k0=5, we have v=3 and v=4. Minimum is 3. At k=5, cur_min becomes 3. That's fine. If we had stored only 3, that's enough. If we had stored only 4, we would miss 3, leading to wrong answer. So we must store the minimum per k0. That's what we do: add[k0] = min(add[k0], v). So we only need to keep the minimum per k0, not all v. That's fine.
Now, could there be a case where a value with a larger k0 is smaller than some values with smaller k0? For example, v=1 has k0=10, v=2 has k0=5. Then at k=5, cur_min becomes 2; at k=10, cur_min becomes min(2,1)=1. So correct.
Now, is it possible that for some k, the answer is not the cur_min but some other value because the cur_min might have been from a v with k0 <= k but that v might not be amazing at k due to some nuance? We've argued it is amazing for all k >= k0. So fine.
Now, confirm that if a value appears, its k0 is at most n. Indeed, maximum gap max_gap is at most n-1 (if appears exactly once at an endpoint) or n if the value never appears. For appearing values, gap_max <= n-1, so k0 <= n. So we will always have add[k0] set for each appearing value.
Now, one more nuance: The array values are between 1 and n, but there could be values that appear but the computed k0 could be 0? No, because max_gap is non-negative, so k0 >=1.
Now, we must ensure to use 64-bit int? Not necessary.
Now, we should think about the potential for other numbers that are not in the array to be considered? The problem defines "number" meaning integer value; it could be any integer? But a_i are between 1 and n, and we are to find minimum number that occurs in all subsegments. If a number does not appear at all in the array, it cannot occur in any subsegment, so cannot be amazing. So we ignore.
Thus algorithm stands.
Now, we should test with some random small n and brute force to be sure. Let's simulate mentally a few.
Case: n=4, a=[1,2,3,4]. Each appears once. Compute k0:
1: before=0, after=3 => max_gap=3 => k0=4
2: before=1, after=2 => max_gap=2? Actually first=2, last=2 => before=1, after=2 => max_gap=2 => k0=3
3: before=2, after=1 => max_gap=2 => k0=3
4: before=3, after=0 => max_gap=3 => k0=4
So add[3]= min(2,3)=2; add[4]= min(1,4)=1.
Ans: k=1:-1; k=2:-1; k=3:2; k=4:1.
Check manually:
k=1: need number in all single elements; none; -1.
k=2: subsegments: [1,2]:1,2; [2,3]:2,3; [3,4]:3,4. Which numbers appear in all? 1 appears only in first, not in [2,3] or [3,4]; 2 appears in first two but not in [3,4]; 3 appears in last two but not in first; 4 appears only in last; so none. -1 correct.
k=3: subsegments: [1,3]:1,2,3; [2,4]:2,3,4. Which numbers appear in both? 2 appears in both? [1,3] has 2, [2,4] has 2, yes. 3 appears in both, yes. 1 appears only in first; 4 only in second. So min among {2,3} is 2. So ans=2 correct.
k=4: whole array: all numbers appear. So min=1. So ans=1 correct.
Case: n=6, a=[2,2,1,1,2,2]. Let's compute.
Positions:
2: 1,2,5,6
1: 3,4
Compute for 2: positions 1,2,5,6. Gaps: between 1-2:0; 2-5: gap=5-2-1=2; 5-6:0; before:0; after:0 => max_gap=2 => k0=3.
For 1: positions 3,4: between: 4-3-1=0; before:2; after:2 => max_gap=2 => k0=3.
So add[3] = min(2,1)=1.
Ans: k=1:-1, k=2:-1, k=3..6:1.
Check:
k=3: subsegments? length3: [1,3]:2,2,1 contains 1? yes; [2,4]:2,1,1 contains 1; [3,5]:1,1,2 contains 1; [4,6]:1,2,2 contains 1. So 1 appears in all. Also 2 appears? Check [3,5] contains 2? yes at 5; [4,6] contains 2; others contain 2. So both appear. Min=1. Good.
k=2: subsegments: [1,2]:2,2 no 1; [2,3]:2,1 contains 1; [3,4]:1,1 contains 1; [4,5]:1,2 contains 1; [5,6]:2,2 no 1. So 1 does not appear in all. 2? [3,4] does not have 2, so fails. So none. -1 correct.
So algorithm works.
Now we must ensure to reset add array for each test case. We'll allocate fresh.
Now consider the memory overhead of having add of size n+1 with INF initially. That's okay.
Now, we need to output the answers separated by spaces and newline.
Now, could there be any oversight regarding the maximum gap computation for values that appear many times? Let's double-check using a test where a value appears at positions that are not sorted? We iterate in order, so they are sorted.
Now, is there any scenario where the gap between the last occurrence and the first occurrence could be considered as a wrap-around? No, array is linear.
Now, implement.
Pseudo-code:
int t; cin >> t;
while(t--){
int n; cin >> n;
vector a(n);
for(int i=0; i<n; i++) cin >> a[i];
code
vector<int> first(n+1, -1), last(n+1, -1), max_gap(n+1, 0);
for(int i=0; i<n; i++){
int x = a[i];
int idx = i+1;
if(first[x] == -1){
first[x] = idx;
} else {
int gap = idx - last[x] - 1;
if(gap > max_gap[x]) max_gap[x] = gap;
}
last[x] = idx;
}
const int INF = n+1;
vector<int> add(n+1, INF); // 1-indexed for k
for(int v=1; v<=n; v++){
if(first[v] != -1){
int before = first[v] - 1;
if(before > max_gap[v]) max_gap[v] = before;
int after = n - last[v];
if(after > max_gap[v]) max_gap[v] = after;
int k0 = max_gap[v] + 1;
if(k0 <= n){
if(v < add[k0]) add[k0] = v;
}
}
}
vector<int> ans(n+1, -1);
int cur_min = INF;
for(int k=1; k<=n; k++){
if(add[k] < cur_min) cur_min = add[k];
if(cur_min != INF) ans[k] = cur_min;
// else remains -1
}
for(int k=1; k<=n; k++) cout << ans[k] << " \n"[k==n];
}
Now we need to consider potential overflow or negative indices: All good.
Now, we should test with the examples.
Example1:
t=3
First test: n=5, a=1 2 3 4 5
Our code will compute as before. Output: "-1 -1 3 2 1 " (with space after last maybe). But problem output has trailing spaces? Usually it's okay. We'll format with spaces between numbers and newline at end.
Now, check second test: 5 4 4 4 4 2 -> should output "-1 4 4 4 2 ".
Third: 6 1 3 1 5 3 1 -> "-1 -1 1 1 1 1 ".
Matches.
Now, edge: t=1, n=1, a=[1] -> output "1". Good.
Now, what about n=3, a=[2,2,2] -> output "2 2 2". Good.
Now, maybe we need to handle cases where multiple test cases and sum n up to 3e5, so vector allocations are fine.
Now, potential bug: In computing max_gap during the loop, we only update when seeing a second occurrence. For values with multiple occurrences, we correctly update. But we also need to consider the gap between the first and second occurrence? That is covered when we see the second occurrence: at i (second), we compute gap = i - last[x] - 1 where last[x] is first occurrence index. So yes.
Now, after the loop, we incorporate before and after gaps. But note: If a value appears only once, max_gap stays 0, then we set before and after. Good.
Now, consider a value that appears but the maximum gap is actually between some later occurrence and the end? We include after. Also, could the maximum gap be before the first occurrence? We include before.
Thus correct.
Now, we should think about alternative interpretation: Could there be a case where a number appears in all subsegments of length k but does not appear in some subsegment? That would contradict. So fine.
Now, about the time: O(n) per test. Sum n ≤ 3e5, so overall O(3e5). Good.
Now, memory: For each test, we allocate vectors of size n+1 each: first, last, max_gap, add, ans. That's about 5*(n+1) ints ~ 20* n bytes. For n=3e5, that's about 6 million bytes, which is okay. But across test cases, we allocate and deallocate, still within limits.
We could reuse vectors by clearing, but it's fine to allocate fresh each test case.
Now, I'll produce final answer.
But before finalizing, we need to double-check the logic for k0 calculation for values that appear multiple times but with overlapping gaps? Already covered.
Now, let's attempt a more thorough proof:
Define positions of x: p1 < p2 < ... < pm.
Define gaps: g0 = p1 - 1, gi = p_{i+1} - p_i - 1 for i=1..m-1, gm = n - pm.
Let G = max(g0, g1, ..., gm). Then for any subsegment of length k, it contains x if and only if k > G. Because:
If k > G, any interval of length k must intersect at least one of the intervals that contain x? Actually if k > G, then any interval of length k cannot be completely contained within any gap (since gaps are length ≤ G). So it must contain at least one p_i.
If k ≤ G, then there exists a gap of length G, and within that gap we can choose a subsegment of length k that lies entirely in the gap, thus contains no x. Therefore x does not appear in all subsegments of length k.
Thus x is k-amazing iff k > G. So the minimal k for which x is amazing is G+1.
Thus our computed k0 is correct.
Now, answer for each k: min{x | G_x < k} = min{x | k0(x) <= k}.
Thus algorithm stands.
Now, I'd like to consider any possible integer issues: None.
Now, let's consider potential pitfalls with large inputs and speed. Use scanf/printf or fast cin/cout with sync.
We can also compress by using arrays of int and memset? But we need to reset per test case. Since sum n is limited, we can allocate vectors with resize and assign -1 for first and last. But we can also use static arrays of size max total? Since total n sum is 3e5, we could allocate global arrays of size 300005 and reuse, but we need to reset only for the values that appear. But using vectors per test case is simpler and safe.
But careful: The sum of n over test cases ≤ 3e5. So worst-case one test with n=3e5, vectors okay.
Now, code.
Now, we can also reduce memory by not storing ans separately, just output on the fly. But it's okay.
Now, there is one more nuance: For values v, we compute k0 = max_gap[v] + 1. But note that max_gap[v] is computed as integer. All good.
Now, let's test with random small n using a brute force script mentally? But we can reason enough.
Now, I'll produce the final solution.
But before finalizing, I recall that this problem is known as "k-Amazing Numbers" from Codeforces Round (maybe #1418?). The intended solution is exactly this.
Now, we should also think about scenario where multiple test cases and we need to flush output.
Now, produce final answer.
Potential improvements: We could compute the threshold without storing max_gap for all values by using the positions list. But it's fine.
Now, let's write the code.
One more subtlety: In the loop for computing gaps, we update max_gap when seeing a subsequent occurrence. However, we might also need to consider the gap between the first occurrence and the start if first occurrence is not at 1. That we do after loop. Also need to consider the gap between the last occurrence and the end.
Now, what about the gap between two consecutive occurrences that are far apart, we update when second occurrence encountered.
Now, I think it's correct.
Now, double-check with a value that appears many times, but the maximum gap occurs between the last and the end, and that is larger than any internal gap. We compute after loop and update max_gap accordingly. Good.
Now, for values that appear at both ends? For example, array starts and ends with x. Then before_gap = 0, after_gap = 0. max_gap may be from internal gaps. Good.
Now, consider a value that appears at indices 1 and n only. Then internal gap: n-1-? Actually positions 1 and n; gap between them: n-1-1 = n-2? Wait: gap between 1 and n: n - 1 - 1 = n-2? Actually formula: p2 - p1 - 1 = n - 1 - 1 = n-2. So max_gap = max(0, n-2, 0) = n-2. Then k0 = n-1? But we also have before_gap = 0, after_gap = 0. So k0 = n-1. Let's test: n=5, positions 1 and 5. Gaps: between: 5-1-1=3. So max_gap=3 => k0=4. For k=4, subsegments: [1,4]: indices1-4: includes x at 1; [2,5]: includes x at 5. So x appears in all length-4 segments? Are there length-4 segments that do not include x? Only two possible: [1,4] and [2,5]. Both include x. So yes, x is amazing for k=4. For k=3, subsegments: [1,3] includes x; [2,4] no x (since indices2-4 have no x), [3,5] includes x. So fails. So threshold is 4, which is n? Actually n=5, k0=4. So correct. So indeed k0 can be n, not necessarily n+1. So all good.
Now, note that for a value that appears only at the first position, gap_max = max(0, n-1) = n-1, k0 = n. So works.
Now, final code.
Potential issues: In the loop where we compute before and after, we need to use max_gap[v] = max(max_gap[v], before) and similarly for after. But if before is larger than current max_gap, we update. That's what we do.
Now, note that for values that appear, we might have computed max_gap incorrectly if we didn't initialize max_gap to 0. That's fine.
Now, one more optimization: We could combine first and last into one array, but not needed.
Now, implement.
But wait: The problem constraint: a_i are between 1 and n. However, values can be up to n, but n can be as large as 3e5. So using arrays sized n+1 is fine.
Now, we might want to use 0-indexing for positions to avoid the +1? It's easier to use 1-indexing as we did.
Now, careful with reading input: using cin might be slower but with ios_base::sync_with_stdio(false) and cin.tie(NULL) it should be fine for 3e5 total.
Now, let's produce final answer.
One more thought: Could there be a case where for a value v, we compute k0 but it might be larger than n? We already check k0 <= n before adding to add. But it's impossible for appearing values? As argued, max_gap <= n-1, so k0 <= n. So always added. But still safe.
Now, we can write.
Now, we need to output the answers for each test case.
Let's produce final solution.
But before final, think about test case with n=300000 and all distinct values. Then each value appears once. For each v, we compute before and after, and k0. That's O(n). The add array will have many entries. That's fine.
Now, final code.
Potential bug: In the first pass, we didn't initialize last[x] before the first occurrence. In our loop, when we see first occurrence, we set first[x]=idx, and then set last[x]=idx. So for subsequent, last[x] is properly set.
Now, confirm: In else branch, we compute gap using last[x] which is previous occurrence. Good.
Now, after loop, we use last[x] for after gap, which is the last occurrence index.
Now, for a value that appears multiple times, after we compute before and after, we might have max_gap correctly.
Now, I'll produce the final solution.
Now, we need to ensure that we don't have any off-by-one in the condition for max_gap vs k. We used k0 = max_gap+1. In the problem, they ask for each k from 1 to n. So if max_gap=0, then k>=1 qualifies, so k0=1.
Now, confirm with example: all same number, max_gap=0, k0=1, ans from k=1 is that number. Good.
Now, I think it's good.
Now, additional check: For k=1, answer is the number that appears in every element, i.e., if the array has all same element, that element. Else -1. Our method: For a value with max_gap=0, k0=1, so it will be in add[1]. So at k=1, cur_min becomes that value. If multiple values have max_gap=0? That would imply every element is both numbers, impossible. So only one value can have max_gap=0 (the constant array). So ans[1] becomes that value. If not constant, no value has max_gap=0, so add[1] remains INF, ans[1] = -1. Good.
Now, finalize.
Now, possible nuance: For a value that appears, its max_gap could be 0 even if it appears not everywhere? Let's think: max_gap=0 means there is no gap of length ≥1 without x. That means between any two consecutive occurrences, there is no index without x, i.e., the indices between them are all x? Actually gap = number of indices between positions that are not x. If there is a gap of length 0, that means consecutive positions both have x? Actually if p_{i+1} - p_i - 1 = 0, then p_{i+1} = p_i+1, meaning x at consecutive indices. To have max_gap=0, we need: before first =0 (so first at 1), after last =0 (so last at n), and all gaps between consecutive occurrences =0, meaning the positions are consecutive indices. That means the array is entirely filled with x from 1 to n. So indeed only constant array yields max_gap=0. So correct.
Now, time to write final answer.
I'll implement as described.
But before final, let's consider if we can reduce memory: We could reuse the 'first' array as something else after computing thresholds, but not needed.
Now, I'll write in C++.
Edge Cases: n=0? Not possible (n>=1).
Now, output formatting: For each test case, print n integers separated by spaces, and newline.
Now, potential trap: The answer for k might be -1, and we need to print -1 as integer. That's fine.
Now, go ahead.
Now, I'll produce the final answer.
But I'm ChatGPT, I need to output final answer with explanation? The request: "You are given ... For each k from 1 to n calculate the k-amazing number of the array a." So the response should provide solution description and code? The typical format for such problems: The assistant should output the code. But the user message is a problem statement, but they might expect the solution code? The initial instruction: "You are given an array a ... For each k from 1 to n calculate the k-amazing number." This is typical of a coding problem. As ChatGPT, I'm expected to produce an answer? Possibly the user wants the solution approach and code. Since it's a problem from Codeforces, the user might want me to write the solution. I'll provide a concise explanation and then the code.
Given that the conversation is in a problem-solving context, I'll provide the solution description and the C++ implementation.
Thus, final response will include explanation of approach and code.
Now, I'll write the answer. We can solve the problem by analyzing, for each value (x) that appears in the array, the largest gap between consecutive occurrences (including before the first and after the last). A value appears in every subsegment of length (k) iff (k) is greater than this maximum gap. Therefore the smallest (k) for which (x) works is (k_0(x) = \text{max_gap}(x) + 1). For any (k \ge k_0(x)), (x) is a valid (k)-amazing number. The property is monotonic: if (x) works for some (k), it also works for all larger (k).
We compute (k_0(x)) for every (x) that occurs. Then for each (k) from (1) to (n) the answer is the minimum (x) with (k_0(x) \le k). This can be obtained by scanning (k) from (1) to (n) and maintaining the smallest (x) that has become eligible up to that (k).
Implementation steps per test case:
Read (n) and the array (a).
For values (1\ldots n) keep:
first[x], last[x] (indices, 1‑based),
max_gap[x] (maximum length of a contiguous block without (x)).
Scan the array. When a value (x) appears at index (i):
If it's the first occurrence, set first[x] = i.
Otherwise update max_gap[x] = max(max_gap[x], i - last[x] - 1).
Set last[x] = i.
After the scan, for each (x) that appears:
Update max_gap[x] with the gaps before the first (first[x]-1) and after the last (n - last[x]).
Compute (k_0 = \text{max_gap}[x] + 1).
If (k_0 \le n), record that at position (k_0) we have a candidate value (x); keep only the smallest (x) for each (k_0).
Iterate (k = 1\ldots n):
Update a running minimum cur_min with the candidate at (k) (if any).
If cur_min is still infinite → answer (-1); otherwise answer cur_min.
Output the answers for all (k).
The total time is (O(n)) per test case, and the sum of (n) over all tests is ≤ (3\cdot10^5), so it easily fits.
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];
vector<int> first(n + 1, -1), last(n + 1, -1), max_gap(n + 1, 0);
for (int i = 0; i < n; ++i) {
int x = a[i];
int idx = i + 1; // 1‑based index
if (first[x] == -1) {
first[x] = idx;
} else {
int gap = idx - last[x] - 1;
if (gap > max_gap[x]) max_gap[x] = gap;
}
last[x] = idx;
}
const int INF = n + 1;
vector<int> add(n + 1, INF); // smallest value that becomes eligible at k
for (int v = 1; v <= n; ++v) {
if (first[v] != -1) {
// consider gaps before first and after last
int before = first[v] - 1;
if (before > max_gap[v]) max_gap[v] = before;
int after = n - last[v];
if (after > max_gap[v]) max_gap[v] = after;
int k0 = max_gap[v] + 1;
if (k0 <= n && v < add[k0]) add[k0] = v;
}
}
vector<int> ans(n + 1, -1);
int cur_min = INF;
for (int k = 1; k <= n; ++k) {
if (add[k] < cur_min) cur_min = add[k];
if (cur_min != INF) ans[k] = cur_min;
}
for (int k = 1; k <= n; ++k)
cout << ans[k] << " \n"[k == n];
}
return 0;
}