Get the best out of our app
GFG App
Open App
Browser
Continue

Number of subarrays for which product and sum are equal

Given a array of n numbers. We need to count the number of subarrays having the product and sum of elements are equal

Examples:

```Input  : arr[] = {1, 3, 2}
Output : 4
The subarrays are :
[0, 0] sum = 1, product = 1,
[1, 1] sum = 3, product = 3,
[2, 2] sum = 2, product = 2 and
[0, 2] sum = 1+3+2=6, product = 1*3*2 = 6

Input : arr[] = {4, 1, 2, 1}
Output : 5```
Recommended Practice

The idea is simple, we check for each subarray that if product and sum of its elements are equal or not. If it is then increase the counter variable by 1

Implementation:

C++

 `// C++ program to count subarrays with` `// same sum and product.` `#include` `using` `namespace` `std;`   `// returns required number of subarrays` `int` `numOfsubarrays(``int` `arr[] , ``int` `n)` `{` `    ``int` `count = 0; ``// Initialize result`   `    ``// checking each subarray` `    ``for` `(``int` `i=0; i

Java

 `// Java program to count subarrays with` `// same sum and product.`   `class` `GFG` `{` `    ``// returns required number of subarrays` `    ``static` `int` `numOfsubarrays(``int` `arr[] , ``int` `n)` `    ``{` `        ``int` `count = ``0``; ``// Initialize result` `     `  `        ``// checking each subarray` `        ``for` `(``int` `i=``0``; i

Python3

 `# python program to` `# count subarrays with` `# same sum and product.`   `# returns required` `# number of subarrays` `def` `numOfsubarrays(arr,n):`   `    ``count ``=` `0` `# Initialize result` ` `  `    ``# checking each subarray` `    ``for` `i ``in` `range``(n):` `    `  `        ``product ``=` `arr[i]` `        ``sum` `=` `arr[i]` `        ``for` `j ``in` `range``(i``+``1``,n):` `        `  `            ``# checking if product is equal` `            ``# to sum or not` `            ``if` `(product``=``=``sum``):` `                ``count``+``=``1` ` `  `            ``product ``*``=` `arr[j]` `            ``sum` `+``=` `arr[j]` `        `  ` `  `        ``if` `(product``=``=``sum``):` `            ``count``+``=``1` `    `  `    ``return` `count`   `# Driver code`   `arr ``=` `[``1``,``3``,``2``]` `n ``=``len``(arr)` `print``(numOfsubarrays(arr , n))`   `# This code is contributed` `# by Anant Agarwal.`

C#

 `// C# program to count subarrays ` `// with same sum and product.` `using` `System;` `class` `GFG {` `    `  `    ``// returns required number` `    ``// of subarrays` `    ``static` `int` `numOfsubarrays(``int` `[]arr , ` `                              ``int` `n)` `    ``{` `        `  `        ``// Initialize result` `        ``int` `count = 0; ` `    `  `        ``// checking each subarray` `        ``for` `(``int` `i = 0; i < n; i++)` `        ``{` `            ``int` `product = arr[i];` `            ``int` `sum = arr[i];` `            ``for` `(``int` `j = i + 1; j < n; j++)` `            ``{` `                `  `                ``// checking if product is ` `                ``// equal to sum or not` `                ``if` `(product == sum)` `                    ``count++;` `    `  `                ``product *= arr[j];` `                ``sum += arr[j];` `            ``}` `    `  `            ``if` `(product == sum)` `                ``count++;` `        ``}` `        ``return` `count;` `    ``}` `    `  `    ``// Driver Code` `    ``public` `static` `void` `Main()` `    ``{` `        ``int` `[]arr = {1,3,2};` `        ``int` `n = arr.Length;` `        ``Console.Write(numOfsubarrays(arr , n));` `    ``}` `}`   `// This code is contributed by Nitin Mittal.`

PHP

 ``

Javascript

 ``

Output

`4`

Time Complexity : O(n2)

This article is contributed by Ayush Jha. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up
Similar Reads
Related Tutorials