← 返回 meta 的题目列表Count Legal Subarrays with Alternating Even and Odd Numbers
类型:online_judge
Given a one-dimensional integer array where each element is either odd or even, a "legal subarray" is defined where adjacent elements must have different parities. That is, an odd number must be followed by an even number and vice versa. All subarrays must be contiguous portions of the original array. Your task is to count the number of such legal subarrays and output this number. For instance, in the array [1,2,3,7,8,9], the segment [1,2,3] forms an alternating sequence, with 6 legal subarrays ( [1], [2], [3], [1,2], [2,3], [1,2,3] ), and similarly the segment [7,8,9] also has 6 legal subarrays, thus the total is 12.
Example
Input
1 2 3 7 8 9