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:

Solution to this problem is similar to - Print All Combinations of subset of size K from Given Array

  1. Create a binary array of the same size as the given array.
  2. Now for every integer, we have two options, whether to select it or ignore it.
  3. Now if we select it, we will put 1 in the boolean array at the corresponding index, or if we ignore it, put 0 at that index.
  4. Say you have a variable called x, which represents the current index at the given array.
  5. Make x = 0 ( ignoring xth index) and x = 1( selecting xth index) and make recursive
 

Output:

{Empty} {3}  {2}  {23}  {1}  {13}  {12}  {123}