Contiguous Array

MediumHashing
Next
ArrayPrefix SumHash Map

Given a binary array of 0s and 1s, find the length of the longest contiguous subarray that contains an equal number of 0s and 1s.

Input

Line 1: n. Line 2: n values, each 0 or 1.

Output

Print the length of the longest balanced subarray (0 if none exists).

Constraints

1 <= n <= 10^5

Example 1
Input
2
0 1
Output
2

The whole array has one 0 and one 1.

Example 2
Input
3
0 1 0
Output
2

The first two (or last two) elements are balanced.

Hints

Contiguous Array - Medium Hashing Problem | Skill Shiksha