#C7039. Longest Equal Even-Odd Subarray

    ID: 50866 Type: Default 1000ms 256MiB

Longest Equal Even-Odd Subarray

Longest Equal Even-Odd Subarray

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

This problem can be reduced to finding the longest subarray with a sum of zero after transforming the array as follows: assign +1 for even numbers and -1 for odd numbers. Mathematically, if we denote the transformed value as \(a_i\), then we need to find the longest subarray \([l, r]\) such that:

[ \sum_{i=l}^{r} a_i = 0 ]

Solve the problem using an efficient algorithm that computes prefix sums and uses a hash map or equivalent data structure to record the first occurrence of each prefix sum.

inputFormat

The input is read from stdin and consists of two lines:

  • The first line contains an integer \(n\) denoting the number of elements in the array.
  • The second line contains \(n\) space-separated integers representing the array elements. If \(n = 0\), the second line is omitted.

outputFormat

Output a single integer to stdout representing the length of the longest contiguous subarray with an equal number of even and odd numbers.

## sample
6
1 2 3 4 5 6
6