FunctionRacerProblemsRace

Subset Sums

EasyRecursionPython, JavaScript, TypeScript, Java, C++

Return every total that can be made by adding up the elements of a subset of nums. The empty subset counts and adds up to 0, the whole list counts too, and each element may be used at most once in a subset. Return the distinct totals in increasing order, each listed once.

For example, [1, 2] has the subsets [], [1], [2] and [1, 2], so the answer is [0, 1, 2, 3].

Example 1

nums [1,2]
returns [0,1,2,3]

The four subsets add up to 0, 1, 2 and 3.

Example 2

nums [2,2]
returns [0,2,4]

The subsets add up to 0, 2, 2 and 4; the repeated 2 is listed once.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Recursion problems