Count of Integers - Problem
You are given two numeric strings num1 and num2 and two integers max_sum and min_sum. We denote an integer x to be good if:
num1 <= x <= num2min_sum <= digit_sum(x) <= max_sum
Return the number of good integers. Since the answer may be large, return it modulo 10^9 + 7.
Note: digit_sum(x) denotes the sum of the digits of x.
Input & Output
Example 1 — Basic Range
$
Input:
num1 = "1", num2 = "12", min_sum = 1, max_sum = 8
›
Output:
11
💡 Note:
Numbers 1-12 have digit sums: 1(1), 2(2), 3(3), 4(4), 5(5), 6(6), 7(7), 8(8), 9(9), 10(1), 11(2), 12(3). All have digit sum ≤ 8, so count = 11.
Example 2 — Narrow Sum Range
$
Input:
num1 = "1", num2 = "5", min_sum = 1, max_sum = 3
›
Output:
3
💡 Note:
Numbers 1,2,3 have digit sums 1,2,3 respectively (all valid). Numbers 4,5 have digit sums 4,5 (both > 3, invalid). Count = 3.
Example 3 — Single Number
$
Input:
num1 = "15", num2 = "15", min_sum = 5, max_sum = 8
›
Output:
1
💡 Note:
Only number 15 in range. Digit sum = 1+5 = 6, which satisfies 5 ≤ 6 ≤ 8. Count = 1.
Constraints
- 1 ≤ num1 ≤ num2 < 1022
- 1 ≤ min_sum ≤ max_sum ≤ 400
Visualization
Tap to expand
Understanding the Visualization
1
Input
Range [num1, num2] and digit sum bounds [min_sum, max_sum]
2
Process
Use digit DP to count valid numbers efficiently
3
Output
Count of numbers satisfying both range and digit sum constraints
Key Takeaway
🎯 Key Insight: Use digit DP to build valid numbers systematically rather than checking every number in the range
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code