Given an array of size N. The task is to find the product of values of all possible non-empty subsets of the given array.
Examples:
Input: N = 2, arr[] = {3, 7}
Output: 441
All non empty subsets are:
3
7
3, 7
Product = 3 * 7 * 3 * 7 = 441Input: N = 1, arr[] = {4}
Output: 4
Approach: On careful observation it can be deduced that number of occurrences of every element across all subsets is 2N-1. Therefore, in the final product, every element of the array will be multiplied to itself by 2N-1 times.
Product = a[0]2N-1*a[1]2N-1********a[N-1]2N-1
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to find product of all elements // in all subsets int product(int a[], int n) { int ans = 1; int val = pow(2, n - 1); for (int i = 0; i < n; i++) { ans *= pow(a[i], val); } return ans; } // Driver Code int main() { int n = 2; int a[] = { 3, 7 }; cout << product(a, n); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to find product of all elements // in all subsets static int product(int a[], int n) { int ans = 1; int val = (int)Math.pow(2, n - 1); for (int i = 0; i < n; i++) { ans *= (int)Math.pow(a[i], val); } return ans; } // Driver Code public static void main (String[] args) { int n = 2; int a[] = { 3, 7 }; System.out.println(product(a, n)); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the approach # Function to find product of # all elements in all subsets def product(a, n): ans = 1 val = pow(2, n - 1) for i in range(n): ans *= pow(a[i], val) return ans # Driver Code n = 2a = [3, 7] print(product(a, n)) # This code is contributed by mohit kumar |
C#
// C# implementation of the approach using System; class GFG { // Function to find product of all elements // in all subsets static int product(int []a, int n) { int ans = 1; int val = (int)Math.Pow(2, n - 1); for (int i = 0; i < n; i++) { ans *= (int)Math.Pow(a[i], val); } return ans; } // Driver Code public static void Main () { int n = 2; int []a = { 3, 7 }; Console.WriteLine(product(a, n)); } } // This code is contributed by anuj_67.. |
441
Time Complexity: O(N)
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:
- Number of subsets with same AND, OR and XOR values in an Array
- Sum of values of all possible non-empty subsets of the given array
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Number of subsets with product less than k
- Product of Primes of all Subsets
- Maximum and Minimum Product Subsets
- Sum of product of all subsets formed by only divisors of N
- Total number of subsets in which the product of the elements is even
- Product of all Subsets of a set formed by first N natural numbers
- Subsets of size K with product equal to difference of two perfect squares
- Product of all sorted subsets of size K using elements whose index divide K completely
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Sum of subsets of all the subsets of an array | O(3^N)
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Sum of cubes of all Subsets of given Array
- Sum of squares of all Subsets of given Array
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.

