Skip to the code
cf.
+10 XP

#305 Distinct Digits to a Target

easy

A PIN-style puzzle asks for k different digits from 1 to 9 that add up to n.

Write digitPicks(k, n) that returns every such group of digits. Write each group sorted ascending and list the groups in sorted order (compare first digits, then second digits, and so on). Return [] if there are none.

Example 1

Input: k = 3, n = 7
Output: [[1,2,4]]

1 + 2 + 4 = 7 is the only way to add 3 different digits to 7.

Example 2

Input: k = 3, n = 9
Output: [[1,2,6],[1,3,5],[2,3,4]]

Three groups of 3 different digits add up to 9.

Constraints

  • 1 <= k <= 9
  • 1 <= n <= 60
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).