Number of subarrays for which product and sum are equal
Last Updated :
11 Jul, 2022
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
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<bits/stdc++.h>
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<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 function
int main()
{
int arr[] = {1,3,2};
int n = sizeof(arr)/sizeof(arr[0]);
cout << numOfsubarrays(arr , n);
return 0;
}
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<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 function
public static void main(String args[])
{
int arr[] = {1,3,2};
int n = arr.length;
System.out.println(numOfsubarrays(arr , n));
}
}
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
<?php
// PHP program to count subarrays
// with same sum and product.
// function returns required
// number of subarrays
function numOfsubarrays($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.
?>
JavaScript
<script>
// Javascript program to count subarrays
// with same sum and product.
// Returns required number
// of subarrays
function numOfsubarrays(arr, n)
{
// Initialize result
let count = 0;
// Checking each subarray
for(let i = 0; i < n; i++)
{
let product = arr[i];
let sum = arr[i];
for(let 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
let arr = [ 1, 3, 2 ];
let n = arr.length;
document.write(numOfsubarrays(arr, n));
// This code is contributed by decode2207
</script>
Time Complexity : O(n2)
Similar Reads
Find the longest Subarray with equal Sum and Product Given an array arr[] of N integers, the task is to find the length of the longest subarray where the sum of the elements in the subarray is equal to the product of the elements in the subarray. Examples: Input: arr[] = [1, 2, 1, 2, 2, 5, 6, 24]Output: 5Explanation: The subarray [1, 2, 1, 2, 2] has a
11 min read
Number of subarrays with given product Given an array of positive numbers and a number k, find the number of subarrays having product exactly equal to k. We may assume that there is no overflow. Examples : Input : arr = [2, 1, 1, 1, 4, 5] k = 4 Output : 4 1st subarray : arr[1..4] 2nd subarray : arr[2..4] 3rd subarray : arr[3..4] 4th suba
15 min read
Number of subarrays having product less than K Given an array of positive numbers, calculate the number of possible contiguous subarrays having product lesser than a given number K. Examples : Input : arr[] = [1, 2, 3, 4] K = 10Output : 7The subarrays are {1}, {2}, {3}, {4}, {1, 2}, {1, 2, 3} and {2, 3} Input : arr[] = [1, 9, 2, 8, 6, 4, 3] K =
13 min read
Find number of subarrays with even sum Given an array, find the number of subarrays whose sum is even. Example : Input : arr[] = {1, 2, 2, 3, 4, 1} Output : 9 There are possible subarrays with even sum. The subarrays are 1) {1, 2, 2, 3} Sum = 8 2) {1, 2, 2, 3, 4} Sum = 12 3) {2} Sum = 2 (At index 1) 4) {2, 2} Sum = 4 5) {2, 2, 3, 4, 1} S
15+ min read
Count subarrays with equal number of 1's and 0's Given an array arr[] of size n containing 0 and 1 only. The problem is to count the subarrays having an equal number of 0's and 1's. Examples: Input: arr[] = {1, 0, 0, 1, 0, 1, 1}Output: 8Explanation: The index range for the 8 sub-arrays are: (0, 1), (2, 3), (0, 3), (3, 4), (4, 5)(2, 5), (0, 5), (1,
14 min read