Product of all non repeating Subarrays of an Array

Given an array containing distinct integers arr[] of size N, the task is to print the product of all non-repeating subarrays of the array.
Examples:

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

Naive Approach: The naive approach for this problem is to generate all the sub-arrays of the given array and compute their product. The time complexity of this approach is exponential.

Efficient Approach: The idea is to make an observation. If we observe the non-repeating sub-arrays, we can observe that the occurrences of a single element in the sub-arrays follows a relationship with the length of the array.



Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

Output:
1944810000

Time Complexity: O(N), where N is the size of the array.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :