📜  数组的所有子数组的乘积|套装2

📅  最后修改于: 2021-05-19 18:28:44             🧑  作者: Mango

给定一个大小为N的整数的数组arr [] ,任务是查找该数组所有子数组的乘积。

例子:

天真的和迭代的方法:请参考这篇文章以了解这些方法。

方法:想法是计算在所有子数组中出现的每个元素的数量。为了计数,我们有以下观察结果:

  • 在以arr [i]开头的每个子数组中,都有(N – i)个从元素arr [i]开头的子集。
    例如:
  • 对于任何元素arr [i] ,都有(N – i)* i个子数组,其中arr [i]不是第一个元素。

因此,根据上述观察,每个元素i的所有子数组中出现的每个元素arr [i]的总数由下式给出:

total_elements = (N - i) + (N - i)*i
total_elements = (N - i)*(i + 1) 

这个想法是将每个元素乘以(N – i)*(i + 1)次,以获得所有子数组中元素的乘积。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to find the product of
// elements of all subarray
long int SubArrayProdct(int arr[],
                        int n)
{
    // Initialize the result
    long int result = 1;
  
    // Computing the product of
    // subarray using formula
    for (int i = 0; i < n; i++)
        result *= pow(arr[i],
                      (i + 1) * (n - i));
  
    // Return the product of all
    // elements of each subarray
    return result;
}
  
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 2, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    cout << SubArrayProdct(arr, N)
         << endl;
    return 0;
}


Java
// Java program for the above approach 
import java.util.*; 
  
class GFG{ 
  
// Function to find the product of
// elements of all subarray
static int SubArrayProdct(int arr[], int n)
{
      
    // Initialize the result
    int result = 1;
  
    // Computing the product of
    // subarray using formula
    for(int i = 0; i < n; i++)
       result *= Math.pow(arr[i], (i + 1) *
                                  (n - i));
  
    // Return the product of all
    // elements of each subarray
    return result;
}
  
// Driver code 
public static void main(String[] args) 
{ 
  
    // Given array arr[]
    int arr[] = new int[]{2, 4};
  
    int N = arr.length;
  
    // Function Call
    System.out.println(SubArrayProdct(arr, N)); 
} 
} 
  
// This code is contributed by Pratima Pandey


Python3
# Python3 program for the above approach
  
# Function to find the product of
# elements of all subarray
def SubArrayProdct(arr, n):
  
    # Initialize the result
    result = 1;
  
    # Computing the product of
    # subarray using formula
    for i in range(0, n):
        result *= pow(arr[i],
                     (i + 1) * (n - i));
  
    # Return the product of all
    # elements of each subarray
    return result;
  
# Driver Code
  
# Given array arr[]
arr = [ 2, 4 ];
N = len(arr);
  
# Function Call
print(SubArrayProdct(arr, N))
  
# This code is contributed by Code_Mech


C#
// C# program for the above approach 
using System;
class GFG{ 
  
// Function to find the product of
// elements of all subarray
static int SubArrayProdct(int []arr, int n)
{
      
    // Initialize the result
    int result = 1;
  
    // Computing the product of
    // subarray using formula
    for(int i = 0; i < n; i++)
       result *= (int)(Math.Pow(arr[i], (i + 1) *
                                        (n - i)));
  
    // Return the product of all
    // elements of each subarray
    return result;
}
  
// Driver code 
public static void Main() 
{ 
  
    // Given array arr[]
    int []arr = new int[]{2, 4};
  
    int N = arr.Length;
  
    // Function Call
    Console.Write(SubArrayProdct(arr, N)); 
} 
} 
  
// This code is contributed by Code_Mech


输出:
64

时间复杂度: O(N) ,其中N是元素数。
辅助空间: O(1)