Product of all non repeating Subarrays of an Array
Last Updated :
13 Aug, 2021
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.
- 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++
#include <bits/stdc++.h>
using namespace std;
long product( int arr[], int n)
{
double occurrence = pow (2, n - 1);
double product = 1;
for ( int i = 0; i < n; i++)
{
product *= pow (arr[i], occurrence);
}
return ( long )product;
}
int main()
{
int arr[] = { 10, 3, 7 };
int len = sizeof (arr) / sizeof (arr[0]);
cout << product(arr, len);
return 0;
}
|
Java
public class GFG {
private static long product( int [] arr)
{
double occurrence = Math.pow( 2 , arr.length - 1 );
double product = 1 ;
for ( int i = 0 ; i < arr.length; i++) {
product *= Math.pow(arr[i], occurrence);
}
return ( long )product;
}
public static void main(String[] args)
{
int [] arr = { 10 , 3 , 7 };
System.out.println(product(arr));
}
}
|
Python3
def product(arr):
occurrence = pow ( 2 , len (arr) - 1 );
product = 1 ;
for i in range ( 0 , len (arr)):
product * = pow (arr[i], occurrence);
return product;
arr = [ 10 , 3 , 7 ];
print (product(arr));
|
C#
using System;
class GFG{
private static long product( int [] arr)
{
double occurrence = Math.Pow(2, arr.Length - 1);
double product = 1;
for ( int i = 0; i < arr.Length; i++)
{
product *= Math.Pow(arr[i], occurrence);
}
return ( long )product;
}
public static void Main(String[] args)
{
int [] arr = { 10, 3, 7 };
Console.WriteLine(product(arr));
}
}
|
Javascript
<script>
function product(arr)
{
let occurrence = Math.pow(2, arr.length - 1);
let product = 1;
for (let i = 0; i < arr.length; i++) {
product *= Math.pow(arr[i], occurrence);
}
return product;
}
let arr = [ 10, 3, 7 ];
document.write(product(arr));
</script>
|
Time Complexity: O(N), where N is the size of the array
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...