Given an array Arr of length N, return the count of number of contiguous subarrays for which the sum and product of elements are equal.
Example 1:
Input:
N = 5
Arr[] = {1, 2, 3, 4, 5}
Output: 6
Explanation: All 6 subarrays having sum of
elements  equals to the product of
elements are: {1}, {2}, {3}, {4}, {5},
{1, 2, 3}
Your Task:
Complete the function numOfsubarrays() which takes an array arr, an integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10
1 <= Arr[i] <= 10 where 1 <= i <= N

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Mathematics

Starter code: #User function Template for python3
class Solution:

	
	def numOfsubarrays(self,arr, n):
    	# code here



Solutions:
class Solution:

	def numOfsubarrays(self, arr, n):
		output = 0
		for i in range(n):
			summa = 0
			product = 1
			for j in range(i, n):
				summa += arr[j]
				product *= arr[j]
				output += summa == product
		return output


import numpy as np

class Solution:

	def numOfsubarrays(self, arr, n):
		count = 0
		for i in range(n - 1):
			for j in range(i + 1, n):
				if np.product(arr[i:j + 1]) == np.sum(arr[i:j + 1]):
					count += 1
		return count + n


class Solution:

	def numOfsubarrays(self, arr, n):
		c = 0
		for i in range(n):
			summ = arr[i]
			product = arr[i]
			for j in range(i + 1, n):
				if summ == product:
					c += 1
				summ += arr[j]
				product *= arr[j]
			if summ == product:
				c += 1
		return c


class Solution:

	def numOfsubarrays(self, arr, n):
		sum = 0
		count = 0
		prod = 1
		for i in range(n):
			sum += arr[i]
			prod *= arr[i]
			for j in range(i + 1, n):
				sum += arr[j]
				prod *= arr[j]
				if sum == prod:
					count += 1
			sum = 0
			prod = 1
		return n + count


class Solution:

	def numOfsubarrays(self, arr, n):
		ansh = 0
		for i in range(n - 1):
			sum = arr[i]
			mul = arr[i]
			for j in range(i + 1, n - 1):
				sum += arr[j]
				mul *= arr[j]
				if sum == mul:
					ansh += 1
		return ansh + len(arr)


class Solution:

	def numOfsubarrays(self, arr, n):
		ans = n
		for i in range(n - 1):
			sumis = arr[i]
			pro = arr[i]
			for j in range(i + 1, n):
				sumis += arr[j]
				pro *= arr[j]
				if sumis == pro:
					ans += 1
		return ans


class Solution:

	def numOfsubarrays(self, arr, n):
		summ = 0
		pr = 1
		k = n
		for i in range(n):
			summ = arr[i]
			pr = arr[i]
			for j in range(i + 1, n):
				summ += arr[j]
				pr *= arr[j]
				if summ == pr:
					k += 1
					break
		return k


import numpy as np

class Solution:

	def numOfsubarrays(self, arr, n):
		count = 0
		prd = 1
		su = 0
		for i in range(n):
			su = arr[i]
			prd = arr[i]
			for j in range(i + 1, n):
				if su == prd:
					count += 1
				prd *= arr[j]
				su += arr[j]
			if su == prd:
				count += 1
		return count


class Solution:

	def numOfsubarrays(self, arr, n):
		c = 0
		for i in range(n):
			p = arr[i]
			s = arr[i]
			c += 1
			for j in range(i + 1, n):
				s += arr[j]
				p = p * arr[j]
				if s == p:
					c += 1
		return c


import numpy as np

class Solution:

	def numOfsubarrays(self, arr, n):
		count = n
		for i in range(n):
			for j in range(i + 1, n):
				a = arr[i:j + 1]
				if sum(a) == np.prod(a):
					count += 1
		return count


from itertools import combinations

class Solution:

	def numOfsubarrays(self, arr, n):
		x = [i for i in range(n + 1)]
		count = 0
		for (i, j) in combinations(x, 2):
			a = arr[i:j]
			m = 1
			for k in a:
				m *= k
			if m == sum(a):
				count += 1
		return count


class Solution:

	def numOfsubarrays(self, arr, n):
		res = 0
		for i in range(n):
			summ = arr[i]
			pro = arr[i]
			if summ == pro:
				res += 1
			j = i + 1
			while j < n:
				summ += arr[j]
				pro *= arr[j]
				if summ == pro:
					res += 1
				j += 1
		return res


class Solution:

	def numOfsubarrays(self, arr, n):
		count = n
		for i in range(n - 1):
			summ = arr[i]
			prod = arr[i]
			for j in range(i + 1, n):
				summ += arr[j]
				prod *= arr[j]
				if summ == prod:
					count += 1
		return count
if __name__ == '__main__':
	tc = int(input())
	while tc > 0:
		n = int(input())
		arr = list(map(int, input().strip().split()))
		ob = Solution()
		ans = ob.numOfsubarrays(arr, n)
		print(ans)
		tc -= 1


class Solution:

	def numOfsubarrays(self, a, n):
		c = 0
		for i in range(n + 1):
			s = 0
			p = 1
			for j in range(i + 1, n + 1):
				s += a[j - 1]
				p *= a[j - 1]
				if s == p:
					c += 1
		return c
if __name__ == '__main__':
	tc = int(input())
	while tc > 0:
		n = int(input())
		arr = list(map(int, input().strip().split()))
		ob = Solution()
		ans = ob.numOfsubarrays(arr, n)
		print(ans)
		tc -= 1


class Solution:

	def numOfsubarrays(self, arr, n):
		sum = 0
		prd = 1
		cnt = 0
		for i in range(n):
			for j in range(i, n):
				sum += arr[j]
				prd *= arr[j]
				if sum == prd:
					cnt += 1
			sum = 0
			prd = 1
		return cnt
if __name__ == '__main__':
	tc = int(input())
	while tc > 0:
		n = int(input())
		arr = list(map(int, input().strip().split()))
		ob = Solution()
		ans = ob.numOfsubarrays(arr, n)
		print(ans)
		tc -= 1


class Solution:

	def numOfsubarrays(self, arr, n):
		cnt = 0
		for i in range(0, n):
			s = arr[i]
			mul = arr[i]
			if s == mul:
				cnt += 1
			for j in range(i + 1, n):
				s = s + arr[j]
				mul = mul * arr[j]
				if s == mul:
					cnt += 1
		return cnt


class Solution:

	def numOfsubarrays(self, arr, n):
		c = n
		for i in range(0, n):
			sum = arr[i]
			pro = arr[i]
			for j in range(i + 1, n):
				sum += arr[j]
				pro *= arr[j]
				if sum == pro:
					c += 1
		return c


class Solution:

	def numOfsubarrays(self, arr, n):
		l = []
		c = 0
		for i in range(len(arr) + 1):
			for j in range(i):
				l.append(arr[j:i])
		for i in l:
			p = 1
			for j in i:
				p = p * j
			if sum(i) == p:
				c += 1
		return c


class Solution:

	def numOfsubarrays(self, arr, n):
		ans = 0
		for i in range(1, n + 1):
			s = sum(arr[:i])
			p = 1
			for k in range(i):
				p *= arr[k]
			if p == s:
				ans += 1
			for j in range(i, n):
				s = s - arr[j - i] + arr[j]
				p = p // arr[j - i] * arr[j]
				if p == s:
					ans += 1
		return ans


from itertools import combinations

class Solution:

	def numOfsubarrays(self, arr, n):
		count = 0
		for i in range(0, n):
			sum = 0
			prod = 1
			for j in range(i, n):
				sum += arr[j]
				prod *= arr[j]
				if sum == prod:
					count += 1
		return count


class Solution:

	def numOfsubarrays(self, arr, n):
		count = n
		start = 0
		end = start + 1
		for i in range(n - 1):
			j = i + 1
			prod = arr[i]
			sums = arr[i]
			while j < n:
				prod = prod * arr[j]
				sums = sums + arr[j]
				if prod == sums:
					count = count + 1
				j = j + 1
		return count


class Solution:

	def numOfsubarrays(self, arr, n):
		count = 0
		for i in range(n):
			product = arr[i]
			sum1 = arr[i]
			for j in range(i + 1, n):
				if product == sum1:
					count += 1
				product *= arr[j]
				sum1 += arr[j]
			if product == sum1:
				count += 1
		return count


import numpy as np

class Solution:

	def numOfsubarrays(self, arr, n):
		c = 0
		for i in range(n + 1):
			for j in range(i):
				p = arr[j:i]
				if sum(p) == np.prod(p):
					c += 1
		return c


class Solution:

	def numOfsubarrays(self, arr, n):
		i = 0
		a = ''
		while i < n:
			j = i
			s = 0
			m = 1
			while j < n:
				s += arr[j]
				m *= arr[j]
				if s == m:
					a += str(i) + ':' + str(j + 1) + ','
				j += 1
			i += 1
		l = a.split(',')
		b = []
		for i in range(len(l) - 1):
			(d, c) = l[i].split(':')
			b.append(arr[int(d):int(c)])
		return len(b)


class Solution:

	def numOfsubarrays(self, a, n):
		(s, p) = (0, 0)
		count = 0
		for i in range(n):
			(s, p) = (a[i], a[i])
			if s == p:
				count += 1
			for j in range(i + 1, n):
				s += a[j]
				p *= a[j]
				if s == p:
					count += 1
		return count


import numpy

class Solution:

	def numOfsubarrays(self, arr, n):
		sub = []
		a = 0
		for i in range(0, n):
			for j in range(i + 1, n):
				sub = arr[i:j]
				if sum(sub) == numpy.prod(sub):
					a += 1
		return a + 1


class Solution:

	def numOfsubarrays(self, arr, n):
		c = n
		for i in range(n):
			a = arr[i]
			m = arr[i]
			for j in range(i + 1, n):
				a += arr[j]
				m *= arr[j]
				if a == m:
					c += 1
		return c


class Solution:

	def numOfsubarrays(self, arr, n):
		p = 0
		q = 0
		for i in range(n):
			su = arr[i]
			product = arr[i]
			if su == product:
				q += 1
			for j in range(i + 1, n):
				su += arr[j]
				product *= arr[j]
				if su == product:
					p += 1
		return p + q


class Solution:

	def numOfsubarrays(self, arr, n):
		c = 0
		for i in range(n):
			cur_sum = 0
			cur_mult = 1
			for j in range(i, n):
				cur_sum += arr[j]
				cur_mult *= arr[j]
				if cur_sum == cur_mult:
					c += 1
		return c


class Solution:

	def numOfsubarrays(self, arr, n):
		res = 0
		for i in range(n):
			su = arr[i]
			mu = arr[i]
			if su == mu:
				res += 1
			j = i + 1
			while j < n:
				su += arr[j]
				mu *= arr[j]
				if su == mu:
					res += 1
				j += 1
		return res


class Solution:

	def numOfsubarrays(self, arr, n):
		c = n
		for i in range(n):
			x = arr[i]
			y = arr[i]
			for j in range(i + 1, n):
				if x + arr[j] == y * arr[j]:
					c += 1
				else:
					x += arr[j]
					y *= arr[j]
		return c


class Solution:

	def numOfsubarrays(self, arr, n):
		count = 0
		for i in range(n):
			s = arr[i]
			p = arr[i]
			for k in range(i + 1, n):
				s += arr[k]
				p *= arr[k]
				if s == p:
					count += 1
		return count + n