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

  • Create an binary array of the same size as the given array.
  • Now for every integer, we have two options, whether to select it or ignore it.
  • 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.
  • Say you have a variable called x, which represents the current index at the given array.
  • Make x = 0 ( ignoring xth index) and x = 1( selecting xth index) and make recursive

Code:

Output:

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