Sum of K-Digit Numbers
You are given a range of digits [l, r] and a length k. You must form every possible k-digit number using ONLY the digits from l to r (Leading zeros are perfectly fine) .
Find the total sum of all these generated numbers. Your task is to sum all possible combinations of these digits of length k.
Because the sum can be very large, return it modulo 109 + 7.
Example 1:
Input: l = 2, r = 3, k = 2
Output: 110
Explanation: To determine the valid numbers, we look at all the possible combinations of exactly 2 digits where each individual digit is strictly chosen from the inclusive range between 2 and 3. This gives us the set of numbers 22, 23, 32, and 33. When we calculate the required total of these four generated integers, we add 22, 23, 32, and 33 together to arrive at a cumulative value of 110. Since this sum is well below the problem's modulo limit of 10^9 + 7, the final answer remains exactly 110.
Example 2:
Input: l = 0, r = 2, k = 2
Output: 99
Explanation: In this scenario, we are permitted to use the digits 0, 1, and 2 to form a sequence of exactly 2 digits. Because 0 is included within the specified range, leading zeros are perfectly acceptable according to the problem constraints. By systematically generating all possible digit permutations, we obtain the numbers 00, 01, 02, 10, 11, 12, 20, 21, and 22. Evaluating these strictly as standard integer values removes the visual leading zeros, leaving us to sum 0, 1, 2, 10, 11, 12, 20, 21, and 22. Adding all of these individual values together produces an overall total sum of 99.
Now Your Turn!
Pick the correct output for the given inputInput : l = 0 , r = 1 , k = 4
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 0 <= l <= r <= 9
- 1 <= k <= 109