#C12175. Longest Contiguous Subarray with Equal Even and Odd Count
Longest Contiguous Subarray with Equal Even and Odd Count
Longest Contiguous Subarray with Equal Even and Odd Count
You are given an array of non-negative integers. Your task is to find the length of the longest contiguous subarray that contains an equal number of even and odd integers.
Formally, let \(even\) be the number of even integers and \(odd\) be the number of odd integers in a subarray. You need to find the maximum length of a contiguous subarray such that \(even = odd\).
It is guaranteed that the input will be valid. Consider the following examples:
- For the array [1, 2, 3, 4], the longest subarray is [1, 2, 3, 4] with a length of 4.
- For the array [1, 1, 2, 2], the entire array is balanced and the length is 4.
- For the array [2, 4, 6, 8], no subarray is balanced so the answer is 0.
inputFormat
The first line contains a single integer \(n\) representing the number of elements in the array. The second line contains \(n\) space-separated non-negative integers.
If \(n = 0\), then the array is empty.
outputFormat
Output a single integer denoting the length of the longest contiguous subarray with an equal number of even and odd integers.
## sample4
1 2 3 4
4