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.

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.
So, there are total of 6 subarrays with only 0's in it.
Input: nums = [2,10,2019]
Output: 0
Explanation: There is no subarray filled with 0. Therefore, we return 0.
Constraints:

Contents

Let's understand this approach with an example:

If you notice a pattern in above examples, when you have a contiguous subarrays with zero's in it, each time you add a zero, we can simply add number zero's have found so far to total result. Let's look at the pattern in above example.

Implementation steps:
public class NumberOfZeroFilledSubArrays { static long zeroFilledSubarray(int[] nums) { long result = 0; long count = 0; for(int i=0; i<nums.length; i++) { if(nums[i] ==0) { count++; } else { count =0; } result = result + count; } return result; } public static void main(String[] args) { System.out.println(zeroFilledSubarray(new int[]{1,3,0,0,2,0,0,4})); } }
Complexity Analysis:

Time complexity: Above code runs in O(n) time where n is the length of nums array.
Space complexity: O(1)

Above implementations source code can be found at GitHub link for Java code