O(2^n) is the first Big O class that we've dealt with that falls into the scary exponential category of algorithms.
Algorithms that grow at an exponential rate become impossible to compute after so few iterations that they are almost worthless in practicality.
At LockedIn, we need to be able to compute the power set of a set of influencers. It has something to do with targeting segments of an audience with ads. I don't know, I just work here.
A power set consists of all possible subsets (combinations) of elements in a given collection, include an empty subset and the original set itself. For example, the power set of [1, 2, 3] is:
[
[], # empty subset
[1],
[2],
[3],
[1, 2],
[1, 3],
[2, 3],
[1, 2, 3], # original set
]
Complete the power_set function using the following algorithm. It takes a list of numbers and returns a list of lists of numbers.
Notice the growth rate of a power set is 2^n, where the number of elements in the original set is the exponent n. Each new element in the input doubles the size of the power set! Therefore, its complexity class is O(2^n).
If we could calculate one subset per millisecond, completing the power_set() of just 25 items would take approximately 9 hours, and that's not accounting for the massive amounts of memory we would need. 40 items would take over 34 years!