#C3733. Longest Contiguous Subarray with Same Parity
Longest Contiguous Subarray with Same Parity
Longest Contiguous Subarray with Same Parity
You are given an integer n and an array of n integers. Your task is to find the length of the longest contiguous subarray in which every element has the same parity (i.e., all even or all odd).
If n is zero, the answer is 0. Otherwise, calculate the maximum length of a subarray where consecutive elements share the same parity.
Note on Parity: An integer is even if it is divisible by 2 without remainder, and odd otherwise. The parity of an integer x is determined by the expression \( x \mod 2 \).
Example:
Input: 7 5 2 2 6 6 5 7 Output: 4
In the sample above, the longest contiguous subarray with the same parity is either the sequence [2, 2, 6, 6] or [6, 6, 5, 7] (both yield a length of 4 when considering parity changes).
inputFormat
The first line of input contains a single integer n denoting the number of elements in the array. The second line contains n space-separated integers representing the array elements.
If n is 0, the array will be empty.
outputFormat
Output a single integer representing the length of the longest contiguous subarray where all elements have the same parity.
## sample7
5 2 2 6 6 5 7
4