Interview Questions

Print all subsets (power set) of a given set

The input for this problem will be an array of numbers representing a set, which only contains unique numbers, and your goal is to print every possible set combination, otherwise known as the power set. For example:
input set = [1, 2, 3]
power set = [[], [1], [2], [3], [1, 2], [2, 3], [1, 3] [1, 2, 3]]
The power set contains every possible combination of numbers. It also includes the empty set which contains no numbers from the original set.
## Algorithm

There will be 2^{N} possible combinations of a set of length N because every element can either be in the set or not, which gives us 2 possibilities, and we do this for N numbers, giving us 2 * 2 * 2 ... = 2^{N}.

You need to be a premium member to see the rest of this question and code.

With our large collection of challengs, tutorials, and solutions, we make it easy for you to become a better coder, prepare for interviews, and learn new skills from more experienced coders.

- 200+ Coding Challenges
- Mock Interview Questions
- 500,000+ Code Solutions
- Algorithm Tutorials
- Interview Prep Courses

mrdaniel
published this on 11/12/15 **|**

0

So in order to solve this one efficiently, do you recommend to use anything else ?
For example Dynamic Programming ?

ntt2k

commented on 12/20/16

Login to submit a comment