Skip to main content
Back to problems
#2348
Medium Algorithms

Number of zero filled subarrays

Array Math
70.1% acceptance
Feb 25, 2026
2784
95
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn zero_filled_subarray(nums: Vec<i32>) -> i64 {
    let mut ans = 0i64;
    let mut run = 0i64;
    for &n in &nums {
      if n == 0 { run += 1; ans += run; } else { run = 0; }
    }
    ans
  }
}