Skip to content

ORIGINAL CODING QUESTION

Locker codes from distinct digits

A campus locker code is a set of k different digits chosen from 1 to 9; the order in which the digits are written does not matter. Count the codes whose digits add up exactly to s.

Open practice workspace

Original exercise · easy. Editorial role relevance does not imply a company has asked this question.

Input and output

Two integers k s. Print the number of codes.

Constraints

1 ≤ k ≤ 9; 0 ≤ s ≤ 60.

Example 1

Input

3 7

Output

1

Only {1, 2, 4} uses three different digits totalling 7.

Example 2

Input

2 10

Output

4

The codes are {1,9}, {2,8}, {3,7} and {4,6}; {5,5} repeats a digit.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Writing the digits of each code in increasing order gives every code exactly one spelling.

Hint 2

Recurse with the next smallest digit allowed, the digits still to choose and the remaining sum.

Hint 3

Stop a branch when the remaining sum goes negative or the digits run out; count it when both the digits to choose and the remaining sum reach 0.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →