ORIGINAL CODING QUESTION
Split a digit run into bounded parts
An old attendance export glued p numbers together into one run of n decimal digits and lost the separators. Count the ways to cut the run into exactly p nonempty consecutive parts so that every part is a number from 0 to limit written without a leading zero. A part that is exactly 0 is allowed, but a part such as 05 is not.
Original exercise · medium. Editorial role relevance does not imply a company has asked this question.
Input and output
n p limit, then n digits, each from 0 to 9. Print the number of ways.
Constraints
1 ≤ n ≤ 12; 1 ≤ p ≤ n; 0 ≤ limit ≤ 1,000,000.
Example 1
Input
5 3 255 1 0 0 2 5
Output
2
Only 10 | 0 | 25 and 100 | 2 | 5 work; every other cut creates a part with a leading zero.
Example 2
Input
4 2 100 1 2 3 4
Output
1
12 | 34 works, while 1 | 234 and 123 | 4 contain a part above 100.
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
Choose the first part, then solve the same problem for the remaining digits with one fewer part.
Hint 2
A part may be longer than one digit only if its first digit is not 0, and its value must not exceed limit.
Hint 3
Stop when the digits run out: the branch counts only if exactly p parts were used.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →