Number of zero filled subarrays

November 12, 2022

math-and-geometry

Problem URL: Number of zero filled subarrays

We will iterate over each number, if the number is zero, we will add the number of subarrays that can be formed with the previous numbers to the result. We will also add the number of subarrays that can be formed with the previous numbers to the result.

class Solution:
    def zeroFilledSubarray(self, nums: List[int]) -> int:
        res, count = 0, 0
        for num in nums:
            if num != 0:
                count = 0
            else:
                count += 1
            res += count
        return res

Time Complexity: O(n)
Space Complexity: O(1)