Ben believes a lot in tarot cards. He believes that they are lucky for him. He even decides to wear clothes according to the color predicted by a draw of a card. This irritates his wife, because sometimes he wears the same color t-shirt continuously if his cards so predict.
Now, his wife wants to go on a vacation with him, but she has a condition that she will only go if he wears a different color t-shirt on every day they are on a vacation. Ben really wants to please her, so he found out in advance the color predicted by his cards for the next N days. Now of these, he wants to find the longest vacation span he can get if he wears a different color t-shirt each day. Also, if there are multiple such occurrences of longest span, he wants to know the earliest such span because his wife will get angry is he delays for too long.
Input:
First line contains T which is the number of test cases.
Each test case contains 2 lines.
First line of every test case contains an integer N where N is the number of days.
Second line of every test case contains the N colors which indicate the color of the T-Shirt Ben wore on each day (1-indexed).
Output:
For each test case, output a line containing 2 integers indicating the starting and ending day numbers of the largest span.
Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 10^5
1 ≤ color ≤ 10^9
Scoring:
1 ≤ N ≤ 10 : (30 pts)
1 ≤ N ≤ 10^3 : (30 pts)
Original Constraints : (40 pts)
SAMPLE INPUT
2
5
1 2 3 4 5
6
1 2 1 3 1 5
SAMPLE OUTPUT
1 5
2 4
Explanation
Case 1: As he can wear 5 different T-shirts on the next 5 days, he can go on a 5 day vacation from day 1 to day 5.
Case 2: The longest duration is of 3 days from [2,4] and [4,6] of which day 2 to day 4 comes earlier.
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 are given T test cases. For each test case, N and a list of colors (integers). We need to find the longest contiguous subarray (span) with all distinct elements (i.e., no repeated colors). If there are multiple subarrays with the maximum length, we need to output the one that occurs earliest. "Earliest" means the one with the smallest starting index? Probably yes, because "if there are multiple such occurrences of longest span, he wants to know the earliest such span because his wife will get angry is he delays for too long." So we need the longest subarray with all distinct values, and among ties choose the one with the smallest start index (and maybe also if same start? but longest length unique start? Actually if two subarrays have same length and both start at different days, the earliest start is the one with smaller starting index. That's natural.)
We need to output start and end day numbers (1-indexed). So we basically need to find the maximum length subarray with all distinct elements, and if multiple, choose the one with minimal start index.
Constraints: N up to 1e5 per test case, T up to 10, so total N up to 1e6? Possibly, but we can handle each test case in O(N) using sliding window (two pointers) and a hash map to store the last occurrence index of each color. Standard approach: maintain left pointer l, iterate right pointer r from 1 to N. Keep a map last_index[color] of the most recent index where that color appeared (1-indexed). When we encounter color at r, if last_index[color] >= l, then we need to move l to last_index[color] + 1 to avoid duplicate. After updating l, update last_index[color] = r. Then the current window [l, r] has all distinct colors. Its length = r - l + 1. We track the maximum length and the earliest start.
But careful: when we have a window, we want to record whenever we find a length > best_len, update best_len and best_start (and best_end = r). If length == best_len, we need to choose the one with earliest start, i.e., if start < best_start? But note: if length == best_len and start < best_start, then we update because earlier start. But what about cases where start > best_start but length same? We don't update.
But we must ensure that when we move l due to a duplicate, the new window might have a smaller length but same start? Actually standard sliding window records the maximum window. However, we need earliest start among maximum length windows. The sliding window algorithm naturally scans from left to right, and at each step we have a valid window ending at r. Among windows ending at r, the leftmost l that makes it valid is used. So as we increase r, we will encounter all possible windows. However, could there be a maximum length window that does not end at some r? Every subarray has an ending index; so it will be considered when r is that ending. So we can track best while iterating.
But we must be careful: The standard sliding window algorithm maintains that for each r, l is the smallest index such that [l, r] has all distinct colors. So as we iterate, we see each valid window exactly once. So we can compare length and update best.
Now, about ties: If we find a window with same length as best_len but earlier start (i.e., l < best_start), we should update because earlier start. But note that since l is minimal for a given r, and r increases, the earliest start among all windows of maximum length might not be the one with smallest r. But if we update when length equals best_len and l < best_start, that would pick the smallest start among all windows we've seen. Since we are scanning all windows, that should give the correct earliest start. However, consider a scenario: Two windows both of maximum length L. One starts at s1, ends at e1; another starts at s2 > s1, ends at e2 > e1. Which one is earlier? s1 < s2, so s1 is earlier. Our algorithm: when we reach r = e1, we record (s1, e1) if length L > current best, or if equal and s1 < best_start (but initially best_start maybe something else). Later when r = e2, we have length L same, and s2 > s1. Since we compare if s2 < best_start? It will be false because best_start = s1. So we keep the earlier start. That's correct.
But need to confirm: Could there be a scenario where a window of maximum length appears but its start is less than the current best_start, but its length is equal to best_len, but we had already set best_len earlier from another window of same length but with start > current start? Actually if we encounter a window with length equal to current best_len and start < best_start, we should update to that start (since earlier). That's fine.
Edge Cases: Colors up to 1e9, so use unordered_map (hash map) or map. Since N up to 1e5, unordered_map fine. But be careful about worst-case O(N) average.
Alternatively, we could use a dictionary and store last occurrence index.
Implementation: For each test case:
read N
read array colors of size N
initialize unordered_map<int, int> last_pos; (maps color to its most recent index (1-indexed)).
loop r from 1 to N:
color = colors[r-1] (if using 0-indexed but easier to keep 1-indexed)
if last_pos[color] >= l: then l = last_pos[color] + 1
last_pos[color] = r
current_len = r - l + 1
if current_len > best_len:
best_len = current_len
best_start = l
best_end = r
elif current_len == best_len:
if l < best_start:
best_start = l
best_end = r
# if l == best_start, but that would mean same window? ignore.
Output best_start and best_end.
Check sample:
Case 1: 1 2 3 4 5: sliding yields windows always distinct, lengths: r=1 len1, r=2 len2, ... r=5 len5, best updates each time, final best_start=1, best_end=5 -> correct.
Case 2: 1 2 1 3 1 5
r=1: color 1, last=0, l=1, last[1]=1, len=1, best=1, start=1
r=2: color 2, last=0, l=1, last[2]=2, len=2, best=2, start=1, end=2
r=3: color 1, last=1 >= l=1, so l=2, last[1]=3, len = 3-2+1=2, not >2, equal? len=2, best_len=2, l=2, best_start=1, l not < best_start, so no update. (But note there is a window of length 3? Not yet)
r=4: color 3, last=0, l=2, last[3]=4, len=3 (2,3,4) -> best_len=3, best_start=2, best_end=4
r=5: color 1, last=3 >= l=2, so l= last[1]+1 = 4, last[1]=5, len = 5-4+1=2, not best.
r=6: color 5, last=0, l=4, last[5]=6, len=3 (4,5,6) -> len=3 equal to best_len=3, l=4, best_start=2, l is not < best_start, so no update. So final best_start=2, best_end=4. matches sample.
So algorithm works.
But need to think about potential nuance: "if there are multiple such occurrences of longest span, he wants to know the earliest such span". Could "earliest" refer to the one that starts earliest, or the one that occurs earliest in time? Usually both same. So yes.
Complexities: O(N) per test.
Now: large constraints up to 1e5, T up to 10, so total up to 1e6, fine.
Edge: Colors can be up to 1e9, unordered_map fine. But note that collisions might degrade but still okay.
Potential issue: The sliding window algorithm as described uses last_pos with default 0 for unseen colors. However, if l starts at 1, then condition last_pos[color] >= l works because last_pos default 0, which is < 1. That's fine. However, if l can become 0? No, l starts at 1 and only updates to last_pos[color]+1, where last_pos is at least 1, so l >= 1. So safe.
But careful: When we update last_pos[color] after moving l, we set to r. That's correct.
Implementation details: Use fast I/O because N up to 1e5 per test, but total not huge; but still advisable: cin with sync off or scanf.
We'll use scanf for safety. Since T up to 10, N up to 1e5, reading 1e6 integers okay.
Alternatively, we can use cin with ios_base::sync_with_stdio(false); cin.tie(NULL);.
Now code: read T, loop.
For each test: read N, allocate vector arr(N), for i in range(N): read arr[i].
Then sliding window.
Memory: store array; but we could process reading on the fly? Since we need last occurrence, we need to know colors as we iterate; but we can read all into array or read one by one. Reading all is easier.
But we could process while reading: we read each color sequentially, and maintain sliding window. That would avoid storing array. Since we only need to process in order, we can do it without storing all N. That would be more memory efficient. However, storing N up to 1e5 is okay. But T up to 10, worst-case memory 1e6 ints ~ 4 MB, fine. So either way.
We'll process while reading: reading one color at a time, update. But careful: we need to know the index r. So we can read N, then loop i from 1 to N, read color, process. That avoids storing array. That's better. We'll do that.
Implementation:
int T; scanf("%d", &T);
while(T--) {
int N; scanf("%d", &N);
unordered_map<int, int> lastPos;
int l = 1;
int best_len = 0, best_start = 1, best_end = 1;
for (int r = 1; r <= N; ++r) {
int color; scanf("%d", &color);
if (lastPos[color] >= l) {
l = lastPos[color] + 1;
}
lastPos[color] = r;
int cur_len = r - l + 1;
if (cur_len > best_len) {
best_len = cur_len;
best_start = l;
best_end = r;
} else if (cur_len == best_len && l < best_start) {
best_start = l;
best_end = r;
}
}
printf("%d %d\n", best_start, best_end);
}
But note: unordered_map's operator[] will create an entry with default 0 if not present. That's okay, but we need to ensure that we don't inadvertently increase memory usage unnecessarily for each unseen color? That's fine, but we also need to consider that lastPos[color] might be accessed multiple times. In the condition if (lastPos[color] >= l), we call operator[], which creates default 0 if not existent. That's acceptable because we will later set lastPos[color]=r anyway. So it's okay.
Alternative: use find to avoid creating, but not needed.
But one subtlety: In some languages, using unordered_map with int keys up to 1e9 could cause many collisions but still average O(1). Should be fine.
Now, check constraints: N up to 1e5, but colors up to 1e9. So map size at most N distinct colors, okay.
Edge Cases:
N=1: array with one color. l=1, lastPos default 0, condition false, set lastPos[color]=1, cur_len=1 > best_len=0 => update best_start=1,best_end=1. Output 1 1.
All colors same: e.g., N=5: 1 1 1 1 1. Let's simulate: r=1: l=1, last[1]=1, cur_len=1, best=1 start=1 end=1.
r=2: last[1]=1 >= l=1 -> l=2, set last[1]=2, cur_len=1 (r-l+1=2-2+1=1) not >1, equal? l=2 > best_start=1 so no update.
r=3: last[1]=2 >= l=2 -> l=3, cur_len=1, etc. So best stays at start=1, end=1. That's correct: longest distinct subarray length is 1, earliest is day1.
Increasing distinct: correct.
Now, about ties: Suppose N=4: colors: 1 2 3 2. Let's compute: distinct subarray max length? The subarrays: [1,2,3] length3 start1, [2,3,2] has duplicate. Also [3,2] length2. So longest is length3 start1 end3. Our algorithm: r=1: len1 best=1 start1. r=2: len2 best=2 start1. r=3: len3 best=3 start1. r=4: color2, last[2]=2, l currently 1? At r=4, before update last[2]=2, l=1, condition true => l=3. last[2]=4. cur_len=2 (3-4) len2, not >3. So final start1 end3.
Now suppose colors: 2 1 2 3. Longest distinct subarray? [1,2,3] length3 from day2 to day4. Also [2,1] length2, [2,3]? Actually also [2,1,2] not distinct. So only length3 start2. Our algorithm: r=1: best=1 start1; r=2: len2 best=2 start1; r=3: color2, last[2]=1 >= l=1 => l=2, last[2]=3, len=2 (2-3) not >2; r=4: color3, len=3 (2-4) best=3 start2. So correct.
Now, tie: Example: 1 2 3 1 2 3. Distinct subarrays length3: there are multiple: [1,2,3] from day1 to day3, and [2,3,1] from day2 to day4, and [3,1,2] from day3 to day5, and [1,2,3] from day4 to day6? Actually day4 is 1, day5 is 2, day6 is 3, so [4,6] also length3. Among these, earliest start is 1. Our algorithm: It should produce start=1 end=3. Let's test: r=1: best=1 start1; r=2: best=2 start1; r=3: best=3 start1; r=4: color1, last[1]=1, l=1 -> l=2, last[1]=4, cur_len=3? r=4, l=2 => len=3 (2-4). Now cur_len==best_len=3, but l=2 > best_start=1, so no update. r=5: color2, last[2]=2? Actually after r=2 last[2]=2, after r=4? last[2] still 2. At r=5, l=2, last[2]=2 >=2 -> l=3, last[2]=5, cur_len=3? (3-5)=3. l=3 >1, no update. r=6: color3, last[3]=3, l=3 -> l=4, last[3]=6, cur_len=3 (4-6) l=4 >1 no update. So final start=1, end=3. Good.
Now, case where later window has same length but earlier start? Actually earlier start is always less than later start, so if we update only when l < best_start, the earliest will be kept. But what if there is a window with same length but starting earlier than the previously recorded best_start? This can happen if we first encounter a window of maximum length that starts later, and later we find a window of same maximum length that starts earlier? But scanning left to right, as r increases, the left pointer l for each window is non-decreasing? Actually l can only increase when we encounter a duplicate. So as we move r forward, l moves only forward (or stays). So the start index l for windows is non-decreasing over r. Therefore, as we go, the start indices of valid windows we encounter are monotonic non-decreasing. So if we encounter a window of length L at some r, its start l is at least as large as the start of any previous window we've seen? Actually, l can sometimes jump forward, but never backward. So as r increases, l never decreases. So the start index of the current valid window (for increasing r) is non-decreasing. Therefore, when we first encounter a window with length equal to the maximum length so far, its start l is the smallest possible start among windows of that length? Not necessarily: because the maximum length could appear later with a smaller start? Wait, if l is non-decreasing, then later windows have l >= earlier windows. So the smallest start among all windows (with any length) that we encounter as we increase r is the earliest l we see. But can there be a window of maximum length that starts earlier than the first time we achieve that maximum length? Let's think: We might achieve a certain length, say best_len, at some r1 with start l1. Later, we might achieve the same length again but with a start l2 that is greater than l1 (since l is non-decreasing). So later windows have start >= earlier windows. So the earliest start among all maximum-length windows will be the first time we achieve that maximum length (or possibly earlier if we achieved a shorter length earlier? but we care about max length). So the earliest maximum-length window will be the one with the smallest start, which is also the one that occurs earliest in the scanning order? Actually since l is non-decreasing, among all windows with the same length, the ones that appear earlier in the scan (i.e., with smaller r) will have smaller l. So the first time we achieve the maximum length, that window will have the smallest l among all windows of that maximum length. Is that always true? Consider scenario where we achieve a window of length L at r1 with start l1. Later, we might achieve a window of length L again at r2 with start l2, and because l is non-decreasing, l2 >= l1. So indeed, the earliest maximum-length window has the smallest start. So we can simply keep the first maximum-length window we encounter and not update for equal lengths. That would give the earliest start? Wait, but consider the case where initially we encounter a window of length 3 starting at 2 (best_len=3). Later we encounter another window also of length 3 but starting at 1? Can that happen? If l is non-decreasing, later window cannot have start 1 if earlier window had start 2, because l can't go from 2 down to 1. So later start cannot be smaller. So indeed the first maximum length window we encounter has the smallest start among all windows of that length. Therefore, we can simply update only when cur_len > best_len, and ignore equal lengths. That would retain the first window with that length, which is earliest. However, careful: Is it possible that we first achieve a length of, say, 3 at r=5 with start=3, then later at r=6 we also get length=3 but start=2? But l cannot decrease, so start=2 is less than 3, so that would be a decrease, impossible because l only moves forward (non-decreasing). So l can't go from 3 to 2. So indeed, once we achieve a certain best_len, any later window with same length will have start >= the start of the first such window. So earliest start is the first occurrence of that maximum length. So we can skip the equality check with l < best_start and just update only on >. That would produce the correct earliest start? Let's test with sample case 2: We had best_len become 3 at r=4 with start=2. Later at r=6 we got len=3 with start=4. Since we only update on >, we keep start=2. That's correct. In case of tie where we first see a length L but later we see a length L that starts earlier? Not possible as argued. So we can simplify: no need to check equality for start. However, what about the scenario where we first achieve length L but at that time best_len was less than L (so we update). Later we might see another window with length L but with smaller start? That can't happen because start for the later window is >= start for the first due to monotonicity of l. So yes.
But wait: The left pointer l is not monotonic across r? In sliding window algorithm, l only increases (or stays same) when we encounter a duplicate that forces it to move forward. So l is non-decreasing. So indeed, as r increases, the start index of the current valid window is non-decreasing. So among all windows we see, the earliest in terms of r will have the smallest l. So if we want earliest start among all windows with maximum length, we should take the first window that attains that maximum length. However, is it possible that there exists a window with maximum length that ends at some r < the r where we first achieved that maximum length? That would contradict "first achieved". Because if there is a window of maximum length that ends earlier, then when we reached that earlier r, we would have had that length. So the first time we achieve that maximum length must be the earliest ending index among windows of that length. But we want earliest start, not necessarily earliest ending. Could there be a window with maximum length that starts earlier but ends later? For example, suppose we have array: [1,2,3,1,4,5]. The longest distinct subarray? Let's find: [1,2,3] length3 start1; [2,3,1] length3 start2; [3,1,4] length3 start3; [1,4,5] length3 start4; [4,5] length2; Actually also [2,3,1,4]? That contains duplicate? 2,3,1,4 all distinct? Wait, 2,3,1,4 are distinct, but check indices: 2,3,4,5? That's colors: index2=2, index3=3, index4=1, index5=4 -> all distinct length4? Actually indices: 2,3,4,5 are 2,3,1,4 distinct -> length4! So let's recalc: array: [1,2,3,1,4,5] at positions:1:1,2:2,3:3,4:1,5:4,6:5. The longest distinct subarray: from index2 to index5? 2-5: 2,3,1,4 distinct length4; from index3 to index6: 3,1,4,5 distinct length4; also maybe from index1? 1,2,3 distinct length3; So max length is 4. Which start is earliest among max length? start2 (index2) and start3. earliest is start2. Now, sliding window algorithm: r=1: l=1 len1; r=2: len2; r=3: len3; r=4: color1, last[1]=1 >= l=1 => l=2, set last[1]=4, len = r=4, l=2 -> len=3? Actually 4-2+1=3; but is that valid? Colors: index2=2, index3=3, index4=1 -> distinct, len=3. At r=5: color4, l=2, last[4]=0, so l stays2, set last[4]=5, len = 5-2+1=4. So at r=5, we get len=4, start=2. That's the first time we achieve length 4. Later r=6: color5, l=2, last[5]=0, len=5 (6-2+1=5)? Actually check duplicates: at r=6, colors seen: index2=2,3,4=1,5=4,6=5 all distinct, so length5? But wait, do we have duplicate? 2,3,1,4,5 all distinct, yes length5. So r=6 len=5, start=2. That's >4. So maximum length becomes 5. So earlier maximum length of 4 is superseded. So first time we get length 4 is at r=5, start=2. Later we get length5. So among length4 windows, the earliest start is indeed start2, which matches first occurrence of length4. So the logic holds.
Now consider an edge case: suppose we achieve a maximum length of L at r1 with start l1. Later, we may get another window of length L at r2 with start l2, and l2 could be equal to l1? Possibly if the window slides? Actually if l1 = l2, then it's essentially the same start, but different end? But length same L, so if l1 = l2, then r1 = l1+L-1, r2 = l2+L-1, so r2 > r1. So two windows of same length and same start but different end? That would mean extending the window while keeping start same, but then length would increase because r increases. So can't have same length and same start with different r; if start same and r larger, length larger. So not possible. So later windows with same length must have start > l1.
Thus the first occurrence of a given length yields the smallest start among all windows of that length.
Therefore, we can simply update best only when cur_len > best_len. No need to check equality. That will automatically give earliest start because when we first hit a new max length, we record its start. Later equal lengths will be ignored, and they have later starts.
But wait, what if there is a tie at the very beginning? For example, array: [1,2,1,2]? The longest distinct subarray length is 2 (since there are duplicates). There are multiple windows of length 2: [1,2] at start1, [2,1] at start2, [1,2] at start3? Actually start3: index3=1, index4=2 -> length2. Among these, earliest start is 1. Our algorithm: r=1: len1, best=1 start1; r=2: len2, best=2 start1; r=3: color1, last[1]=1 >= l=1 => l=2, len=2 (2-3) -> cur_len=2 not >2; r=4: color2, last[2]=2 >= l=2 => l=3, len=2 (3-4) not >2. So final start1 end2. Good.
Thus we can simplify.
But is there any scenario where the earliest maximum-length window does not appear as the first time we achieve that length? Suppose we have array where the first time we get a window of length L, but later we get another window also of length L that starts earlier? As argued, l is non-decreasing, so later window cannot start earlier. So impossible.
But careful: l is non-decreasing in our algorithm, but is that guaranteed for all valid windows? In the sliding window algorithm we maintain the leftmost l such that [l, r] has all distinct colors. When we move r, we adjust l only by increasing it if needed. So indeed, l never decreases. So monotonic.
Thus we can update only on >.
Nevertheless, the problem statement explicitly says: "if there are multiple such occurrences of longest span, he wants to know the earliest such span". So we must ensure we output the earliest. Since our algorithm with only > will give earliest, that's fine.
But some might argue: what if the longest span appears multiple times with same length and same start? That's irrelevant.
So solution: sliding window, update best only when length > best_len.
But wait: Could there be a scenario where we first encounter a length L1, then later we get a longer length L2 > L1, but L2's start is later, and then later we get a window of length L1 again but with an earlier start than the first L1? No, because after we moved l forward for later windows, we cannot go back to earlier start. So L1's start will be later if we see it again.
Thus our simplified approach works.
But let's double-check: The typical algorithm for longest substring without repeating characters usually records the maximum length, and if we need the substring itself, we keep track of start. But they often update when length >= maxLen, and if length > maxLen update, else if length == maxLen and start < minStart update. But since l is non-decreasing, the equal case with earlier start can't happen if we process sequentially? Actually, it could happen if we update not only on new r but also on some internal condition? Wait, consider that we might have two different windows of same length but one appears later with smaller start? That would require l to decrease, which it doesn't. So no.
But there is a nuance: In some implementations, we might update best_start when we move l? Actually we only evaluate at each r. So as argued.
Thus we can implement with only >.
However, I've seen some coding problems where they ask for the earliest start and they need to check equality. But in those problems, the approach may be different, e.g., they might compute max length separately and then find earliest start that yields that length. But sliding window with monotonic l works.
Better to be safe: we can include equality check with l < best_start, but it's not necessary. Including it doesn't hurt but adds a tiny overhead. I'll include it to be safe, especially if any corner case where monotonicity could be broken by something else? Actually our l is strictly non-decreasing, but could it stay same? Yes, but that doesn't produce a smaller start. So no issue.
But think about if we use unordered_map and the condition lastPos[color] >= l, and we update lastPos after moving l. That's standard.
One more nuance: The colors are integers up to 1e9. So we need a hashmap. In C++, unordered_map can be used, but we should be cautious about worst-case O(N) per operation if many collisions. But N is 1e5, so even O(N) in worst-case would be too much? But average O(1). Alternatively, we could use a map (ordered) with O(log N) per operation, total O(N log N) which for N=1e5 is fine (1e5*log(1e5) ~ 1.7e6). T=10 gives 17e6 operations, still fine. So either is okay. But unordered_map might be faster generally. However, in competition platforms, sometimes unordered_map can cause TLE due to anti-hash tests. But with N=1e5, it's probably okay. To be safe, we can use map. But 1e5 * log(1e5) is fine. I'd probably use unordered_map with reserve to avoid rehashing.
Alternatively, we can compress colors: Since N <= 1e5, we can map colors to indices from 1..M where M <= N. That would allow using an array of size N+1 for last occurrence. That would be O(N) and deterministic. Approach: read all colors into a vector, then sort unique, map each to an integer, then run sliding window using an array lastPos[M+1] initialized to 0. This would be faster and avoid hash issues. But we need to read all colors first (or we can read, compress, then process). Since we earlier considered reading on the fly to save memory, but compressing requires storing all colors anyway. But we can read all into vector, compress, then simulate with the mapped indices. That is also O(N log N) due to sorting, which is fine. But the sliding window with array is O(N). However, the sorting adds extra O(N log N). Still okay. But for simplicity, using unordered_map is easiest. Given constraints, it should pass. But to be robust, I'll use unordered_map with reserve.
But note: We must clear the map for each test case. Also, we might need to free memory. We'll declare inside loop.
Implementation in C++:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while(T--) {
int N;
cin >> N;
unordered_map<int, int> lastPos;
lastPos.reserve(N); // optional
int l = 1;
int best_len = 0, best_start = 1, best_end = 1;
for (int r = 1; r <= N; ++r) {
int color;
cin >> color;
auto it = lastPos.find(color);
if (it != lastPos.end() && it->second >= l) {
l = it->second + 1;
}
lastPos[color] = r;
int cur_len = r - l + 1;
if (cur_len > best_len) {
best_len = cur_len;
best_start = l;
best_end = r;
}
// optional equality check:
// else if (cur_len == best_len && l < best_start) {
// best_start = l;
// best_end = r;
// }
}
cout << best_start << " " << best_end << "\n";
}
return 0;
}
But careful: In condition we used if (it != lastPos.end() && it->second >= l). That's good to avoid creating default entry. Actually we used find, not operator[]. So we avoid unnecessary insertion until later. That's fine.
Alternatively, we can do:
int prev = lastPos[color]; // but that would insert if not present. That's okay, but we may want to avoid insertion before we know l? It still works because we would set it to 0 initially (since int()=0). However, after we set lastPos[color]=r, it overwrites. That's fine. But using find is slightly more efficient and doesn't create entry prematurely. But both are okay.
Now, verify with sample input: Should output "1 5" and "2 4". Yes.
Now, double-check large input: N=1e5, T=10 -> total 1e6. The algorithm is O(N) per test with hashmap, fine.
Potential pitfalls:
The array may have all distinct colors, then best_len = N, best_start = 1. That's fine.
When N=0? Not possible; N>=1.
Colors may be negative? No, 1 ≤ color ≤ 10^9.
The output should be 1-indexed.
Now, think about tie-breaking again: The problem says "if there are multiple such occurrences of longest span, he wants to know the earliest such span because his wife will get angry is he delays for too long." That suggests that among all spans of maximum length, we pick the one that starts earliest. Our algorithm with > only picks the first such span. As argued, first such span has earliest start. So correct.
But is it possible that the first such span in terms of ending index does not have the earliest start? For a given maximum length L, the windows that achieve length L are all subarrays of length L with distinct elements. The set of start indices for these windows is some subset. The earliest start is the minimum start. The first time we encounter a window of length L while scanning from left to right, we encounter the window with the smallest ending index among those windows? Actually, scanning r from 1 to N, when we hit a window of length L, its ending index is the smallest r for which there exists a valid window of length L. This window's start is l = r - L + 1. Could there be another window of length L with a smaller start but ending later? That window would have start s < l, and end e = s + L - 1 > r (since s < l implies e = s+L-1 < l+L-1? Actually if s < l, then e = s + L - 1 < l + L - 1 = r? Wait l = r - L + 1, so l + L - 1 = r. So s < l => s + L - 1 < l + L - 1 = r. So if s < l, then e < r. That would be a window of length L ending earlier than r. But then we would have encountered it when r = e, because at that time we would have a valid window of length L (maybe not? Let's examine). If there exists a window of length L with start s < l and end e = s+L-1, then at r = e, we would have some left pointer l' such that [l', e] is the maximal valid window ending at e? But the algorithm ensures that for each r, l is the smallest left index such that [l, r] is valid. So at r = e, we would have some l' ≤ s (since s is a valid left index for that r). Actually we need to check: If there is a valid window [s, e] with distinct elements, then at r = e, the left pointer l' computed by the algorithm is the smallest index such that [l', e] is valid. Since s is a valid left index, we have l' ≤ s. Also, the length of the valid window [l', e] is at least L (since l' ≤ s, length = e - l' + 1 ≥ e - s + 1 = L). It could be greater than L if l' < s and still distinct. But could it be that l' > s? No, because l' is the smallest index making the window valid; if s works, l' cannot be greater than s. So l' ≤ s. Then the length at r = e would be at least L. So at that r, cur_len ≥ L. If cur_len > L, then we would have already set best_len to something >= L (maybe larger). But if cur_len == L exactly, then l' = s. So at r = e we would have a window of length L. Since e < r (our current r), that would have been encountered earlier. So the first time we achieve length L would be at r = e, not at the later r. So there cannot be a window with start earlier than the first length L window's start, because that would have ended earlier and been discovered first. Therefore, the first length L window we encounter indeed has the earliest start. So monotonicity argument holds.
Thus we can safely update only on >.
Nevertheless, some coders might implement with equality check to be safe, it's fine.
Now, about memory: unordered_map may be cleared each test, but it's okay.
Now, test with some random small cases to confirm.
We'll also consider an alternate approach: keep a set or map for counts, but sliding window with last occurrence map is more efficient.
Now, ensure we handle input correctly. Use scanf/printf for speed? I'll use cin with sync off; that's fast enough for 1e6 ints.
Now, produce final solution.
But note: The problem says "the color predicted by his cards for the next N days" and "he wants to find the longest vacation span he can get if he wears a different color t-shirt each day." That's exactly longest subarray with all distinct elements.
Now, consider possibility of N up to 10^5, but sum of N across test cases maybe up to 10^6? T ≤ 10, so max sum 10^6, fine.
We'll produce code.
Potential improvements: Use std::map for deterministic O(N log N). I'll stick with unordered_map but include reserve to avoid rehash.
Edge: Input could have spaces and newlines. Use cin.
Now final answer.
But I want to double-check: The sliding window algorithm as I wrote using lastPos.find and then updating lastPos[color] = r. But note: The condition if (it != lastPos.end() && it->second >= l) is correct: if we've seen the color before and its index is at or after l (i.e., within current window), we move l to that index+1. After moving l, we set lastPos[color] = r. That's correct.
But what if the color appears but its last occurrence is before l? Then we don't need to move l, and we update lastPos to r. Good.
Now, one nuance: Suppose we have color at r that was seen earlier at index p, and l <= p. Then we set l = p+1. After that, we set lastPos[color]=r. That's fine. But what about if we had multiple duplicates within the window? The map holds only the most recent occurrence, which is enough because we only need to know the latest duplicate. That's correct.
Now, we need to ensure we output start and end day numbers (1-indexed). Our l and r are 1-indexed. So fine.
Now, produce final solution code.
But wait, need to handle T test cases: Input format: first line T. Then for each test case: first line N, second line N integers. There may be multiple lines? Actually it says "Second line of every test case contains the N colors". So exactly one line with N integers. But sometimes the input might have line breaks in between; but we can just read integers sequentially. So using cin will work.
Now, include necessary headers.
Now, final code in answer.
I'll provide code with unordered_map and equality check for safety.
Also, to avoid potential issues with unordered_map performance, we could use a vector of pairs to store last occurrence after compressing colors, but I'll keep it simple.
But I recall that sometimes on platforms like CodeChef, using unordered_map without custom hash can lead to collisions on large data. But with N=1e5, it's likely okay. However, to be safe, I might use map. map is O(log N), but still fine for 1e5. Let's compute: 1e5 * log2(1e5) ~ 1.7e6 operations per test, times 10 = 17e6, easily under 1 sec in C++. So map is safe. I'll use map to avoid any hash issues.
Actually unordered_map should be okay too. I'll use unordered_map but with .reserve(N). But I'll write code with both options and comment.
Given that the original constraints award 40 pts for original (N up to 1e5), both are acceptable.
I'll choose map for simplicity and reliability.
Implementation with map:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
map<int, int> lastPos;
int l = 1;
int best_len = 0, best_start = 1, best_end = 1;
for (int r = 1; r <= N; ++r) {
int color;
cin >> color;
auto it = lastPos.find(color);
if (it != lastPos.end() && it->second >= l) {
l = it->second + 1;
}
lastPos[color] = r;
int cur_len = r - l + 1;
if (cur_len > best_len) {
best_len = cur_len;
best_start = l;
best_end = r;
}
}
cout << best_start << " " << best_end << "\n";
}
return 0;
}
That should work.
Now, test with sample:
2
5
1 2 3 4 5
6
1 2 1 3 1 5
Outputs: 1 5 and 2 4.
Edge: T=1, N=1, color=5 -> output "1 1".
All good.
Now, consider time: map operations O(log M) where M ≤ N. For N=1e5, about 17e5 operations per test, fine.
Memory: map stores up to N entries.
Thus solution is solid.
Now, produce final answer with explanation.
We'll include reasoning in solution description: sliding window, two pointers, track last occurrence, update best window.
Now, produce final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
code
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
// last occurrence (1-indexed) of each colour
map<int, int> lastPos;
int left = 1; // left end of current valid window
int best_len = 0, best_start = 1, best_end = 1;
for (int right = 1; right <= N; ++right) {
int colour;
cin >> colour;
// if this colour is already inside the current window, shrink from left
auto it = lastPos.find(colour);
if (it != lastPos.end() && it->second >= left) {
left = it->second + 1;
}
// update last occurrence of this colour
lastPos[colour] = right;
int cur_len = right - left + 1;
// keep the earliest longest window
if (cur_len > best_len) {
best_len = cur_len;
best_start = left;
best_end = right;
}
}
cout << best_start << " " << best_end << "\n";
}
return 0;