Back to problems

Count Subarrays With Alternating Parity (Odd/Even) Including Negatives

Algorithm · Instacart · Medium

Given an integer array nums, which can include negative values, determine the number of contiguous subarrays whose neighboring elements alternate between odd and even. More precisely, nums[l..r] qualifies when each index i satisfying l < i <= r has parity opposite to nums[i-1]; that is, one value is odd and the other is even. Use the usual parity interpretation for negative integers: for example, -7 is odd and -10 is even. Input First line: an integer n Second line: n…

Checking your access…