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.
Example 1:
Input: nums = [1,3,0,0,2,0,0,4] Output: 6 Explanation: There are 4 occurrences of [0] as a subarray. There are 2 occurrences of [0,0] as a subarray. There is no occurrence of a subarray with a size more than 2 filled with 0. Therefore, we return 6.
Example 2:
Input: nums = [0,0,0,2,0,0] Output: 9 Explanation: There are 5 occurrences of [0] as a subarray. There are 3 occurrences of [0,0] as a subarray. There is 1 occurrence of [0,0,0] as a subarray. There is no occurrence of a subarray with a size more than 3 filled with 0. Therefore, we return 9.
Example 3:
Input: nums = [2,10,2019] Output: 0 Explanation: There is no subarray filled with 0. Therefore, we return 0.
Constraints:
1 <= nums.length <= 105-109 <= nums[i] <= 109This approach involves iterating through the array and counting the length of contiguous segments of zeros. For each segment of zeros with length n, the total number of zero-filled subarrays is the sum of the first n natural numbers.
For a segment with length n, the number of zero-filled subarrays is (n * (n + 1)) / 2. This formula is derived from the well-known sum formula for the first n integers.
The C solution iterates over the array, increasing the count for each contiguous zero encountered and adds the current count to the total subarrayCount anytime zeros[i] != 0 is encountered.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), where n is the size of the input array. This is because we only traverse the array once.
Space Complexity: O(1), since we only use a constant number of extra variables.
This approach is slightly variation. We keep track of the number of zero subarrays as we proceed through the array. Whenever a zero is encountered, the count of zero subarrays ending at that position is incremented by the number of zero subarrays ending at the previous position plus one. When a non-zero is encountered, we reset our counter.
A variation of the simple count, where we keep another variable zeroEndsHere which tracks ongoing segments of zeros. This accumulates into count as the loop progresses.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n) - traversing each element once to compute the solution.
Space Complexity: O(1) - using only a few extra integer variables for tracking.
| Approach | Complexity |
|---|---|
| Approach 1: Count zero subarrays using a contiguous segments approach | Time Complexity: O(n), where Space Complexity: O(1), since we only use a constant number of extra variables. |
| Approach 2: Accumulative count of zero subarrays | Time Complexity: O(n) - traversing each element once to compute the solution. Space Complexity: O(1) - using only a few extra integer variables for tracking. |
Number of Zero-Filled Subarrays - Leetcode 2348 - Python • NeetCodeIO • 11,677 views views
Watch 9 more video solutions →Practice Number of Zero-Filled Subarrays with our built-in code editor and test cases.
Practice on FleetCode