What is your hint or solution suggestion?
See the solution for “Coins” to see how I generated the subarrays. I then use Array.filter to remove subarrays that do not sum up to the expected value, and to remove subarrays with same values.
Solution 1
function combinations(possibleNumbers, total) {
var police = possibleNumbers.filter(x => x % 2 == 0);
var sanitation = possibleNumbers;
var fire = possibleNumbers;
var parts = [police,sanitation,fire],
result = parts.reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []));
const sums = result.filter(x => x.reduce((a, b) => a + b, 0) == total);
const sums1 = sums.filter(x => x[1] != x[2]);
return sums1;
}
Challenge: Department Numbers
Link to the challenge: