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] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for counting zero-filled subarrays involves examining every possible group of numbers within the given sequence. We simply look at each possible starting point and length for a subarray. If the subarray only contains zeros, we increment our count; otherwise, we move on.
Here's how the algorithm would work step-by-step:
def number_of_zero_filled_subarrays_brute_force(numbers):
number_of_zero_filled_subarrays = 0
for start_index in range(len(numbers)):
for end_index in range(start_index, len(numbers)):
is_zero_filled = True
# Check if the current subarray is all zeros
for index in range(start_index, end_index + 1):
if numbers[index] != 0:
is_zero_filled = False
break
# Increment the counter if the subarray is zero-filled
if is_zero_filled:
number_of_zero_filled_subarrays += 1
return number_of_zero_filled_subarrays
def number_of_zero_filled_subarrays(numbers):
number_of_zero_filled_subarrays = 0
current_zeros = 0
for number in numbers:
if number == 0:
current_zeros += 1
# Accumulate the number of zero filled subarrays
number_of_zero_filled_subarrays += current_zeros
else:
# Reset the counter if a non-zero is encountered
current_zeros = 0
return number_of_zero_filled_subarraysThe key idea is to count consecutive zeros and use a formula to quickly calculate the number of zero-filled subarrays for each such sequence. By iterating through the given numbers and tracking these sequences, we efficiently compute the total number of zero-filled subarrays without redundant calculations.
Here's how the algorithm would work step-by-step:
def number_of_zero_filled_subarrays(numbers):
total_subarrays = 0
consecutive_zeros_count = 0
for number in numbers:
if number == 0:
# Increment count for consecutive zeros.
consecutive_zeros_count += 1
else:
# Calculate subarrays and reset the count.
total_subarrays += (consecutive_zeros_count * (consecutive_zeros_count + 1)) // 2
# Reset count when encountering a non-zero.
consecutive_zeros_count = 0
# Add remaining subarrays after the loop.
total_subarrays += (consecutive_zeros_count * (consecutive_zeros_count + 1)) // 2
return total_subarrays| Case | How to Handle |
|---|---|
| Null or empty input array | Return 0 immediately since there are no subarrays. |
| Array with a single element which is zero | Return 1 as a single zero is a valid zero-filled subarray. |
| Array with a single element which is non-zero | Return 0 as there are no zero-filled subarrays. |
| Array containing only zeros | Calculate the sum of integers from 1 to the length of the array to represent all possible subarrays. |
| Array with alternating zeros and non-zeros | Correctly identify and count each individual zero-filled subarray sequence. |
| Array with a very long sequence of zeros (potential overflow) | Use a 64-bit integer type to store the counts of subarrays to prevent overflow. |
| Array with negative numbers | Negative numbers should not affect the logic, as only zeros are counted for subarrays. |
| Maximum sized input array with all zeros | Ensure the solution scales efficiently, avoiding unnecessary computations for large arrays, and uses 64-bit integers to store the result without overflow. |