Skip to content

2348. Number of Zero-Filled Subarrays #2072

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to count the number of contiguous subarrays filled with zeros in a given integer array. The key insight here is recognizing that each segment of consecutive zeros contributes a certain number of zero-filled subarrays, which can be calculated using a mathematical formula.

Approach

  1. Problem Analysis: The task is to count all contiguous subarrays within the array that consist entirely of zeros. A subarray is defined as a contiguous sequence of elements in the array.
  2. Intuition: For any segment of consecutive zeros of length k, the number of zero-filled subarrays within that segment is given by the formula k * (k + 1) / 2. This is because each single zero forms a subarray of length 1, …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Aug 19, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Aug 19, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants