DSA problem
Number of Zero-Filled Subarrays
Given an integer array nums, return the number of subarrays filled with 0.
A subarray is a contiguous non-empty sequence of elements within an array.
The key insight is that a single contiguous run of k consecutive zeros contributes k * (k + 1) / 2 zero-filled subarrays, because for a run of length k there are k subarrays ending at the last zero, plus all those counted for the prefix runs. By tracking the length of the current consecutive-zero streak and accumulating it as we scan, we can compute the total in a single pass.
For example, the array [1, 3, 0, 0, 2, 0, 0, 4] has two zero runs of length 2 each, contributing 3 + 3 = 6 zero-filled subarrays.
Examples
nums = [1,3,0,0,2,0,0,4]6nums = [0,0,0,2,0,0]9nums = [2,10,2019]0Constraints
- 1 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.