#C3348. Shortest Even Product Subarray
Shortest Even Product Subarray
Shortest Even Product Subarray
You are given an array of integers, and your task is to find the length of the shortest contiguous subarray whose product is even. Recall that a product of numbers is even if at least one of the factors is even. Therefore, the answer will be 1 if any element of the array is even, and -1 otherwise.
More formally, given an array \(A = [a_1, a_2, \ldots, a_n]\), find the minimum length \(L\) of a contiguous subarray such that \[ \prod_{i=s}^{s+L-1} a_i \equiv 0 \pmod{2}, \] for some valid starting index \(s\). If no such subarray exists, output \(-1\).
Note: Since a single even number is enough to have an even product, you only need to check if the array contains any even number.
inputFormat
The input is read from standard input (stdin). The first line contains an integer \(T\) representing the number of test cases. Each test case consists of two lines. The first line of a test case contains an integer \(n\), the number of elements in the array. The second line contains \(n\) space-separated integers representing the array elements.
For example:
3 4 1 5 3 7 5 2 4 6 8 10 3 3 7 5
outputFormat
For each test case, output a single line containing the answer: the length of the shortest subarray with an even product, which will be 1 if the array contains any even numbers or -1 if it does not.
For the sample input above, the output should be:
-1 1 -1## sample
3
4
1 5 3 7
5
2 4 6 8 10
3
3 7 5
-1
1
-1
</p>