Official algorithmic problem description and constraints.
Yousef has given you an array π
of 2π
integers. Every integer π₯β[0,πβ1]
appears exactly twice in the array.
Your task is to find a subarray π
π
,π
π+1
,β¦,π
π
that is a palindrome
β
such that its mex(π
π
,π
π+1
,β¦,π
π
)
β
is maximized. Output this maximum possible value.
β
A palindrome is a string that reads the same backward as forward, for example strings "z", "aaa", "aba", "abccba" are palindromes, but strings "codeforces", "reality", "ab" are not.
β
The mex
(minimum excludant) of an array of integers is defined as the smallest non-negative integer which does not occur in the array. For example:
Input
The first line contains a single integer π‘
(1β€π‘β€10
4
) β the number of test cases.
The first line of each test case contains a single integer π
(1β€πβ€10
5
).
The second line of each test case contains 2π
integers π
1
,π
2
,β¦,π
2π
(0β€π
π
β€πβ1
). It is guaranteed that every integer in the range [0,πβ1]
appears exactly twice.
It is guaranteed that the sum of 2π
over all test cases does not exceed 2β 10
5
.
Output
For each test case, output a single integer β the maximum mex
of any palindromic subarray.
Example
Input
Copy
6 4 1 2 0 3 3 0 2 1 2 0 1 0 1 2 1 1 0 0 3 2 0 2 1 1 0 4 0 1 3 0 3 1 2 2 3 0 1 2 1 0 2
Output
Copy
4 2 1 1 2 3
Note
In the first test case, the only optimal subarray to choose is π[1,8]=[1,2,0,3,3,0,2,1]
, which is palindromic and has a mex
of 4
.
In the second test case, one of the optimal subarrays to choose is π[2,4]=[1,0,1]
, which is palindromic and has a mex
of 2
.
In the third test case, we can choose π[3,3]=[0]
, which is palindromic and has a mex
of 1
. No other palindromic subarray has a mex
greater than 1
.
Detailed video explanations, mathematical intuition, and clean C++ implementation code.