📜  所有可能的子阵列的乘积之和

📅  最后修改于: 2021-05-17 17:57:43             🧑  作者: Mango

给定N个正整数的数组arr [] ,任务是找到所有可能的子数组元素的乘积之和。

例子:

天真的方法:解决问题的最简单方法是生成所有可能的子数组,并计算每个子数组的所有元素的乘积,并将其加到最终总和上。

时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:为了优化上述方法,其想法是将问题观察为某种模式。假设我们有四个数字abcd 。我们可以将所有可能的子数组产品写为:

请按照以下步骤解决问题:

  • 遍历最后一个元素,并使重复出现的表达式随每个元素更新,并进一步使用它。在此过程中,相应地更新结果。
  • 初始化ANS0将保存最后一笔和资源0,将保留以前的子阵列中的所有元素的产品价值的轨道。
  • 从背面遍历数组,对于每个元素, arr [i]执行以下操作:
    • 将ans乘以arr [i](1 + res)的乘积。
    • 将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


输出:
20










时间复杂度: O(N)
辅助空间: O(1)