Zapray recently did not have much time to finish his homework due to his addiction to Pokemon. What is even worse, this week’s algorithm class has assigned a very challenging homework problem: “Given an array of integers, find the longest consecutive sub-array so that each element in the sub-array appears exactly twice.”

Zapray remembers how to find the longest consecutive sub-array so that each element in the sub-array appears exactly once using sliding window. But he could not figure out how to extend it for this problem. Could you help Zapray?

-----Input-----
The first line of the input has an integer $N$ ($1 \leq N \leq 10^5$), the length of the array. The second line contains $N$ positive integers giving the elements of the array. Each integer is no larger than $10^5$.

-----Output-----
Output the length of the longest sub-array so that each element in the sub-array appears exactly twice.

-----Examples-----
Sample Input 1:
6
1 2 3 3 4 2
Sample Output 1:
2

Sample Input 2:
8
1 2 1 3 1 3 1 2
Sample Output 2:
4

Difficulty: competition

Starter code:

Solutions: