var a = [1,3,6,10,-1];
function combinations(array, n) {
}
combinations(a, 9) // should return...
[[1], [3], [6], [-1], [1,3], [1,6], [1,-1], [3,6], [3,-1], [6, -1], [10, -1], [1,3,-1], [3,6,-1], [1,6,-1], [1,3,6,-1]]
maybe i'm missing some correct answers but you get the idea. Really dying to know how to solve this!
Try this:
This logged:
Brute force O(N*2N) solution, where
N = a.length < 31
.This uses the index
i
as a bit field tofilter
the elements ofa
in each iteration into a sublist.edit: giving credit where due.. borrowed the bulk of this logic from this answer
output