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:64Explanation:

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:1944810000Explanation:

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.

- For example, let the array arr[] = {10, 3, 7}.
- All the non repeating possible subarrays of the above array are: {{10}, {10, 3}, {10, 7}, {10, 3, 7}, {3}, {7}, {3, 7}}.
- In the above sub-arrays, the frequency of every element can be observed as:
Frequency of 10 in subarrays = 4 Frequency of 3 in subarrays = 4 Frequency of 7 in subarrays = 4

- Here, the following identity holds true for the arrays of any length:
Frequency of element = 2

^{(arr.length-1)}

- Hence, in order to get the final product, we can simply perform:
product = 10 * (freq_of_10) * 3 * (freq_of_3) * 7 * (freq_of_7)

- Therefore, the idea is to simply iterate through the array and perform multiplication of every element and its corresponding frequency.

Below is the implementation of the above approach:

## C++

`// C++ program to find the product of ` `// all non-repeating Subarrays of an Array ` `#include <bits/stdc++.h> ` `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` |

*chevron_right*

*filter_none*

## 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)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Product of non-repeating (distinct) elements in an Array
- Minimum increments of Non-Decreasing Subarrays required to make Array Non-Decreasing
- Split array into subarrays at minimum cost by minimizing count of repeating elements in each subarray
- Find sum of non-repeating (distinct) elements in an array
- k-th distinct (or non-repeating) element in an array.
- Queries to check if any non-repeating element exists within range [L, R] of an Array
- Count of subarrays whose products don't have any repeating prime factor
- Product of all Subarrays of an Array
- Product of all Subarrays of an Array | Set 2
- Count all distinct pairs of repeating elements from the array for every array element
- Non-Repeating Element
- Find first non-repeating character in a given string using Linked List
- Non-repeating Primes
- Product of values of all possible non-empty subsets of given Array
- Minimum steps to make sum and the product of all elements of array non-zero
- Find all possible subarrays having product less than or equal to K
- Count subarrays with non-zero sum in the given Array
- Minimize count of increments of each element of subarrays required to make Array non-increasing
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows

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.