Count of Integers - Problem
Count of Integers is a fascinating digit dynamic programming problem that challenges you to count numbers within a range based on their digit sum constraints.

You're given two numeric strings num1 and num2 representing the lower and upper bounds of a range, along with two integers min_sum and max_sum that define the valid digit sum range.

Your task is to count how many integers x satisfy both conditions:
num1 ≤ x ≤ num2 (x is within the numeric range)
min_sum ≤ digit_sum(x) ≤ max_sum (x's digit sum is within bounds)

Since the answer can be extremely large, return it modulo 109 + 7.

Example: If num1 = "1", num2 = "12", min_sum = 1, max_sum = 8, then numbers like 1 (digit sum: 1), 10 (digit sum: 1), 11 (digit sum: 2), 12 (digit sum: 3) are all valid, but we exclude any numbers whose digit sum exceeds 8.

Input & Output

example_1.py — Basic Range
$ Input: num1 = "1", num2 = "12", min_sum = 1, max_sum = 8
Output: 11
💡 Note: Numbers 1,2,3,4,5,6,7,8,10,11,12 have digit sums [1,2,3,4,5,6,7,8,1,2,3] respectively. All are within [1,8] range, so count = 11.
example_2.py — Tight Constraints
$ Input: num1 = "1", num2 = "5", min_sum = 1, max_sum = 5
Output: 5
💡 Note: Numbers 1,2,3,4,5 have digit sums [1,2,3,4,5]. All satisfy the constraints 1 ≤ digit_sum ≤ 5.
example_3.py — No Valid Numbers
$ Input: num1 = "1", num2 = "9", min_sum = 15, max_sum = 20
Output: 0
💡 Note: Single digit numbers (1-9) have digit sums 1-9, none of which fall in range [15,20], so count = 0.

Visualization

Tap to expand
Smart Digital Counter: Digit DP VisualizationCounter Range[num1, num2]1stdigit2nddigit3rddigit...moresum≤maxsum>maxvalidcheckprunedMemoization CacheState: (position=1, sum=5, tight=true, started=true) → Count: 847State: (position=2, sum=12, tight=false, started=true) → Count: 234State: (position=3, sum=8, tight=true, started=true) → Count: 156✓ Avoid recalculating same states✓ O(digits × max_sum × 4) total states✓ Polynomial time complexityAlgorithm PerformanceBrute Force: O(num2 - num1) - up to 10²² iterations!Digit DP: O(digits × max_sum × 4) - typically < 10⁴ statesResult: Exponential problem solved in polynomial time!
Understanding the Visualization
1
Set Boundaries
Define the counting range [num1, num2] and digit sum constraints [min_sum, max_sum]
2
Build Digit Tree
Construct numbers digit by digit, exploring only valid branches
3
Track Constraints
Monitor tight bounds and current digit sum at each level
4
Prune Invalid Paths
Skip branches that violate digit sum or range constraints
5
Memoize States
Cache results for repeated subproblems to avoid redundant calculations
Key Takeaway
🎯 Key Insight: Digit DP transforms an impossible brute force problem (up to 10²² iterations) into a tractable polynomial solution by intelligently exploring only the valid portion of the solution space using memoization.

Time & Space Complexity

Time Complexity
⏱️
O(n × sum_range × 2 × 2)

n is the number of digits, sum_range is max_sum - min_sum, and we have 2 boolean states for tight constraints

n
2n
Linear Growth
Space Complexity
O(n × sum_range × 2 × 2)

Memoization table stores all possible DP states

n
2n
Linearithmic Space

Constraints

  • 1 ≤ num1 ≤ num2 < 1022
  • 1 ≤ min_sum ≤ max_sum ≤ 400
  • num1 and num2 consist of digits only and don't have leading zeros
  • The range can contain up to 1022 numbers, making brute force impossible
Asked in
Google 12 Amazon 8 Microsoft 6 Meta 4
31.2K Views
Medium Frequency
~35 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen