← 返回 instacart 的题目列表Count Subarrays With Alternating Parity (Odd/Even) Including Negatives
类型:online_judge
Given an integer array nums (may contain negative numbers), count how many contiguous subarrays have strictly alternating parity between adjacent elements.
Formally, a subarray nums[l..r] is valid if for every i with l < i <= r, nums[i] and nums[i-1] have different parity (one odd, one even).
Negative numbers follow normal parity rules (e.g., -3 is odd, -4 is even).
Input
Line 1: integer n
Line 2: n integers
Output
The number of valid subarrays.
Constraints
1 <= n <= 2*10^5
-1e9 <= nums[i] <= 1e9
Example
Input:
5
1 2 3 4 6
Output:
11
Example
Input
5
1 2 3 4 6
Output
11