给定一个大小为N的整数的数组arr [] ,任务是查找该数组所有子数组的乘积。
例子:
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
天真的和迭代的方法:请参考这篇文章以了解这些方法。
方法:想法是计算在所有子数组中出现的每个元素的数量。算一下,我们有以下观察结果:
- 在每个以arr [i]开头的子数组中,都有(N – i)个以元素arr [i]开头的子集。
例如:
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}
- 对于任何元素arr [i] ,都有(N – i)* i个子数组,其中arr [i]不是第一个元素。
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}
因此,根据上述观察,每个元素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
Javascript
64
时间复杂度: O(N) ,其中N是元素数。
辅助空间: O(1)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。