#K70282. Longest Even Sum Subarray
Longest Even Sum Subarray
Longest Even Sum Subarray
You are given an array of integers. Your task is to compute the length of the longest contiguous subarray for which the sum of its elements is even. A subarray is a contiguous portion of the array. In mathematical terms, if the array is A = [a_1, a_2, \dots, a_n], you need to find the maximum length L such that there exist indices i and j with 1 \le i \le j \le n for which
[ a_i + a_{i+1} + \dots + a_j \equiv 0 \pmod{2} ]
If the sum of the entire array is even, then the answer is simply the length of the array. Otherwise, by removing one odd number (either from the beginning or the end) you can achieve an even sum. This problem tests your ability to efficiently process arrays and implement simple greedy strategies.
inputFormat
The input begins with a single integer n
indicating the number of elements in the array. The next line contains n
space-separated integers representing the elements of the array.
For example:
5 1 2 3 4 5
outputFormat
Output a single integer which is the length of the longest contiguous subarray with an even sum.
For example:
4## sample
5
1 2 3 4 5
4
</p>