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

C++

filter_none

editclose

play_arrow

linkbrightness_4code

// C++ program to count subarrays with

// same sum and product.

#include<bits/stdc++.h>

usingnamespacestd;

// returns required number of subarrays

intnumOfsubarrays(intarr[] , intn)

{

intcount = 0; // Initialize result

// checking each subarray

for(inti=0; i<n; i++)

{

intproduct = arr[i];

intsum = arr[i];

for(intj=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++;

}

returncount;

}

// driver function

intmain()

{

intarr[] = {1,3,2};

intn = sizeof(arr)/sizeof(arr[0]);

cout << numOfsubarrays(arr , n);

return0;

}

chevron_right

filter_none

Java

filter_none

editclose

play_arrow

linkbrightness_4code

// Java program to count subarrays with

// same sum and product.

classGFG

{

// returns required number of subarrays

staticintnumOfsubarrays(intarr[] , intn)

{

intcount = 0; // Initialize result

// checking each subarray

for(inti=0; i<n; i++)

{

intproduct = arr[i];

intsum = arr[i];

for(intj=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++;

}

returncount;

}

// Driver function

publicstaticvoidmain(String args[])

{

intarr[] = {1,3,2};

intn = arr.length;

System.out.println(numOfsubarrays(arr , n));

}

}

chevron_right

filter_none

Python3

filter_none

editclose

play_arrow

linkbrightness_4code

# python program to

# count subarrays with

# same sum and product.

# returns required

# number of subarrays

defnumOfsubarrays(arr,n):

count =0# Initialize result

# checking each subarray

fori inrange(n):

product =arr[i]

sum=arr[i]

forj inrange(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

returncount

# Driver code

arr =[1,3,2]

n =len(arr)

print(numOfsubarrays(arr , n))

# This code is contributed

# by Anant Agarwal.

chevron_right

filter_none

C#

filter_none

editclose

play_arrow

linkbrightness_4code

// C# program to count subarrays

// with same sum and product.

usingSystem;

classGFG {

// returns required number

// of subarrays

staticintnumOfsubarrays(int[]arr ,

intn)

{

// Initialize result

intcount = 0;

// checking each subarray

for(inti = 0; i < n; i++)

{

intproduct = arr[i];

intsum = arr[i];

for(intj = 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++;

}

returncount;

}

// Driver Code

publicstaticvoidMain()

{

int[]arr = {1,3,2};

intn = arr.Length;

Console.Write(numOfsubarrays(arr , n));

}

}

// This code is contributed by Nitin Mittal.

chevron_right

filter_none

PHP

filter_none

editclose

play_arrow

linkbrightness_4code

<?php

// PHP program to count subarrays

// with same sum and product.

// function returns required

// number of subarrays

functionnumOfsubarrays($arr, $n)

{

// Initialize result

$count= 0;

// checking each subarray

for($i= 0; $i< $n; $i++)

{

$product= $arr[$i];

$sum= $arr[$i];

for($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

$arr= array(1, 3, 2);

$n= sizeof($arr);

echo(numOfsubarrays($arr, $n));

// This code is contributed by Ajit.

?>

chevron_right

filter_none

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 contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.