The Wayback Machine - https://web.archive.org/web/20240923003212/https://www.geeksforgeeks.org/backtracking-to-find-all-subsets/
Open In App

Print all subsets of a given Set or Array

Last Updated : 30 May, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Companies:
Show Topics
Solve Problem
Medium
44.3%
32.6K

Given an array Arr[] of size N, print all the subsets of the array.

Subset: A subset of an array is a tuple that can be obtained from the array by removing some (possibly all) elements of it

Example:

Input: N = 3, Arr = [1, 2, 3]
Output: {}
{1}
{1, 2}
{1, 2, 3}
{1, 3}
{2}
{2, 3}
{3}
Explanation: These are all the subsets that can be formed from the given array, it can be proven that no other subset exists other than the given output.

Input: N = 2, Arr = [2, 4]
Output: {}
{2}
{2, 4}
{4}
Explanation: These are all the subsets that can be formed from the given array, it can be proven that no other subset exists other than the given output.

How many Subsets are possible for an Array of size ‘N’ ?

Before jumping into the solution, can we observe some kind of relation between the size of array N and the number of subsets formed by that array? The answer is YES, there exists a relation that is given by the following formula:

Number of Subsets of an array of size N = [Tex]2^N [/Tex]

Proof: For each element of the array we have 2 choices:

  • Choice 1: Include it into the subset.
  • Choice 2: Exclude it from the subset.

Since, each element has 2 choice to contribute into the subset and we have total N elements, therefore total subsets = [Tex]2^N [/Tex]

Let’s see how can we construct our solution from this observation.

Printing all subsets using Backtracking:

As mentioned above, for each element there two options, either include it into subset or exclude it. Backtracking algorithm can allow us to explore all possible choices one by one recursively.

State Space Tree for printing all subsets using Backtracking:

Suppose an array of size 3 having elements {1, 2, 3}, the state space tree can be constructed as below:

State Space Tree of Subset

Follow the the steps below to implement the above idea:

  • It starts with an empty subset and adds it to the result list.
  • It iterates through the elements of the input vector:
    • Includes the current element in the subset.
    • Recursively calls itself with the updated subset and the next index.
    • Excludes the current element from the subset (backtracks).

Below are the implementation of the above approach:

C++

#include <iostream> #include <vector> using namespace std; void calcSubset(vector<int>& A, vector<vector<int> >& res, vector<int>& subset, int index) { // Add the current subset to the result list res.push_back(subset); // Generate subsets by recursively including and // excluding elements for (int i = index; i < A.size(); i++) { // Include the current element in the subset subset.push_back(A[i]); // Recursively generate subsets with the current // element included calcSubset(A, res, subset, i + 1); // Exclude the current element from the subset // (backtracking) subset.pop_back(); } } vector<vector<int> > subsets(vector<int>& A) { vector<int> subset; vector<vector<int> > res; int index = 0; calcSubset(A, res, subset, index); return res; } // Driver code int main() { vector<int> array = { 1, 2, 3 }; vector<vector<int> > res = subsets(array); // Print the generated subsets for (int i = 0; i < res.size(); i++) { for (int j = 0; j < res[i].size(); j++) cout << res[i][j] << " "; cout << endl; } return 0; }

Java

import java.util.ArrayList; import java.util.List; public class Subsets { public static void calcSubset(List<Integer> A, List<List<Integer> > res, List<Integer> subset, int index) { // Add the current subset to the result list res.add(new ArrayList<>(subset)); // Generate subsets by recursively including and // excluding elements for (int i = index; i < A.size(); i++) { // Include the current element in the subset subset.add(A.get(i)); // Recursively generate subsets with the current // element included calcSubset(A, res, subset, i + 1); // Exclude the current element from the subset // (backtracking) subset.remove(subset.size() - 1); } } public static List<List<Integer> > subsets(List<Integer> A) { List<Integer> subset = new ArrayList<>(); List<List<Integer> > res = new ArrayList<>(); int index = 0; calcSubset(A, res, subset, index); return res; } // Driver code public static void main(String[] args) { List<Integer> array = List.of(1, 2, 3); List<List<Integer> > res = subsets(array); // Print the generated subsets for (List<Integer> subset : res) { for (Integer num : subset) { System.out.print(num + " "); } System.out.println(); } } }

Python

def calcSubset(A, res, subset, index): # Add the current subset to the result list res.append(subset[:]) # Generate subsets by recursively including and excluding elements for i in range(index, len(A)): # Include the current element in the subset subset.append(A[i]) # Recursively generate subsets with the current element included calcSubset(A, res, subset, i + 1) # Exclude the current element from the subset (backtracking) subset.pop() def subsets(A): subset = [] res = [] index = 0 calcSubset(A, res, subset, index) return res # Driver code if __name__ == "__main__": array = [1, 2, 3] res = subsets(array) # Print the generated subsets for subset in res: print(*subset)

C#

using System; using System.Collections.Generic; class Program { static void CalcSubset(List<int> A, List<List<int> > res, List<int> subset, int index) { // Add the current subset to the result list res.Add(new List<int>(subset)); // Generate subsets by recursively including and // excluding elements for (int i = index; i < A.Count; i++) { // Include the current element in the subset subset.Add(A[i]); // Recursively generate subsets with the current // element included CalcSubset(A, res, subset, i + 1); // Exclude the current element from the subset // (backtracking) subset.RemoveAt(subset.Count - 1); } } static List<List<int> > Subsets(List<int> A) { List<int> subset = new List<int>(); List<List<int> > res = new List<List<int> >(); int index = 0; CalcSubset(A, res, subset, index); return res; } // Driver code static void Main(string[] args) { List<int> array = new List<int>{ 1, 2, 3 }; List<List<int> > res = Subsets(array); // Print the generated subsets foreach(List<int> subset in res) { Console.WriteLine(string.Join(" ", subset)); } } }

JavaScript

function calcSubset(A, res, subset, index) { // Add the current subset to the result list res.push([...subset]); // Generate subsets by recursively including and excluding elements for (let i = index; i < A.length; i++) { // Include the current element in the subset subset.push(A[i]); // Recursively generate subsets with the current element included calcSubset(A, res, subset, i + 1); // Exclude the current element from the subset (backtracking) subset.pop(); } } function subsets(A) { const subset = []; const res = []; let index = 0; calcSubset(A, res, subset, index); return res; } // Driver code function main() { const array = [1, 2, 3]; const res = subsets(array); // Print the generated subsets for (let i = 0; i < res.length; i++) { console.log(res[i].join(' ')); } } // Call the main function main();


Output

1 1 2 1 2 3 1 3 2 2 3 3

Complexity Analysis:

  • Time Complexity: O(N*2^N), where n is the size of given array.
  • Auxiliary Space:  O(N * 2^N)
    • O(N) : if we only print our subsets, there will at max N recursion stack
    • O(2^N) : if we would store all the subsets we will need 2^N memory blocks to store each subset

Printing all Subsets Using Bit Manipulation

This approach is simpler compared to backtracking, as it just requires basic knowledge of bits.

Observation: A bit can be either 0 or 1. What can we deduce from this?

Since, each element has only two choices i.e. either get included or get excluded. Assign these choices to a bit representation such that:
0 means Excluded
1 means Included
i’th bit represents i’th element of the array

Now let’s say, there are N elements in the array. This array will have 2^N subsets. These subsets can be uniquely expressed in form of Bit representation of number from 0 to (2^N)-1.

Example: If elements in an array of size 2 = {A, B}
All the subsets of this array form the bit representation of number from 0 to (2^2)-1 i.e. 0 to 3

0 = 00 => A excluded, B excluded => { empty }
1 = 01 => A excluded, B included => { B }
2 = 10 => A included, B excluded => { A }
3 = 11 => A included, B included=> { A, B }

Illustration:

Suppose given an array of size 3 = {1, 2, 3}. Generate all the subsets using bit manipulation as shown in the image below:

bit-representation-of-subset

Below are the step-by-step approach:

  • Iterate numbers from 0 to (2^n)-1
  • Generate binary representation of that number and include the elements of array as per below cases:
    • If the i’th bit is 1, then include i’th element of the array into current subset
    • If the i’th bit is 0, do nothing and continue.
  • Each bit representation of the number will give a unique subset.

Below are the implementation of the above approach:

C++

#include <iostream> using namespace std; // Function to find the subsets of the given array void findSubsets(int nums[], int n) { // Loop through all possible subsets using bit manipulation for (int i = 0; i < (1 << n); i++) { // Loop through all elements of the input array for (int j = 0; j < n; j++) { // Check if the jth bit is set in the current subset if ((i & (1 << j)) != 0) { // If the jth bit is set, add the jth element to the subset cout << nums[j] << " "; } } cout << endl; } } // Driver Code int main() { int arr[] = { 1, 2, 3 }; int n = sizeof(arr)/sizeof(arr[0]); findSubsets(arr, n); }

Java

// Java program for the above approach import java.io.*; import java.util.*; class GFG { // Function to find the subsets of // the given array public static void findSubsets(int[] nums) { // Get the length of the input array int n = nums.length; // Loop through all possible subsets // using bit manipulation for (int i = 0; i < (1 << n); i++) { // Loop through all elements // of the input array for (int j = 0; j < n; j++) { // Check if the jth bit is set // in the current subset if ((i & (1 << j)) != 0) { // If the jth bit is set, // add the jth // element to the subset System.out.print(nums[j] + " "); } } System.out.println(); } } // Driver Code public static void main(String[] args) { int[] arr = new int[] { 1, 2, 3 }; findSubsets(arr); } }

Python

#Function to find the subsets of the given array def findSubsets(nums, n): # Loop through all possible subsets using bit manipulation for i in range(1 << n): # Loop through all elements of the input array for j in range(n): #Check if the jth bit is set in the current subset if (i & (1 << j)) != 0: #If the jth bit is set, add the jth element to the subset print(nums[j], end=" ") print() #Driver Code arr = [1, 2, 3] n = len(arr) findSubsets(arr, n)

C#

using System; public class GFG { // Function to find the subsets of the given array static void FindSubsets(int[] nums) { int n = nums.Length; // Loop through all possible subsets using bit // manipulation for (int i = 0; i < (1 << n); i++) { // Loop through all elements of the input array for (int j = 0; j < n; j++) { // Check if the jth bit is set in the // current subset if ((i & (1 << j)) != 0) { // If the jth bit is set, add the jth // element to the subset Console.Write(nums[j] + " "); } } Console.WriteLine(); } } // Driver Code public static void Main(string[] args) { int[] arr = { 1, 2, 3 }; FindSubsets(arr); } } // This code is contributed by shivamgupta0987654321

JavaScript

// Function to find the subsets of the given array function findSubsets(nums, n) { // Loop through all possible subsets using bit manipulation for (let i = 0; i < (1 << n); i++) { // Loop through all elements of the input array for (let j = 0; j < n; j++) { // Check if the jth bit is set in the current subset if ((i & (1 << j)) !== 0) { // If the jth bit is set, add the jth element to the subset process.stdout.write(nums[j] + " "); } } console.log(); } } // Driver Code let arr = [1, 2, 3]; let n = arr.length; findSubsets(arr, n); // THIS CODE IS CONTRIBUTED BY YASH AGARWAL(YASHAGARWAL2852002)


Output

1 2 1 2 3 1 3 2 3 1 2 3

Complexity Analysis:

  • Time Complexity: O(N*2^N), where N is the size of given array.
  • Auxiliary Space:
    • O(1) : if we only print our subsets
    • O(2^N) : if we would store all the subsets we will need 2^N memory blocks to store each subset


Previous Article
Next Article

Similar Reads

Sum of subsets of all the subsets of an array | O(3^N)
Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset will be {} and {1}, Sum = 0 + 1 = 1 c) {1} : 1 All t
6 min read
Sum of subsets of all the subsets of an array | O(2^N)
Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset will be {} and {1}, Sum = 0 + 1 = 1 c) {1} : 1 All t
6 min read
Split array into minimum number of subsets such that elements of all pairs are present in different subsets at least once
Given an array arr[] consisting of N distinct integers, the task is to find the minimum number of times the array needs to be split into two subsets such that elements of each pair are present into two different subsets at least once. Examples: Input: arr[] = { 3, 4, 2, 1, 5 } Output: 3 Explanation: Possible pairs are { (1, 2), (1, 3), (1, 4), (1,
6 min read
Sum of subsets of all the subsets of an array | O(N)
Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset will be {} and {1}, Sum = 0 + 1 = 1 c) {1} : 1 All t
7 min read
Print sums of all subsets of a given set
Given an array of integers, print sums of all subsets in it. Output sums can be printed in any order. Examples : Input : arr[] = {2, 3} Output: 0 2 3 5 Input : arr[] = {2, 4, 5} Output : 0 2 4 5 6 7 9 11Recommended PracticeSubset SumsTry It! Method 1 (Recursive) We can recursively solve this problem. There are total 2n subsets. For every element, w
12 min read
Print all subsets of given size of a set
Generate all possible subsets of size r of the given array with distinct elements. Examples: Input : arr[] = {1, 2, 3, 4} r = 2Output : 1 2 1 3 1 4 2 3 2 4 3 4Input : arr[] = {10, 20, 30, 40, 50} r = 3Output : 10 20 30 10 20 40 10 20 50 10 30 40 10 30 50 10 40 50 20 30 40 20 30 50 20 40 50 30 40 50 Recommended: Please try your approach on {IDE} fir
15+ min read
Divide array in two Subsets such that sum of square of sum of both subsets is maximum
Given an integer array arr[], the task is to divide this array into two non-empty subsets such that the sum of the square of the sum of both the subsets is maximum and sizes of both the subsets must not differ by more than 1.Examples: Input: arr[] = {1, 2, 3} Output: 26 Explanation: Sum of Subset Pairs are as follows (1)2 + (2 + 3)2 = 26 (2)2 + (1
6 min read
Maximum number of subsets an array can be split into such that product of their minimums with size of subsets is at least K
Given an array arr[] consisting of N integers and an integer K, the task is to find the maximum number of disjoint subsets that the given array can be split into such that the product of the minimum element of each subset with the size of the subset is at least K. Examples: Input: arr[] = {7, 11, 2, 9, 5}, K = 10Output: 2Explanation:All such disjoi
8 min read
Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
Given an array of size N. The task is to partition the given array into two subsets such that the average of all the elements in both subsets is equal. If no such partition exists print -1. Otherwise, print the partitions. If multiple solutions exist, print the solution where the length of the first subset is minimum. If there is still a tie then p
14 min read
Recursive program to print all subsets with given sum
Given an array and a number, print all subsets with sum equal to given the sum.Examples: Input : arr[] = {2, 5, 8, 4, 6, 11}, sum = 13 Output : 5 8 2 11 2 5 6 Input : arr[] = {1, 5, 8, 4, 6, 11}, sum = 9 Output : 5 4 1 8 This problem is an extension of check if there is a subset with given sum. We recursively generate all subsets. We keep track of
6 min read
Perfect Sum Problem (Print all subsets with given sum)
Given an array of integers and a sum, the task is to print all subsets of the given array with a sum equal to a given sum. Examples: Input : arr[] = {2, 3, 5, 6, 8, 10} sum = 10 Output : 5 2 3 2 8 10 Input : arr[] = {1, 2, 3, 4, 5} sum = 10 Output : 4 3 2 1 5 3 2 5 4 1 Asked in Tesco Recommended PracticePerfect Sum ProblemTry It! This problem is ma
15 min read
Print all possible ways to split an array into K subsets
Given an array arr[] of size N and an integer K, the task is to print all possible ways to split the given array into K subsets. Examples: Input: arr[] = { 1, 2, 3 }, K = 2Output: { {{ 1, 2 }, { 3 }}, {{ 1, 3 }, { 2 }}, {{ 1 }, { 2, 3 }}}. Input: arr[] = { 1, 2, 3, 4 }, K = 2Output: { {{ 1, 2, 3 }, { 4 }}, {{ 1, 2, 4 }, { 3 }}, {{ 1, 2 }, { 3, 4 }}
13 min read
Sum of bitwise OR of all possible subsets of given set
Given an array arr[] of size n, we need to find sum of all the values that comes from ORing all the elements of the subsets. Prerequisites : Subset Sum of given set Examples : Input : arr[] = {1, 2, 3} Output : 18 Total Subsets = 23 -1= 7 1 = 1 2 = 2 3 = 3 1 | 2 = 3 1 | 3 = 3 2 | 3 = 3 1 | 2 | 3 = 3 0(empty subset) Now SUM of all these ORs = 1 + 2
8 min read
Sum of bitwise AND of all possible subsets of given set
Given an array, we need to calculate the Sum of Bit-wise AND of all possible subsets of the given array. Examples: Input : 1 2 3 Output : 9 For [1, 2, 3], all possible subsets are {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3} Bitwise AND of these subsets are, 1 + 2 + 3 + 0 + 1 + 2 + 0 = 9. So, the answer would be 9. Input : 1 2 3 4 Output : 13 R
7 min read
Find all unique subsets of a given set using C++ STL
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets, using C++ STL. Note: Each subset should be sorted. Examples: Input: N = 3, arr[] = {2, 1, 2}Output:(), (1), (1 2), (1 2 2), (2), (2 2)Explanation: All possible subsets = (), (2), (1), (1, 2), (2), (2 2), (2 1), (2, 1, 2)After S
3 min read
Given a set, find XOR of the XOR's of all subsets.
The question is to find XOR of the XOR's of all subsets. i.e if the set is {1,2,3}. All subsets are : [{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}]. Find the XOR of each of the subset and then find the XOR of every subset result.We strongly recommend you to minimize your browser and try this yourself first.This is a very simple question to sol
5 min read
Finding all subsets of a given set in Java
Problem: Find all the subsets of a given set. Input: S = {a, b, c, d} Output: {}, {a} , {b}, {c}, {d}, {a,b}, {a,c}, {a,d}, {b,c}, {b,d}, {c,d}, {a,b,c}, {a,b,d}, {a,c,d}, {b,c,d}, {a,b,c,d} The total number of subsets of any given set is equal to 2^ (no. of elements in the set). If we carefully notice it is nothing but binary numbers from 0 to 15
2 min read
Find all Unique Subsets of a given Set
Given an array A[] of positive integers, print all the unique non-empty subsets of the array  Note: The set can not contain duplicate elements, so any repeated subset should be considered only once in the output. Examples:  Input: A[] = {1, 5, 6}Output: {{1}, {1, 5}, {1, 6}, {5}, {5, 6}, {6}, {1, 5, 6}}Explanation: The number of all the non-empty p
15+ min read
Find all distinct subsets of a given set using BitMasking Approach
Given an array of integers arr[], The task is to find all its subsets. The subset can not contain duplicate elements, so any repeated subset should be considered only once in the output. Examples: Input: S = {1, 2, 2}Output: {}, {1}, {2}, {1, 2}, {2, 2}, {1, 2, 2}Explanation: The total subsets of given set are - {}, {1}, {2}, {2}, {1, 2}, {1, 2}, {
12 min read
Sum of squares of all Subsets of given Array
Given an array arr[]. The value of a subset of array A is defined as the sum of squares of all the numbers in that subset. The task is to calculate the sum of values of all possible non-empty subsets of the given array. Since, the answer can be large print the val mod 1000000007.Examples: Input: arr[] = {3, 7} Output: 116 val({3}) = 32 = 9 val({7})
5 min read
Sum of values of all possible non-empty subsets of the given array
Given an array arr[] of N integers, the task is to find the sum of values of all possible non-empty subsets of array the given array.Examples: Input: arr[] = {2, 3} Output: 10 All non-empty subsets are {2}, {3} and {2, 3} Total sum = 2 + 3 + 2 + 3 = 10Input: arr[] = {2, 1, 5, 6} Output: 112 Approach: It can be observed that when all the elements ar
4 min read
Product of values of all possible non-empty subsets of given Array
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
3 min read
Sum of products of all possible K size subsets of the given array
Given an array arr[] of N non-negative integers and an integer 1 ? K ? N. The task is to find the sum of the products of all possible subsets of arr[] of size K. Examples: Input: arr[] = {1, 2, 3, 4}, K = 2 Output: 35 (1 * 2) + (1 * 3) + (1 * 4) + (2 * 3) + (2 * 4) + (3 * 4) = 2 + 3 + 4 + 6 + 8 + 12 = 35 Input: arr[] = {1, 2, 3, 4}, K = 3 Output: 5
15+ min read
Sum of all subsets whose sum is a Perfect Number from a given array
Given an array arr[] consisting of N integers, the task is to find the sum of all subsets from an array, whose sum is a Perfect Number. Examples: Input: arr[] = {5, 4, 6}Output: 6Explanation:All possible subsets from the array arr[] are:{5} ? Sum = 5{4} ? Sum = 4.{6} ? Sum = 6.{5, 4} ? Sum = 9.{5, 6} ? Sum = 11.{4, 6} ? Sum = 10.{5, 4, 6} ? Sum = 1
14 min read
Find maximum AND value among all K-size subsets of given Array
Given an array arr[] containing N non-negative integers, the task is to find the maximum AND value among all the subsets having length K. Examples: Input: arr[] = {1, 6, 9, 7}, K = 1Output: 9Explanation: As only one element is allowed 9 is the greatest value that can be obtained. Input: arr[] = {3, 3, 3}, K = 2Output: 3 Input: arr[] = {7, 8, 9, 10,
7 min read
Sum of cubes of all Subsets of given Array
Given an array arr[] , the task is to calculate the sum of cubes of all possible non-empty subsets of the given array. Since, the answer can be large, print the value as mod 1000000007. Examples: Input: arr[] = {1, 2} Output: 18 subset({1}) = 13 = 1 subsetval({2}) = 23 = 8 subset({1, 2}) = 13 + 23 = 1 + 8 = 9 Sum of cubes of all Subsets = 1 + 8 + 9
5 min read
Maximize count of subsets into which the given array can be split such that it satisfies the given condition
Given an array arr[] of size N and a positive integer X, the task is to partition the array into the maximum number of subsets such that the multiplication of the smallest element of each subset with the count of elements in the subsets is greater than or equal to K. Print the maximum count of such subsets possible. Examples: Input: arr[] = {1, 3,
11 min read
Sum of sum of all subsets of a set formed by first N natural numbers
Given N, and ff(N) = f(1) + f(2) + ...... + f(N), where f(k) is the sum of all subsets of a set formed by first k natural numbers. The task is to find ff(N) modulo 1000000007. Examples: Input: 2 Output: 7 f(1) + f(2) f(1) = 1 = 1 f(2) = 1 + 2 + {1 + 2} = 6 Input: 3 Output: 31 f(1) + f(2) + f(3) f(1) = 1 = 1 f(2) = 1 + 2 + {1 + 2} = 6 f(3) = 1 + 2 +
11 min read
Product of all Subsets of a set formed by first N natural numbers
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} = 4Input: N = 3 Output: 1296 Possible subsets are {{1}, {2}, {3}, {1, 2}, {1, 3}, {2
4 min read
Sum of all subsets of a set formed by first n natural numbers
Given a number n, we need to find the sum of all the elements from all possible subsets of a set formed by first n natural numbers.Examples : Input : n = 2 Output : 6 Possible subsets are {{1}, {2}, {1, 2}}. Sum of elements in subsets is 1 + 2 + 1 + 2 = 6 Input : n = 3 Output : 24 Possible subsets are {{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2,
4 min read
Article Tags :
Practice Tags :