Input: nums =[1,3,0,0,2,0,0,4]Output: 6Explanation:
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 with0. Therefore, we return6.
Example 2:
1
2
3
4
5
6
7
Input: nums =[0,0,0,2,0,0]Output: 9Explanation:
There are 5 occurrences of [0] as a subarray.There are 3 occurrences of [0,0] as a subarray.There is1 occurrence of [0,0,0] as a subarray.There is no occurrence of a subarray with a size more than 3 filled with0. Therefore, we return9.
Example 3:
1
2
3
Input: nums =[2,10,2019]Output: 0Explanation: There is no subarray filled with0. Therefore, we return0.
Traverse through the array to count contiguous sequences of zeros.
For each sequence of zeros of length n, the number of subarrays is given by the sum of the first n natural numbers: ( \text{count} = \frac{n \times (n + 1)}{2} ).