给定一个包含大小为N 的不同整数arr[]的数组,任务是打印数组的所有非重复子数组的乘积。
例子:
Input: arr[] = {2, 4}
Output: 64
Explanation:
The possible subarrays for the given array are {2}, {2, 4}, {4}
The products are 2, 8, 4 respectively. Therefore, the overall product of all the subarrays = 64
Input: arr[] = {10, 3, 7}
Output: 1944810000
Explanation:
The possible subarrays for the given array are {10}, {10, 3}, {0, 7}, {10, 3, 7}, {3}, {7}, {3, 7}.
The products are 10, 30, 70, 210, 3, 7, 21 respectively. Therefore, the overall product of all the subarrays = 1944810000
朴素方法:这个问题的朴素方法是生成给定数组的所有子数组并计算它们的乘积。这种方法的时间复杂度是指数级的。
有效的方法:这个想法是进行观察。如果我们观察非重复子数组,我们可以观察到子数组中单个元素的出现与数组的长度有关系。
- 例如,让数组 arr[] = {10, 3, 7}。
- 上述数组的所有非重复可能子数组为:{{10}, {10, 3}, {10, 7}, {10, 3, 7}, {3}, {7}, {3, 7} }.
- 在上面的子数组中,可以观察到每个元素的频率为:
Frequency of 10 in subarrays = 4
Frequency of 3 in subarrays = 4
Frequency of 7 in subarrays = 4
- 这里,以下恒等式适用于任意长度的数组:
Frequency of element = 2(arr.length-1)
- 因此,为了获得最终产品,我们可以简单地执行:
product = 10 * (freq_of_10) * 3 * (freq_of_3) * 7 * (freq_of_7)
- 因此,想法是简单地遍历数组并执行每个元素及其对应频率的乘法。
下面是上述方法的实现:
C++
// C++ program to find the product of
// all non-repeating Subarrays of an Array
#include
using namespace std;
// Function to find the product of
// all non-repeating Subarrays of an Array
long product(int arr[], int n)
{
// Finding the occurrence of every element
double occurrence = pow(2, n - 1);
double product = 1;
// Iterating through the array and
// finding the product
for(int i = 0; i < n; i++)
{
// We are taking the power of each
// element in array with the occurrence
// and then taking product of those.
product *= pow(arr[i], occurrence);
}
return (long)product;
}
// Driver code
int main()
{
int arr[] = { 10, 3, 7 };
int len = sizeof(arr) / sizeof(arr[0]);
cout << product(arr, len);
return 0;
}
// This code is contributed by PrinciRaj1992
Java
// Java program to find the product of
// all non-repeating Subarrays of an Array
public class GFG {
// Function to find the product of
// all non-repeating Subarrays of an Array
private static long product(int[] arr)
{
// Finding the occurrence of every element
double occurrence = Math.pow(2, arr.length - 1);
double product = 1;
// Iterating through the array and
// finding the product
for (int i = 0; i < arr.length; i++) {
// We are taking the power of each
// element in array with the occurrence
// and then taking product of those.
product *= Math.pow(arr[i], occurrence);
}
return (long)product;
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 10, 3, 7 };
System.out.println(product(arr));
}
}
Python3
# Python3 program to find the product of
# all non-repeating Subarrays of an Array
# Function to find the product of
# all non-repeating Subarrays of an Array
def product(arr):
# Finding the occurrence of every element
occurrence = pow(2, len(arr) - 1);
product = 1;
# Iterating through the array and
# finding the product
for i in range(0, len(arr)):
# We are taking the power of each
# element in array with the occurrence
# and then taking product of those.
product *= pow(arr[i], occurrence);
return product;
# Driver code
arr = [ 10, 3, 7 ];
print(product(arr));
# This code is contributed by Code_Mech
C#
// C# program to find the product of
// all non-repeating Subarrays of an Array
using System;
class GFG{
// Function to find the product of
// all non-repeating Subarrays of an Array
private static long product(int[] arr)
{
// Finding the occurrence of every element
double occurrence = Math.Pow(2, arr.Length - 1);
double product = 1;
// Iterating through the array and
// finding the product
for (int i = 0; i < arr.Length; i++)
{
// We are taking the power of each
// element in array with the occurrence
// and then taking product of those.
product *= Math.Pow(arr[i], occurrence);
}
return (long)product;
}
// Driver code
public static void Main(String[] args)
{
int[] arr = { 10, 3, 7 };
Console.WriteLine(product(arr));
}
}
// This code is contributed by amal kumar choubey
Javascript
1944810000
时间复杂度: O(N) ,其中 N 是数组的大小。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live