Product of all Subarrays of an Array | Set 2

Given an array arr[] of integers of size N, the task is to find the products of all subarrays of the array.

Examples:

Input: arr[] = {2, 4}
Output: 64
Explanation:
Here, subarrays are {2}, {2, 4}, and {4}.
Products of each subarray are 2, 8, 4.
Product of all Subarrays = 64

Input: arr[] = {1, 2, 3}
Output: 432
Explanation:
Here, subarrays are {1}, {1, 2}, {1, 2, 3}, {2}, {2, 3}, {3}.
Products of each subarray are 1, 2, 6, 2, 6, 3.
Product of all Subarrays = 432

Naive and Iterative approach: Please refer this post for these approaches.



Approach: The idea is to count the number of each element occurs in all the subarrays. To count we have below observations:

Therefore, from the above observations, the total number of each element arr[i] occurs in all the subarrays at every index i is given by:

total_elements = (N - i) + (N - i)*i
total_elements = (N - i)*(i + 1) 

The idea is to multiply each element (N – i)*(i + 1) number of times to get the product of elements in all subarrays.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the product of
// elements of all subarray
long int SubArrayProdct(int arr[],
                        int n)
{
    // Initialize the result
    long int result = 1;
  
    // Computing the product of
    // subarray using formula
    for (int i = 0; i < n; i++)
        result *= pow(arr[i],
                      (i + 1) * (n - i));
  
    // Return the product of all
    // elements of each subarray
    return result;
}
  
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 2, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    cout << SubArrayProdct(arr, N)
         << endl;
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach 
import java.util.*; 
  
class GFG{ 
  
// Function to find the product of
// elements of all subarray
static int SubArrayProdct(int arr[], int n)
{
      
    // Initialize the result
    int result = 1;
  
    // Computing the product of
    // subarray using formula
    for(int i = 0; i < n; i++)
       result *= Math.pow(arr[i], (i + 1) *
                                  (n - i));
  
    // Return the product of all
    // elements of each subarray
    return result;
}
  
// Driver code 
public static void main(String[] args) 
  
    // Given array arr[]
    int arr[] = new int[]{2, 4};
  
    int N = arr.length;
  
    // Function Call
    System.out.println(SubArrayProdct(arr, N)); 
  
// This code is contributed by Pratima Pandey 
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
  
# Function to find the product of
# elements of all subarray
def SubArrayProdct(arr, n):
  
    # Initialize the result
    result = 1;
  
    # Computing the product of
    # subarray using formula
    for i in range(0, n):
        result *= pow(arr[i],
                     (i + 1) * (n - i));
  
    # Return the product of all
    # elements of each subarray
    return result;
  
# Driver Code
  
# Given array arr[]
arr = [ 2, 4 ];
N = len(arr);
  
# Function Call
print(SubArrayProdct(arr, N))
  
# This code is contributed by Code_Mech
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach 
using System;
class GFG{ 
  
// Function to find the product of
// elements of all subarray
static int SubArrayProdct(int []arr, int n)
{
      
    // Initialize the result
    int result = 1;
  
    // Computing the product of
    // subarray using formula
    for(int i = 0; i < n; i++)
       result *= (int)(Math.Pow(arr[i], (i + 1) *
                                        (n - i)));
  
    // Return the product of all
    // elements of each subarray
    return result;
}
  
// Driver code 
public static void Main() 
  
    // Given array arr[]
    int []arr = new int[]{2, 4};
  
    int N = arr.Length;
  
    // Function Call
    Console.Write(SubArrayProdct(arr, N)); 
  
// This code is contributed by Code_Mech
chevron_right

Output:
64

Time Complexity: O(N), where N is the number of elements.
Auxiliary Space: O(1)





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.



Improved By : dewantipandeydp, Code_Mech

Article Tags :
Practice Tags :