Given a number **N**, the task is to find the product of all the elements from all possible subsets of a set formed by first N natural numbers.

**Examples:**

Input:N = 2

Output:4

Possible subsets are {{1}, {2}, {1, 2}}.

Product of elements in subsets = {1} * {2} * {1 * 2} = 4

Input:N = 3

Output:1296

Possible subsets are {{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}

Product of elements in subsets = 1 * 2 * 3 * (1 * 2) * (1 * 3) * (2 * 3) * (1 * 2 * 3) = 1296

**Naive Approach:** A simple solution is to generate all subsets of first N natural number. Then for every subset, compute its product and finally return overall product of each subset.

**Efficient Approach:**

- It can be observed that each element of the original array appears in 2
^{(N – 1)}times in all subsets. - Therefore contribution of any element
**arr**in the final answer will be_{i}i * 2

^{(N – 1)} - So, the Sum of cubes of all Subsets will be
1

^{2}^{N-1}* 2^{2}^{N-1}* 3^{2}^{N-1}......N^{2}^{N-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 the product of all elements ` `// in all subsets in natural numbers from 1 to N ` `int` `product(` `int` `N) ` `{ ` ` ` `int` `ans = 1; ` ` ` `int` `val = ` `pow` `(2, N - 1); ` ` ` ` ` `for` `(` `int` `i = 1; i <= N; i++) { ` ` ` `ans *= ` `pow` `(i, val); ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 2; ` ` ` ` ` `cout << product(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG { ` ` ` ` ` `// Function to find the product of all elements ` ` ` `// in all subsets in natural numbers from 1 to N ` ` ` `static` `int` `product(` `int` `N) ` ` ` `{ ` ` ` `int` `ans = ` `1` `; ` ` ` `int` `val = (` `int` `)Math.pow(` `2` `, N - ` `1` `); ` ` ` ` ` `for` `(` `int` `i = ` `1` `; i <= N; i++) { ` ` ` `ans *= (` `int` `)Math.pow(i, val); ` ` ` `} ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `N = ` `2` `; ` ` ` ` ` `System.out.println(product(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to find the product of all elements ` `# in all subsets in natural numbers from 1 to N ` `def` `product(N) : ` ` ` `ans ` `=` `1` `; ` ` ` `val ` `=` `2` `*` `*` `(N ` `-` `1` `); ` ` ` ` ` `for` `i ` `in` `range` `(` `1` `, N ` `+` `1` `) : ` ` ` `ans ` `*` `=` `(i` `*` `*` `val); ` ` ` ` ` `return` `ans; ` ` ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `2` `; ` ` ` ` ` `print` `(product(N)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find the product of all elements ` ` ` `// in all subsets in natural numbers from 1 to N ` ` ` `static` `int` `product(` `int` `N) ` ` ` `{ ` ` ` `int` `ans = 1; ` ` ` `int` `val = (` `int` `)Math.Pow(2, N - 1); ` ` ` ` ` `for` `(` `int` `i = 1; i <= N; i++) { ` ` ` `ans *= (` `int` `)Math.Pow(i, val); ` ` ` `} ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main (` `string` `[] args) ` ` ` `{ ` ` ` `int` `N = 2; ` ` ` ` ` `Console.WriteLine(product(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

4

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:

- Sum of all subsets of a set formed by first n natural numbers
- Sum of sum of all subsets of a set formed by first N natural numbers
- Sum of product of all subsets formed by only divisors of N
- Divide first N natural numbers into 3 equal sum subsets
- Sum of series formed by difference between product and sum of N natural numbers
- Check if product of first N natural numbers is divisible by their sum
- Sum of subsets of all the subsets of an array | O(3^N)
- 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)
- Print all increasing sequences of length k from first n natural numbers
- Maximum LCM among all pairs (i, j) of first N natural numbers
- Find all divisors of first N natural numbers
- Sum of first N natural numbers with all powers of 2 added twice
- Maximum GCD among all pairs (i, j) of first N natural numbers
- 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
- Product of values of all possible non-empty subsets of given Array
- Product of all sorted subsets of size K using elements whose index divide K completely
- Print squares of first n natural numbers without using *, / and -
- LCM of First n Natural Numbers

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.