Learn DSADSAContests
coding75 logo
LeetCode POTDPOTDSheets
coding75 Pro
coding75 logo
Dashboard
Learn DSA
Course RoadmapFlow
DSA Topic TreeNew πŸš€
Contest SolutionsPractice
Practice Sheets
MasterclassesLive πŸ‘¨πŸ»β€πŸ’»

coding75 ProPRO

Live Classes & Placement Guidance

coding75 logo

The premier developer platform for mastering Data Structures & Algorithms, exploring contest editorials, building ATS-ready resumes, and accelerating your tech career.

Connect & Community

DSA & Contests

  • Learn DSA
  • Contest Solutions
  • LeetCode POTDDaily
  • Practice Sheets
  • MasterclassesSoon

Interview Prep

  • Portfolio Projects
  • CS Fundamentals
  • System DesignSoon
  • Interview ExperiencesSoon
  • Mock InterviewsSoon

Career & Pro

  • Jobs & Internships
  • Resume BuilderATS
  • coding75 Pro
  • Submit Feedback
Β© 2026coding75β€’crackDSAβ„’β€’Maa Lalita Edtech Private Limited. All Rights Reserved.
Privacy PolicyTerms & ConditionsContact Support
Registered Office: Kanpur 208021β€’Regional Office: Indiranagar, Bangalore, 560008
Maa Lalita Edtech Private Limited
Contests/Codeforces/Codeforces Round 1096 (Div. 3)/D. Palindromex
PrevNext
CodeforcesCodeforces
Specialist
Codeforces Round 1096 (Div. 3)

D. Palindromex

MathArrayGreedy
Loading...
Solve on Codeforces

Problem Statement

Official algorithmic problem description and constraints.

Open on Codeforces

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:

  • The mex
  •  of [2,2,1]
  •  is 0
  • , because 0
  •  does not belong to the array.
  • The mex
  •  of [3,1,0,1]
  •  is 2
  • , because 0
  •  and 1
  •  belong to the array, but 2
  •  does not.
  • The mex
  •  of [0,3,1,2]
  •  is 4
  • , because 0
  • , 1
  • , 2
  •  and 3
  •  belong to the array, but 4
  •  does not.

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

.


Solutions & Walkthrough

Detailed video explanations, mathematical intuition, and clean C++ implementation code.

Connecting secure classroom stream...
Back to Codeforces Round 1096 (Div. 3)
Previous ProblemNext Problem