#K15291. Longest Even-Odd Subsequence
Longest Even-Odd Subsequence
Longest Even-Odd Subsequence
You are given T test cases. For each test case, a sequence of integers is provided. Your task is to determine the length of the longest contiguous subsequence that contains an equal number of even and odd integers.
The problem can be formulated using a balance approach. Define a balance value that increases by 1 for every even integer encountered and decreases by 1 for every odd integer. If two positions in the sequence have the same balance value, then the subarray between these positions will have an equal number of even and odd integers. The answer for each test case is the maximal length among such intervals. If no such subarray exists, the answer is 0.
Note: All mathematical formulas, when used, are expressed in LaTeX format. For example, the condition for a balanced subsequence can be written as \(\sum_{i=l}^{r}\delta(a_i)=0\), where \(\delta(a_i)=1\) if \(a_i\) is even and \(\delta(a_i)=-1\) if \(a_i\) is odd.
inputFormat
The input is read from standard input and consists of multiple test cases. The first line contains an integer \(T\) denoting the number of test cases. For each test case:
- The first line contains an integer \(N\) representing the length of the sequence.
- The second line contains \(N\) space-separated integers denoting the sequence.
outputFormat
For each test case, output a single integer on a new line representing the length of the longest contiguous subsequence with an equal number of even and odd integers. If no such subsequence exists, output 0.
## sample2
5
1 2 3 4 5
4
2 4 6 8
4
0
</p>