Sum of products of all possible Subarrays
Given an array arr[] of N positive integers, the task is to find the sum of the product of elements of all the possible subarrays.
Examples:
Input: arr[] = {1, 2, 3}
Output: 20
Explanation: Possible Subarrays are: {1}, {2}, {3}, {1, 2}, {2, 3}, {1, 2, 3}.
Products of all the above subarrays are 1, 2, 3, 2, 6 and 6 respectively.
Sum of all products = 1 + 2 + 3 + 2 + 6 + 6 = 20.
Input: arr[] = {1, 2, 3, 4}
Output: 84
Explanation:
Possible Subarrays are: {1}, {2}, {3}, {4}, {1, 2}, {2, 3}, {3, 4}, {1, 2, 3}, {2, 3, 4}, {1, 2, 3, 4}. Products of all the above subarrays are 1, 2, 3, 4, 2, 6, 12, 6, 24 and 24.
Sum of all products = 1 + 2 + 3 + 4 + 2 + 6 + 12 + 6 + 24 + 24 = 84.
Naive Approach: The simplest approach to solve the problem is to generate all possible subarrays and calculate the product of all elements of each subarray and add it to the final sum.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to observe the problem into some pattern. Suppose we have four numbers a, b, c, and d. We can write all possible subarrays products as:
a + b + c + d+ ab + bc + cd + abc + bcd + abcd
= (a + ab + abc + abcd) + (b + bc + bcd) + (c + cd) + d
= a * (1+ b + bc + bcd) + (b + bc + bcd) + (c + cd) + d
Now, the value of underlined expression (b + bc + bcd) can be calculated once and use twice.
Again, (b+ bc + bcd) + (c + cd) = b * (1 + c + cd) + (c + cd)
In the same way, the expression (c + cd) can be used twice.
The latter part is the same as above.
Follow the below steps to solve the problem:
- Iterate through the last element and make the reoccurring expression updated with every element and use it further. In this process, update the result accordingly.
- Initialize the ans as 0 that will store the final sum and res as 0 that will keep the track of the value of the product of all elements of the previous subarray.
- Traverse the array from the back and for each element, arr[i] do the following:
- Increment the ans by the product of arr[i] and (1 + res).
- Update res to arr[i]*(1 + res).
- After the above steps, print the sum of the product of all subarrays stored in ans.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
void sumOfSubarrayProd( int arr[], int n)
{
int ans = 0;
int res = 0;
for ( int i = n - 1; i >= 0; i--)
{
int incr = arr[i] * (1 + res);
ans += incr;
res = incr;
}
cout << (ans);
}
int main()
{
int arr[] = { 1, 2, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
sumOfSubarrayProd(arr, N);
}
|
Java
import java.io.*;
class GFG {
static void sumOfSubarrayProd( int arr[],
int n)
{
int ans = 0 ;
int res = 0 ;
for ( int i = n - 1 ; i >= 0 ; i--) {
int incr = arr[i] * ( 1 + res);
ans += incr;
res = incr;
}
System.out.println(ans);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 };
int N = arr.length;
sumOfSubarrayProd(arr, N);
}
}
|
Python3
def sumOfSubarrayProd(arr, n):
ans = 0
res = 0
i = n - 1
while (i > = 0 ):
incr = arr[i] * ( 1 + res)
ans + = incr
res = incr
i - = 1
print (ans)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 ]
N = len (arr)
sumOfSubarrayProd(arr, N)
|
C#
using System;
class solution{
static void sumOfSubarrayProd( int []arr,
int n)
{
int ans = 0;
int res = 0;
for ( int i = n - 1; i >= 0; i--)
{
int incr = arr[i] * (1 + res);
ans += incr;
res = incr;
}
Console.WriteLine(ans);
}
public static void Main(String[] args)
{
int []arr = {1, 2, 3 };
int N = arr.Length;
sumOfSubarrayProd(arr, N);
}
}
|
Javascript
<script>
function sumOfSubarrayProd(arr, n)
{
let ans = 0;
let res = 0;
for (let i = n - 1; i >= 0; i--) {
let incr = arr[i] * (1 + res);
ans += incr;
res = incr;
}
document.write(ans);
}
let arr = [ 1, 2, 3 ];
let N = arr.length;
sumOfSubarrayProd(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Last Updated :
02 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...