Given a list of numbers, find all distinct sums that can be generated from the subsets of the given list and return them in increasing order.
Start with the given list of numbers. Create all possible unique combinations of these numbers, including single numbers, pairs, triplets, and so on, up to the entire list itself. For each of these sublists, calculate the sum of the numbers within it. Arrange the calculated sums in ascending order (from smallest to largest).
def subsum(ls): if len(ls) <= 0: return [0] l = subsum(ls[1:]) return l + [x + ls[0] for x in l] ls = [1, 2] output = subsum(ls) output.sort() print(output)
function subsum(list) { if (list.length <= 0) { return [0]; } const ls = subsum(list.slice(1)); return [...ls, ...ls.map(x => x + list[0])]; } const list = [1, 2]; console.log(subsum(list).sort((a, b) => a - b));