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

📅  最后修改于: 2021-05-17 17:56:39             🧑  作者: 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 occurance = 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 occurance
        // and then taking product of those.
        product *= pow(arr[i], occurance);
    }
    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 occurance = 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 occurance
            // and then taking product of those.
            product *= Math.pow(arr[i], occurance);
        }
  
        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
    occurance = 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 occurance
        # and then taking product of those.
        product *= pow(arr[i], occurance);
      
    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 occurance = 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 occurance
        // and then taking product of those.
        product *= Math.Pow(arr[i], occurance);
    }
    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


输出:
1944810000

时间复杂度: O(N) ,其中N是数组的大小。