#K46812. Longest Valid Segment
Longest Valid Segment
Longest Valid Segment
You are given an array of n integers. Your task is to find the length of the longest contiguous segment (subarray) such that the sum of the elements in this segment is even. In other words, find the maximum value of r - l + 1 for indices l and r (0-indexed) satisfying
\( \sum_{i=l}^{r} a_i \equiv 0 \pmod{2} \).
If no such segment exists, output 0.
Note: A segment is defined as a contiguous subarray of the given array.
inputFormat
The input is given via standard input (stdin) and consists of two lines:
- The first line contains a single integer n representing the number of elements in the array.
- The second line contains n space-separated integers representing the array elements.
outputFormat
Output a single integer to standard output (stdout) representing the length of the longest contiguous segment with an even sum.
## sample5
1 2 3 4 5
4