The Number of Zero-Filled Subarrays problem asks you to count all subarrays that consist entirely of zeros. This coding problem applies the same run-length triangular number technique as "Number of Substrings With Only 1s" — for a run of L consecutive zeros, there are L*(L+1)/2 all-zero subarrays. The array and math interview pattern is demonstrated with the incremental run-count approach.
Microsoft, Meta, Amazon, and Google ask this as a warm-up problem that tests whether candidates recognize the triangular number pattern for runs of identical elements. The incremental count approach (adding run length to total at each step) is cleaner than computing formula per run.
Incremental run tracking. For each index i: if arr[i] == 0, run = prev_run + 1. Else, run = 0. Add run to total count. Return total. This correctly counts 1 + 2 + ... + L = L*(L+1)/2 for each run of L zeros, accumulated incrementally.
arr=[0,0,1,0,0,0]. Process:
The incremental contribution pattern — "add current run length to total at each step" — automatically computes the triangular number sum for each run. This O(n) single-pass approach is cleaner than finding all runs and computing formulas. It extends to any "count subarrays of all identical elements" problem. Memorize this pattern: it's a common building block for array subarray counting.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum of Absolute Value Expression | Medium | Solve | |
| Minimum Moves to Equal Array Elements | Medium | Solve | |
| Global and Local Inversions | Medium | Solve | |
| Beautiful Arrangement II | Medium | Solve | |
| Count Alternating Subarrays | Medium | Solve |