Product of all Subarrays of an Array
Given an array of integers arr of size N, the task is to print products of all subarrays of the array.
Examples:
Input: arr[] = {2, 4}
Output: 64
Here, subarrays are [2], [2, 4], [4]
Products are 2, 8, 4
Product of all Subarrays = 64
Input : arr[] = {10, 3, 7}
Output : 27783000
Here, subarrays are [10], [10, 3], [10, 3, 7], [3], [3, 7], [7]
Products are 10, 30, 210, 3, 21, 7
Product of all Subarrays = 27783000
Naive Approach: A simple solution is to generate all sub-array and compute their product.
C++
#include <bits/stdc++.h>
using namespace std;
void product_subarrays( int arr[], int n)
{
int product = 1;
for ( int i = 0; i < n; i++) {
for ( int j = i; j < n; j++) {
for ( int k = i; k <= j; k++)
product *= arr[k];
}
}
cout << product << "\n" ;
}
int main()
{
int arr[] = { 10, 3, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
product_subarrays(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void product_subarrays( int arr[], int n)
{
int product = 1 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = i; j < n; j++) {
for ( int k = i; k <= j; k++)
product *= arr[k];
}
}
System.out.print(product + "\n" );
}
public static void main(String args[])
{
int arr[] = { 10 , 3 , 7 };
int n = arr.length;
product_subarrays(arr, n);
}
}
|
Python3
def product_subarrays(arr, n):
product = 1 ;
for i in range ( 0 , n):
for j in range (i, n):
for k in range (i, j + 1 ):
product * = arr[k];
print (product, "\n" );
arr = [ 10 , 3 , 7 ];
n = len (arr);
product_subarrays(arr, n);
|
C#
using System;
class GFG {
static void product_subarrays( int [] arr, int n)
{
int product = 1;
for ( int i = 0; i < n; i++) {
for ( int j = i; j < n; j++) {
for ( int k = i; k <= j; k++)
product *= arr[k];
}
}
Console.Write(product + "\n" );
}
public static void Main(String[] args)
{
int [] arr = { 10, 3, 7 };
int n = arr.Length;
product_subarrays(arr, n);
}
}
|
Javascript
<script>
function product_subarrays(arr, n)
{
let product = 1;
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
for (let k = i; k <= j; k++)
product *= arr[k];
}
}
document.write(product + "</br>" );
}
let arr = [ 10, 3, 7 ];
let n = arr.length;
product_subarrays(arr, n);
</script>
|
Time Complexity: O(n3)
Auxiliary Space: O(1)
Efficient Approach: An efficient approach is to use two loops and calculate the products while traversing the subarrays.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void product_subarrays( long long int arr[], int n)
{
long long int res = 1;
for ( int i = 0; i < n; i++) {
long long int product = 1;
for ( int j = i; j < n; j++) {
product = product * arr[j];
res *= product;
}
}
cout << res << "\n" ;
}
int main()
{
long long int arr[] = { 10, 3, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
product_subarrays(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void product_subarrays( int arr[], int n)
{
int res = 1 ;
for ( int i = 0 ; i < n; i++) {
int product = 1 ;
for ( int j = i; j < n; j++) {
product = product * arr[j];
res *= product;
}
}
System.out.println(res + "\n" );
}
public static void main(String args[])
{
int arr[] = { 10 , 3 , 7 };
int n = arr.length;
product_subarrays(arr, n);
}
}
|
Python3
def product_subarrays(arr, n):
res = 1 ;
for i in range (n):
product = 1
for j in range (i, n):
product * = arr[j];
res = res * product
print (res);
if __name__ = = '__main__' :
arr = [ 10 , 3 , 7 ];
n = len (arr);
product_subarrays(arr, n);
|
C#
using System;
class GFG {
static void product_subarrays( int [] arr, int n)
{
int res = 1;
for ( int i = 0; i < n; i++) {
int product = 1;
for ( int j = i; j < n; j++) {
product *= arr[j];
res = res * product;
}
}
Console.WriteLine(res + "\n" );
}
public static void Main(String[] args)
{
int [] arr = { 10, 3, 7 };
int n = arr.Length;
product_subarrays(arr, n);
}
}
|
Javascript
<script>
function product_subarrays(arr, n)
{
var res = 1;
for ( var i = 0; i < n; i++) {
var product = 1;
for ( var j = i; j < n; j++) {
product = product * arr[j];
res *= product;
}
}
document.write( res );
}
var arr = [10, 3, 7];
var n = arr.length;
product_subarrays(arr, n);
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(1)
Last Updated :
24 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...