Last Updated: April 2, 2026
We need to count every contiguous subarray that consists entirely of zeros. A single 0 counts as one subarray. Two consecutive zeros give us three subarrays: [0] at the first position, [0] at the second position, and [0,0] spanning both. Three consecutive zeros give us six: three singles, two pairs, and one triple.
Notice the pattern? A run of k consecutive zeros produces k*(k+1)/2 zero-filled subarrays. That's because we can pick any start and end within the run, and the number of ways to do that is the sum 1 + 2 + 3 + ... + k.
This observation is the key to solving the problem efficiently.
1 <= nums.length <= 10^5 → With up to 100,000 elements, we need O(n) or O(n log n) at most. An O(n^2) brute force risks TLE.-10^9 <= nums[i] <= 10^9 → Values can be negative, positive, or zero. Only zeros matter for our count, so the actual magnitudes are irrelevant.long → A run of 10^5 zeros produces 10^5 (10^5 + 1) / 2 = ~5 10^9 subarrays, which overflows a 32-bit integer. We need a 64-bit integer for the result.The most direct way to solve this: for every index that contains a zero, try extending a subarray to the right as long as the elements are still zero. Each valid extension is another zero-filled subarray to count.
This is what you'd naturally try first. It's simple and correct, just not fast enough for the largest inputs.
count to 0.i from 0 to n-1:nums[i] is not 0, skip it.i, extend right while elements are 0.j you reach (including i itself), increment count by 1.count.The brute force re-scans overlapping ranges for each starting zero. What if instead of recounting from each starting point, we figured out how many new subarrays each zero contributes based on its position within the current run?
Instead of recounting overlapping subarrays, think about what each zero contributes individually.
When you encounter a zero, ask: how many new zero-filled subarrays end at this position?
[0] (itself) and [0,0] (paired with the previous zero).[0], [0,0], and [0,0,0].So the pattern is: the k-th consecutive zero contributes exactly k new subarrays. All we need is a running counter of consecutive zeros.
When we hit a non-zero element, the run breaks and we reset the counter to 0. At each step, we add the current counter value to our total.
The insight is the contribution technique. Instead of enumerating all subarrays and checking if they're all zeros, we flip the question: for each zero, how many zero-filled subarrays end here?
A zero at position i that is the k-th consecutive zero can form subarrays of length 1, 2, ..., k all ending at position i. That's exactly k subarrays. By summing these contributions across all zeros, we get the total count without any double-counting.
This is the same math as the triangular number formula. A run of k zeros contributes 1 + 2 + ... + k = k*(k+1)/2 subarrays. But we don't even need to compute that formula explicitly because the incremental approach handles it naturally.
count to 0 and consecutiveZeros to 0.nums:consecutiveZeros by 1.consecutiveZeros to 0.consecutiveZeros to count.count.count and consecutiveZeros) regardless of input size.