Skip to main content
Back to problems
Leetcode
Medium
Arrays
Number Of Zero Filled Subarrays

Count how many contiguous subarrays consist entirely of zeros.

Acceptance 0%
Problem Statement

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 0 matters 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.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.