Subarrays with Odd Zero Frequency
Arrays & HashingAuto-graded · 20 test casesLast updated
Tests understanding of prefix parity tracking and combinatorial counting to efficiently solve subarray problems.
You're given an integer array nums. Your task is to determine how many contiguous subarrays contain an odd number of zeros.
A subarray is a contiguous non-empty slice of the array. For example, in [1, 0, 2], the subarrays are [1], [0], [2], [1, 0], [0, 2], and [1, 0, 2].
Return the total count of subarrays where the number of zeros is odd (1, 3, 5, etc.).
Constraints
1 <= nums.length <= 100,000-10^9 <= nums[i] <= 10^9- Your solution should run in O(n) time
Examples
Example 1
Input: count_odd_zero_subarrays([0, 0, 1, 0, 1])
Output: 9
Example 2
Input: count_odd_zero_subarrays([1, 2, 3])
Output: 0
Example 3
Input: count_odd_zero_subarrays([0])
Output: 1
Example 4
Input: count_odd_zero_subarrays([0, 1, 0, 0])
Output: 6
Hints
Hint 1
Think about the parity (even or odd) of zero counts in prefixes rather than the actual counts themselves.
Hint 2
A subarray from index i to j has an odd zero count if and only if the prefix ending at j and the prefix ending before i have different parities.
Hint 3
Track how many prefixes have even parity and how many have odd parity as you scan through the array.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is to track the parity (even or odd) of zero counts in prefixes rather than the actual counts themselves.
For any subarray from index i to j, the number of zeros equals the difference between the zero count in the prefix ending at j and the prefix ending just before i. A difference of two integers is odd if and only if those integers have different parities.
Therefore, a subarray has an odd zero count exactly when its two boundary prefixes have different parities.
We solve this in one pass:
-
Maintain two counters:
even_count(prefixes with even zero parity) andodd_count(prefixes with odd zero parity). Initializeeven_count = 1to represent the empty prefix. -
Track
current_parityas we scan through the array. Flip it whenever we encounter a zero. -
At each position, count how many valid subarrays end at this position. If the current prefix has odd parity, it pairs with all previous even-parity prefixes. If even, it pairs with all previous odd-parity prefixes.
-
After using the current prefix for counting, add it to the appropriate parity bucket.
Why It Works
Each subarray corresponds to exactly one pair of prefix positions with opposite parities. By processing positions left-to-right and maintaining running counts of each parity type, we count each valid pair exactly once without double-counting or missing any.
The initialization of even_count = 1 accounts for the empty prefix (before the array starts), which allows subarrays starting at index 0 to be counted correctly.
Complexity
- Time complexity: O(n) — single pass through the array with constant work per element
- Space complexity: O(1) — only a fixed number of integer variables
Solution
from typing import List
def count_odd_zero_subarrays(nums: List[int]) -> int:
even_count = 1
odd_count = 0
result = 0
current_parity = 0
for num in nums:
if num == 0:
current_parity ^= 1
if current_parity:
result += even_count
odd_count += 1
else:
result += odd_count
even_count += 1
return result