Ctrl K

Distinct Digits to a Sum

Medium
Pick exactly count **distinct** digits from 1 to 9 whose sum is total. Each digit may be used at most once, and zero is not allowed. Return every such set of digits. Sets that differ only in the order of their digits are the same and must appear once. The sets, and the digits inside each set, may be listed in any order. If there are no valid sets, return an empty list.
Example 1
count=2, total=5 → [[1, 4], [2, 3]]

5 = 1 + 4 = 2 + 3. The split 0 + 5 does not count, since 0 is not allowed.

Example 2
count=3, total=23 → [[6, 8, 9]]

The three largest digits give 7 + 8 + 9 = 24; only 6 + 8 + 9 makes 23.

Example 3
count=3, total=5 → []

Even the three smallest digits give 1 + 2 + 3 = 6 > 5, so there are no sets.

Constraints
  • 2 ≤ count ≤ 9
  • 1 ≤ total ≤ 60
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWeight Sets for an Exact Load MediumCard Hands for an Exact Total MediumAll Orderings MediumDistinct Orderings With Repeats MediumEvery Topping Combo Medium
loading Python…
Input
count=2, total=5
Expected
[[1, 4], [2, 3]]

Run checks the first 3 tests in your browser. Submit checks all 60 tests on the server, hidden ones included.