#K42217. Longest Even Subarray
Longest Even Subarray
Longest Even Subarray
Given multiple datasets where each dataset consists of an integer n
followed by a line of n
integers, your task is to determine the length of the longest contiguous subarray that contains only even numbers. The input terminates when a dataset with n = 0
is encountered.
You need to compute the longest subarray in each dataset such that every number in the subarray is even. Formally, for a given array \(A\), find the maximum \(L\) such that there exists an index \(i\) with a contiguous block \(A[i], A[i+1], \ldots, A[i+L-1]\) and for every \(j\) in \([i, i+L-1]\), \(A[j] \equiv 0 \; (\text{mod }2)\). If no even numbers exist in the array, the answer is 0.
inputFormat
The input consists of multiple test cases. Each test case is described in two lines:
- The first line contains a single integer
n
(\(1 \leq n \leq 10^5\)), the number of elements in the array. A value of0
indicates the end of input and should not be processed. - The second line contains
n
space-separated integers.
You should read input from standard input (stdin).
outputFormat
For each test case, output a single integer on a separate line representing the length of the longest contiguous subarray which contains only even numbers. Write the result to standard output (stdout).
## sample6
1 2 4 6 1 2
5
5 7 9 2 4
0
3
2
</p>