给定一个由N 个正整数组成的数组arr[] ,任务是找到所有可能子数组元素的乘积之和。
例子:
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.
朴素方法:解决问题的最简单方法是生成所有可能的子数组,并计算每个子数组的所有元素的乘积并将其添加到最终总和中。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:优化上述方法,其思想是将问题观察到某种模式中。假设我们有四个数字a 、 b 、 c和d 。我们可以将所有可能的子数组乘积写为:
a + b + c + 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.
请按照以下步骤解决问题:
- 迭代最后一个元素,并使用每个元素更新重复出现的表达式并进一步使用它。在这个过程中,相应地更新结果。
- 初始化ANS为0将保存最后一笔和资源为0,将保留以前的子阵列中的所有元素的产品价值的轨道。
- 从后面遍历数组,对于每个元素, arr[i]执行以下操作:
- 通过arr[i]和(1 + res)的乘积增加 ans。
- 将 res 更新为arr[i]*(1 + res) 。
- 完成上述步骤后,打印ans 中存储的所有子数组的乘积之和。
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
using namespace std;
// Function that finds the sum of
// products of all subarray of arr[]
void sumOfSubarrayProd(int arr[], int n)
{
// Stores sum of all subarrays
int ans = 0;
int res = 0;
// Iterate array from behind
for(int i = n - 1; i >= 0; i--)
{
int incr = arr[i] * (1 + res);
// Update the ans
ans += incr;
// Update the res
res = incr;
}
// Print the final sum
cout << (ans);
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 1, 2, 3 };
// Size of array
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
sumOfSubarrayProd(arr, N);
}
// This code is contributed by mohit kumar 29
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function that finds the sum of
// products of all subarray of arr[]
static void sumOfSubarrayProd(int arr[],
int n)
{
// Stores sum of all subarrays
int ans = 0;
int res = 0;
// Iterate array from behind
for (int i = n - 1; i >= 0; i--) {
int incr = arr[i] * (1 + res);
// Update the ans
ans += incr;
// Update the res
res = incr;
}
// Print the final sum
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 1, 2, 3 };
// Size of array
int N = arr.length;
// Function Call
sumOfSubarrayProd(arr, N);
}
}
Python3
# Python3 program for the above approach
# Function that finds the sum of
# products of all subarray of arr[]
def sumOfSubarrayProd(arr, n):
# Stores sum of all subarrays
ans = 0
res = 0
# Iterate array from behind
i = n - 1
while(i >= 0):
incr = arr[i] * (1 + res)
# Update the ans
ans += incr
# Update the res
res = incr
i -= 1
# Print the final sum
print(ans)
# Driver Code
if __name__ == '__main__':
# Given array arr[]
arr = [ 1, 2, 3 ]
# Size of array
N = len(arr)
# Function call
sumOfSubarrayProd(arr, N)
# This code is contributed by ipg2016107
C#
// C# program for the
// above approach
using System;
// Function that finds
// the sum of products
// of all subarray of arr[]
class solution{
static void sumOfSubarrayProd(int []arr,
int n)
{
// Stores sum of all
// subarrays
int ans = 0;
int res = 0;
// Iterate array from behind
for(int i = n - 1; i >= 0; i--)
{
int incr = arr[i] * (1 + res);
// Update the ans
ans += incr;
// Update the res
res = incr;
}
// Print the final sum
Console.WriteLine(ans);
}
// Driver Code
public static void Main(String[] args)
{
// Given array arr[]
int []arr = {1, 2, 3 };
// Size of array
int N = arr.Length;
// Function call
sumOfSubarrayProd(arr, N);
}
}
// This code is contributed by SURENDRA_GANGWAR
Javascript
20
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live