📜  数组的所有非重复子数组的乘积

📅  最后修改于: 2021-09-07 02:05:45             🧑  作者: Mango

给定一个包含大小为N 的不同整数arr[]的数组,任务是打印数组的所有非重复子数组的乘积。
例子:

朴素方法:这个问题的朴素方法是生成给定数组的所有子数组并计算它们的乘积。这种方法的时间复杂度是指数级的。
有效的方法:这个想法是进行观察。如果我们观察非重复子数组,我们可以观察到子数组中单个元素的出现与数组的长度有关系。

  • 例如,让数组 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