You are given an array arr  of length N of 0's and 1's. Find the total number of subarrays of 0's
Example 1:
Input:
N = 4
arr[] = {0, 0, 1, 0}
Output:
4
Explanation:
Following are the subarrays of
length 1: {0}, {0}, {0} - 3
length 2: {0, 0} - 1
Total Subarrays: 3 + 1 = 4
Example 2:
Input:
N = 4
arr[] = {0, 0, 0, 0}
Output:
10
Explanation:
Following are the subarrays of
length 1: {0}, {0}, {0}, {0} - 4
length 2: {0, 0}, {0, 0}, {0, 0} - 3
length 3: {0, 0, 0}, {0, 0, 0} - 2
length 4: {0, 0, 0, 0} - 1
Total Subarrays: 4 + 3 + 2 + 1 = 10
Your Task:
Your task is to complete the function no_of_subarrays(), which takes an integer N and an integer array arr as the input parameters and returns an integer denoting the total number of subarrays of 0's.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
	1 <= N <= 10^{6}
	0 <= arr[i] <= 1

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Mathematics

Starter code: #User function Template for python3



class Solution():

    def no_of_subarrays(self, n,arr):

        #your code goes here

Solutions:
class Solution:

	def no_of_subarrays(self, n, arr):
		count = 0
		ans = 0
		for i in arr:
			if i is 0:
				count += 1
			else:
				ans += count * (count + 1) // 2
				count = 0
		if count > 0:
			ans += count * (count + 1) // 2
		return ans


from itertools import groupby

class Solution:

	def no_of_subarrays(self, n, arr):
		count = 0
		for (k, g) in groupby(arr):
			if k == 0:
				l = len(list(g))
				count += l * (l + 1) >> 1
		return count


class Solution:

	def no_of_subarrays(self, n, arr):
		count = 0
		count_of_zero = 0
		for num in arr:
			if num == 0:
				count_of_zero += 1
			else:
				count = count + count_of_zero * (count_of_zero + 1) // 2
				count_of_zero = 0
		count = count + count_of_zero * (count_of_zero + 1) // 2
		return count


class Solution:

	def no_of_subarrays(self, n, arr):
		count = 0
		ans = 0
		for v in arr:
			if v == 0:
				count += 1
				ans += count
			else:
				count = 0
		return ans


class Solution:

	def no_of_subarrays(self, n, arr):
		c = 0
		zero = 0
		for i in arr:
			if i == 0:
				c += 1
			else:
				zero += c * (c + 1) // 2
				c = 0
		if c != 0:
			zero += c * (c + 1) // 2
		return zero


class Solution:

	def no_of_subarrays(self, n, arr):
		count = 0
		current = 0
		for index in range(n):
			if not arr[index]:
				current += 1
			else:
				count += current * (current + 1) // 2
				current = 0
		count += current * (current + 1) // 2
		return count


class Solution:

	def no_of_subarrays(self, n, arr):
		count = 0
		z = 0
		for i in range(0, n):
			if arr[i] == 0:
				count += 1
				z += count
			else:
				count = 0
		return z


import math

class Solution:

	def no_of_subarrays(self, n, arr):
		count1 = 0
		count2 = 0
		for i in range(n):
			if arr[i] == 0:
				count1 += 1
			elif arr[i] == 1:
				z1 = count1 * (count1 + 1) // 2
				count2 += z1
				count1 = 0
		if count1 > 0:
			z1 = count1 * (count1 + 1) // 2
			count2 += z1
			count1 = 0
		return count2


class Solution:

	def no_of_subarrays(self, n, arr):
		from collections import defaultdict
		l = defaultdict(int)
		cnt = 0
		for i in arr:
			if i == 0:
				cnt += 1
			else:
				l[cnt] += 1
				cnt = 0
		l[cnt] += 1
		ret = 0
		for i in l:
			ret += i * (i + 1) * l[i] // 2
		return ret


class Solution:

	def no_of_subarrays(self, n, arr):
		res = 0
		count = 0
		for i in range(len(arr)):
			if arr[i] == 0:
				count += 1
			else:
				a = count * (count + 1) // 2
				res += a
				count = 0
		a = count * (count + 1) // 2
		res += a
		return res


class Solution:

	def no_of_subarrays(self, n, arr):
		element = 0
		subarray = 0
		for i in arr:
			if i == 0:
				element += 1
				subarray += element
			else:
				element = 0
		return subarray


class Solution:

	def no_of_subarrays(self, n, arr):
		dp = [0] * n
		dp[0] = 1 if arr[0] == 0 else 0
		c = dp[0]
		for i in range(1, n):
			if arr[i] == 0:
				dp[i] = dp[i - 1] + 1
			c += dp[i]
		return c


class Solution:

	def no_of_subarrays(self, n, arr):
		total = 0
		sub = 0
		for i in arr:
			if i == 0:
				total += 1
				sub += 1
			else:
				total += sub * (sub - 1) // 2
				sub = 0
		total += sub * (sub - 1) // 2
		return total


class Solution:

	def no_of_subarrays(self, n, arr):
		ans = 0
		cnt = 0
		for i in range(n):
			if arr[i] == 0:
				cnt += 1
				ans += cnt
			else:
				cnt = 0
		return ans


class Solution:

	def calc_partial_subarrays(self, n):
		return n * (1 + n) // 2

	def no_of_subarrays(self, n, arr):
		subarrays = 0
		zeroes = 0
		for elem in arr:
			if elem == 0:
				zeroes += 1
			else:
				subarrays += self.calc_partial_subarrays(zeroes)
				zeroes = 0
		return subarrays + self.calc_partial_subarrays(zeroes)


class Solution:

	def no_of_subarrays(self, n, arr):
		local = 0
		for i in range(n - 1, -1, -1):
			if arr[i] == 0:
				local += 1
				arr[i] = local
			else:
				local = 0
				arr[i] = local
		return sum(arr[0:n])


class Solution:

	def no_of_subarrays(self, n, arr):
		if 0 not in arr:
			return 0
		count1 = 0
		z = 0
		for i in arr:
			if i == 1:
				count1 += (z + 1) * z // 2
				z = 0
			else:
				z += 1
		count1 += (z + 1) * z // 2
		return count1


class Solution:

	def no_of_subarrays(self, n, arr):
		c = 0
		total = 0
		t = 0
		for i in range(n):
			if arr[i] == 0:
				c += 1
			if arr[i] == 1:
				t = c * (c + 1) // 2
				total += t
				c = 0
		t = c * (c + 1) // 2
		total += t
		return total


class Solution:

	def no_of_subarrays(self, n, nums):
		s = ''
		for i in range(len(nums)):
			if nums[i] != 0:
				s += '-'
			else:
				s += '0'
		s1 = s.split('-')
		z = []
		for i in s1:
			if len(i):
				z.append(len(i))
		sum1 = 0
		for n in z:
			sum1 += n * (n + 1) // 2
		return sum1


class Solution:

	def no_of_subarrays(self, n, arr):
		c = 0
		k = 0
		for i in arr:
			if i == 0:
				c += 1
				k += c
			else:
				c = 0
		return k


class Solution:

	def no_of_subarrays(self, n, arr):
		count = 0
		answer = 0
		for i in range(n):
			if arr[i] == 0:
				count += 1
			else:
				answer += int(count * (count + 1) / 2)
				count = 0
		answer += int(count * (count + 1) / 2)
		return answer


class Solution:

	def no_of_subarrays(self, n, arr):
		l = []
		totalcount = 0
		count = 0
		for x in range(n):
			if arr[x] == 0:
				count += 1
				if x == n - 1:
					l.append(count)
			else:
				l.append(count)
				count = 0
		for x in l:
			totalcount += x * (x + 1) // 2
		return totalcount


class Solution:

	def no_of_subarrays(self, n, arr):
		start = end = 0
		subs = 0
		while end < n:
			if arr[end] != 1:
				subs += end - start + 1
			else:
				start = end + 1
			end += 1
		return subs


class Solution:

	def no_of_subarrays(self, N, arr):
		(ans, i) = (0, 0)
		while i < N:
			if arr[i] == 1:
				i += 1
				continue
			j = i + 1
			while j < N and arr[j] == 0:
				j += 1
			l = j - i
			ans += l * (l + 1) // 2
			i = j
		return ans


class Solution:

	def no_of_subarrays(self, n, arr):
		comb = 0
		ans = 0
		for element in arr:
			if element == 0:
				comb += 1
				ans += comb
			else:
				comb = 0
		return ans


class Solution:

	def no_of_subarrays(self, n, arr):

		def Sum(num):
			return int(num * (num + 1) / 2)
		count = 0
		sum = 0
		for i in range(len(arr)):
			if arr[i] == 0:
				count += 1
			else:
				sum += Sum(count)
				count = 0
		if count > 0:
			sum += Sum(count)
		return sum


class Solution:

	def no_of_subarrays(self, n, arr):
		a = []
		c = 0
		for i in arr:
			if i == 0:
				c += 1
			elif c > 0:
				a.append(c)
				c = 0
		if c > 0:
			a.append(c)
		s = 0
		for x in a:
			s += x * (x + 1) // 2
		return s


class Solution:

	def no_of_subarrays(self, n, arr):
		(a, d) = (0, 0)
		for i in arr:
			if i == 0:
				a += 1
				d += a
			else:
				a = 0
		return d


class Solution:

	def no_of_subarrays(self, n, arr):
		arr.append(1)
		(s, c) = (0, 0)
		for i in range(n + 1):
			if arr[i] == 0:
				c += 1
			else:
				s = s + c * (c + 1) / 2
				c = 0
		return int(s)


class Solution:

	def no_of_subarrays(self, n, arr):
		ans = 0
		c = 0
		arr.append(1)
		for i in range(n):
			if arr[i] == 0:
				c += 1
				if arr[i + 1] != 0:
					ans += c * (c + 1) // 2
			if arr[i] != 0:
				c = 0
		return ans


class Solution:

	def no_of_subarrays(self, n, arr):
		sc = 0
		res = 0
		for i in range(n):
			if arr[i] == 1:
				res += int(sc * (sc + 1) // 2)
				sc = 0
			else:
				sc += 1.0
		res += int(sc * (sc + 1) // 2)
		return res


class Solution:

	def no_of_subarrays(self, n, nums):
		if 0 not in nums:
			return 0
		else:
			c = 0
			n = 0
			i = 0
			while i < len(nums):
				if nums[i] == 0:
					n = 0
					while i < len(nums) and nums[i] == 0:
						n += 1
						i += 1
					c = c + n * (n + 1) // 2
				else:
					i += 1
			return c


class Solution:

	def no_of_subarrays(self, n, arr):
		l = []
		c = 0
		for i in arr:
			if i == 0:
				c += 1
			else:
				l.append(c)
				c = 0
		if c > 0:
			l.append(c)
		a = 0
		for j in l:
			a += j * (j + 1) // 2
		return a


class Solution:

	def no_of_subarrays(self, n, arr):
		a = 0
		result = 0
		for i in range(n):
			if arr[i] == 0:
				a = a + 1
				result = result + a
			elif arr[i] == 1:
				a = 0
		return result


class Solution:

	def no_of_subarrays(self, n, arr):
		if arr.count(1) == 0:
			return n * (n + 1) // 2
		s = 0
		i = 0
		j = 0
		while j < n:
			if arr[j] == 1:
				l = (j - i) * (j - i + 1) // 2
				s += l
				i = j + 1
			j += 1
		s += (j - i) * (j - i + 1) // 2
		return s


class Solution:

	def no_of_subarrays(self, n, arr):
		if n == 1:
			if arr[n - 1] == 0:
				return 1
			else:
				return 0
		dp = [0 for i in range(n + 1)]
		dp[1] = 1
		for i in range(2, n + 1):
			dp[i] = dp[i - 1] + i
		result = 0
		cnt = 0
		for i in range(n):
			if arr[i] == 0:
				cnt += 1
			else:
				result += dp[cnt]
				cnt = 0
		if arr[n - 1] == 0:
			result += dp[cnt]
		return result


class Solution:

	def no_of_subarrays(self, n, arr):
		res = c = 0
		for e in arr:
			if e == 1:
				res += c * (c + 1) // 2
				c = 0
			else:
				c += 1
		res += c * (c + 1) // 2
		return res


class Solution:

	def no_of_subarrays(self, n, arr):
		ans = 0
		z = 0
		for i in range(n):
			if arr[i] == 0:
				z = z + 1
				ans = ans + z
			if arr[i] == 1:
				z = 0
		return ans


class Solution:

	def no_of_subarrays(self, n, arr):
		count = 0
		s = -1
		e = -1
		for i in range(n):
			if s == -1 and arr[i] == 0:
				s = i
			if s != -1 and arr[i] == 1:
				sl = i - s
				count += sl * (sl + 1) // 2
				s = -1
		if arr[n - 1] == 0:
			sl = n - s
			count += sl * (sl + 1) // 2
		return count


import array

class Solution:

	def no_of_subarrays(self, n, arr):
		sum = 0
		count = 0
		for i in range(n):
			if arr[i] == 0:
				count += 1
			if arr[i] == 1:
				sum = sum + count * (count + 1) // 2
				count = 0
			elif i == n - 1:
				sum = sum + count * (count + 1) // 2
		return sum


class Solution:

	def no_of_subarrays(self, n, arr):
		count = 0
		(i, j) = (0, 0)
		while j < len(arr):
			if arr[j] == 1:
				count += (j - i) * (j - i + 1) // 2
				i = j + 1
			j += 1
		if arr[-1] == 0:
			count += (j - i) * (j - i + 1) // 2
		return count


class Solution:

	def no_of_subarrays(self, n, arr):
		ans = 0
		i = 0
		j = 1
		while i < n:
			if arr[i] == 0:
				ans += j
				j = j + 1
			else:
				j = 1
			i = i + 1
		return ans


class Solution:

	def no_of_subarrays(self, n, arr):
		total = 0
		count = 0
		for i in range(n):
			if arr[i] == 0:
				count += 1
			else:
				total += count * (count + 1) // 2
				count = 0
		if i == n - 1 and count != 0:
			total += count * (count + 1) // 2
		return total


class Solution:

	def no_of_subarrays(self, n, arr):
		total = 0
		count = 0
		arr.append(1)
		for j in arr:
			if j == 0:
				count += 1
			else:
				total += count * (count + 1) // 2
				count = 0
		return total


class Solution:

	def no_of_subarrays(self, n, arr):
		ans = 0
		i = 0
		while i < n:
			zeroCount = 0
			while i < n and arr[i] == 0:
				zeroCount += 1
				i += 1
			ans += zeroCount * (zeroCount + 1) // 2
			i += 1
		return ans


class Solution:

	def no_of_subarrays(self, n, arr):
		b = 0
		a = 0
		for i in arr:
			if i == 1:
				a = 0
				continue
			a = a + 1
			b = b + a
		return b


class Solution:

	def no_of_subarrays(self, N, arr):
		(count_0, count_subArr) = (0, 0)
		for i in range(N):
			if arr[i] == 0:
				count_0 += 1
			else:
				count_subArr += count_0 * (count_0 + 1) // 2
				count_0 = 0
		count_subArr += count_0 * (count_0 + 1) // 2
		return count_subArr


class Solution:

	def no_of_subarrays(self, n, arr):
		l = 0
		c = 0
		ans = 0
		for i in range(n):
			if arr[i] == 0:
				ans = ans + (i - l + 1)
			else:
				l = i + 1
				continue
		return ans