(array, idx = null)
| 19 | // runs in the same time/space complexity |
| 20 | |
| 21 | function powerset(array, idx = null) { |
| 22 | if (idx === null) { |
| 23 | idx = array.length - 1; |
| 24 | } |
| 25 | if (idx < 0) { |
| 26 | return [[]]; |
| 27 | } |
| 28 | const ele = array[idx]; |
| 29 | const subsets = powerset(array, idx - 1); |
| 30 | const length = subsets.length; |
| 31 | for (let i = 0; i < length; i++) { |
| 32 | const currentSubset = subsets[i]; |
| 33 | subsets.push(currentSubset.concat(ele)); |
| 34 | } |
| 35 | return subsets; |
| 36 | } |
| 37 | |
| 38 | const array = []; |
| 39 |