The question is in the title.
As an example:
Given this array = [1,1,2,2,3,7,9,1]
and sum = 7
, I expect this result:
1,1,2,2,1
1,1,2,3
...
7
A quadratic time answer is trivial, I am looking for a sub-quadratic-time solution...
My current sloooow solution:
function subsetSum(numbers, target, partial) {
var s, n, remaining;
partial = partial || [];
// sum partial
s = partial.reduce(function (a, b) {
return a + b;
}, 0);
// check if the partial sum is equals to target
if (s === target) {
console.log(partial.join(","))
}
if (s >= target) {
return; // if we reach the number we don't bother to continue
}
for (var i = 0; i < numbers.length; i++) {
n = numbers[i];
remaining = numbers.slice(i + 1);
subsetSum(remaining, target, partial.concat([n]));
}
}
subsetSum([1,1,2,2,3,7,9,1], 7);
Output:
1,1,2,2,1
1,1,2,3
1,1,2,3
1,2,3,1
1,2,3,1
1,2,3,1
1,2,3,1
2,2,3
7
For 2k 1-digit numbers, after 4 minutes of crunching, in my box nodejs (v11.14.0) spits out:
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
:-(