This post is completed by 3 users
|
Add to List |
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:
- Use Recursion – We break the problem into smaller subproblems.
- Two Choices for Each Element –
- Include the current element in the subset.
- Exclude the current element from the subset.
- Base Case – When we reach the end of the array, print the current subset.
- Recursive Calls – Move to the next element with both choices (include/exclude).
- 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