#C9333. Longest Subarray with Equal Even and Odd Count

    ID: 53415 Type: Default 1000ms 256MiB

Longest Subarray with Equal Even and Odd Count

Longest Subarray with Equal Even and Odd Count

You are given an array of n integers. Your task is to find the length of the longest contiguous subarray which contains an equal number of even and odd numbers.

Formally, given an array \(A\) of length \(n\), find the maximum length \(L\) such that there exists indices \(i\) and \(j\) with \(0 \le i \le j < n\) and the subarray \(A[i \ldots j]\) satisfies:

[ \text{#evens} = \text{#odds} ]

If no such subarray exists, output 0.

Note: The array elements can be any integers. Even numbers are those divisible by 2, and odd numbers are the others.

inputFormat

The input is given in two lines:

  • The first line contains a single integer \(n\) (the number of elements in the array).
  • The second line contains \(n\) space-separated integers representing the array elements.

outputFormat

Output a single integer that is the length of the longest contiguous subarray having an equal count of even and odd numbers.

## sample
4
1 2 1 2
4