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.
nums.0 matters for counting zero-filled subarrays.1 <= nums.length.nums[i] may be any integer.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
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.