This post is completed by 3 users

  • 1
Add to List
Hard

121. Print All the Subsets of a Given Set (Power Set)

Objective: Given a set of numbers, print all the posssible subsets of it including empty set.

Power Set: In mathematics, PowerSet of any given set S, PS(S) is set of all subsets of S including empty set.
Example:

 S ={1,2,3}
PS(S): {{ᵩ}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}.

Approach:

  1. Use Recursion – We break the problem into smaller subproblems.
  2. Two Choices for Each Element
    • Include the current element in the subset.
    • Exclude the current element from the subset.
  3. Base Case – When we reach the end of the array, print the current subset.
  4. Recursive Calls – Move to the next element with both choices (include/exclude).
  5. Generate All Subsets – This way, we explore all possible subsets of the array.
 

Output:

123
12
13
1
23
2
3
Empty

Similar Problem - Print All Combinations of subset of size K from Given Array