#K75757. Longest Even-Odd Balanced Subarray
Longest Even-Odd Balanced Subarray
Longest Even-Odd Balanced Subarray
You are given T queries. In each query, you are given an integer n and an array of n integers. For each query, find the maximum length of a contiguous subarray whose count of even and odd numbers is equal.
Let \(a_1, a_2, \ldots, a_n\) be the elements of the array. A contiguous subarray \(a_l, a_{l+1}, \ldots, a_r\) is called balanced if the number of even integers in the subarray equals the number of odd integers. If no such subarray exists, the answer for that query is 0.
Input Format: The first line contains the number of queries, \(T\). For each query, the first line contains an integer \(n\), and the second line contains \(n\) space-separated integers.
Output Format: For each query, output the maximum length of a balanced subarray on a new line.
inputFormat
The input begins with an integer T representing the number of queries. Each query consists of two lines: the first line contains an integer n, representing the number of elements in the array, and the second line contains n space-separated integers.
outputFormat
For each query, output a single integer on a new line, indicating the maximum length of a subarray that contains an equal number of even and odd integers. If no such subarray exists, output 0.## sample
3
6
1 2 2 3 4 5
5
1 1 1 1 1
4
2 4 6 8
6
0
0
</p>