Under construction

Still being built. Things may change

The segment is not even.

Interview

Time limit1sMemory limit1024 MB

Summary
Find the longest contiguous subarray whose first half and second half contain the same multiset of values, where the halves are split at the midpoint.
Level

Medium4 of 10

Topics
Sliding window, Hash map, Array
Solved
No attempts yet

Problem

Two sequences ss and tt of equal length are even if they can be made equal to each other by repeating the following operation as many times as you want.

  • Choose two positive integers ii and jj with 1≤i<j≤∣s∣1 \leq i < j \leq |s|, and swap the values of sis_i and sjs_j.

Given a sequence AA of length NN, let A[l,r]=[Al,Al+1,…,Ar]A[l,r]=[A_{l},A_{l+1},\dots,A_{r}] be the contiguous subsequence containing only the elements from the ll-th to the rr-th element of AA. Find the maximum length of A[l,r]A[l,r] that satisfies the following condition.

Let m=⌊l+r2⌋m=\left\lfloor\frac{l+r}{2}\right\rfloor. Then A[l,m]A[l,m] and A[m+1,r]A[m+1,r] are even to each other.

Input

The first line contains an integer NN, the length of sequence AA. (1≤N≤5 0001 \leq N \leq 5\,000)

The second line contains NN integers A1,A2,…,ANA_{1},A_{2},\dots,A_{N}, separated by spaces. (1≤Ai≤1091 \leq A_i \leq 10^{9})

Output

Print the maximum length of A[l,r]A[l,r] when splitting the contiguous subsequence A[l,r]A[l,r] in half gives two sequences that are even to each other.

If no such contiguous subsequence exists, print 00.

Examples2

  1. Example 1

    Input
    3
    1 1 1
    
    Expected output
    2
    
  2. Example 2

    Input
    5
    1 2 3 2 3
    
    Expected output
    4