Count how many contiguous subarrays consist entirely of zeros.
Given an integer array nums, count the number of contiguous subarrays whose elements are all 0.
A subarray is a contiguous non-empty slice of the array. The same zero-filled segment contributes many subarrays: for example, a run of length k contributes k * (k + 1) / 2 zero-filled subarrays.
Input Format
- An integer array
nums. - Each element is an integer; only the value
0matters for counting zero-filled subarrays.
Output Format
- Return a single integer: the number of contiguous subarrays made entirely of zeros.
Constraints
1 <= nums.length.nums[i]may be any integer.- The answer may exceed 32-bit integer range, so use a 64-bit integer type if needed.
Example 1
Input
nums = [1,0,0,2,0,0,0]
Output
9
Explanation
The zero runs have lengths 2 and 3. They contribute 23/2 = 3 and 34/2 = 6 subarrays, for a total of 9.
Example 2
Input
nums = [0,0,0]
Output
6
Explanation
All subarrays are zero-filled: [0], [0], [0], [0,0], [0,0], [0,0,0].
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.