#C8616. Longest Even-Odd Subarray
Longest Even-Odd Subarray
Longest Even-Odd Subarray
Given an array of integers, find the length of the longest contiguous subarray that contains an equal number of even and odd integers.
To formalize, let the subarray be denoted by indices from i to j (0-indexed). Define the balance function as:
$$B(k)=\text{number of even numbers in }[0,k] - \text{number of odd numbers in }[0,k]. $$A subarray from i+1 to j (inclusive) has an equal number of even and odd numbers if and only if:
Your task is to compute the maximum length of such a subarray. If no such subarray exists, output 0.
inputFormat
The first line contains an integer n (1 ≤ n ≤ 105), representing the number of elements in the array.
The second line contains n space-separated integers, representing the elements of the array.
outputFormat
Output a single integer which is the length of the longest contiguous subarray that has an equal number of even and odd integers.
## sample6
1 2 1 2 3 4
6