Product of all Subarrays of an Array | Set 2
Last Updated :
05 May, 2021
Given an array arr[] of integers of size N, the task is to find the products of all subarrays of the array.
Examples:
Input: arr[] = {2, 4}
Output: 64
Explanation:
Here, subarrays are {2}, {2, 4}, and {4}.
Products of each subarray are 2, 8, 4.
Product of all Subarrays = 64
Input: arr[] = {1, 2, 3}
Output: 432
Explanation:
Here, subarrays are {1}, {1, 2}, {1, 2, 3}, {2}, {2, 3}, {3}.
Products of each subarray are 1, 2, 6, 2, 6, 3.
Product of all Subarrays = 432
Naive and Iterative approach: Please refer this post for these approaches.
Approach: The idea is to count the number of each element occurs in all the subarrays. To count we have below observations:
- In every subarray beginning with arr[i], there are (N – i) such subsets starting with the element arr[i].
For Example:
For array arr[] = {1, 2, 3}
N = 3 and for element 2 i.e., index = 1
There are (N – index) = 3 – 1 = 2 subsets
{2} and {2, 3}
-
- For any element arr[i], there are (N – i)*i subarrays where arr[i] is not the first element.
For array arr[] = {1, 2, 3}
N = 3 and for element 2 i.e., index = 1
There are (N – index)*index = (3 – 1)*1 = 2 subsets where 2 is not the first element.
{1, 2} and {1, 2, 3}
Therefore, from the above observations, the total number of each element arr[i] occurs in all the subarrays at every index i is given by:
total_elements = (N - i) + (N - i)*i
total_elements = (N - i)*(i + 1)
The idea is to multiply each element (N – i)*(i + 1) number of times to get the product of elements in all subarrays.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long int SubArrayProdct( int arr[],
int n)
{
long int result = 1;
for ( int i = 0; i < n; i++)
result *= pow (arr[i],
(i + 1) * (n - i));
return result;
}
int main()
{
int arr[] = { 2, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << SubArrayProdct(arr, N)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int SubArrayProdct( int arr[], int n)
{
int result = 1 ;
for ( int i = 0 ; i < n; i++)
result *= Math.pow(arr[i], (i + 1 ) *
(n - i));
return result;
}
public static void main(String[] args)
{
int arr[] = new int []{ 2 , 4 };
int N = arr.length;
System.out.println(SubArrayProdct(arr, N));
}
}
|
Python3
def SubArrayProdct(arr, n):
result = 1 ;
for i in range ( 0 , n):
result * = pow (arr[i],
(i + 1 ) * (n - i));
return result;
arr = [ 2 , 4 ];
N = len (arr);
print (SubArrayProdct(arr, N))
|
C#
using System;
class GFG{
static int SubArrayProdct( int []arr, int n)
{
int result = 1;
for ( int i = 0; i < n; i++)
result *= ( int )(Math.Pow(arr[i], (i + 1) *
(n - i)));
return result;
}
public static void Main()
{
int []arr = new int []{2, 4};
int N = arr.Length;
Console.Write(SubArrayProdct(arr, N));
}
}
|
Javascript
<script>
function SubArrayProdct(arr, n)
{
let result = 1;
for (let i = 0; i < n; i++)
result *= Math.pow(arr[i], (i + 1) *
(n - i));
return result;
}
let arr = [2, 4];
let N = arr.length;
document.write(SubArrayProdct(arr, N));
</script>
|
Time Complexity: O(N), where N is the number of elements.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...