Count Numbers with Non-Decreasing Digits - Problem

You're given two integers l and r represented as strings, and an integer b (the base). Your task is to count how many integers in the inclusive range [l, r] have non-decreasing digits when represented in base b.

A number has non-decreasing digits if, when reading from left to right (most significant to least significant digit), each digit is greater than or equal to the previous digit.

Example: In base 10, the number 1234 has non-decreasing digits (1 ≤ 2 ≤ 3 ≤ 4), but 4321 does not (4 > 3 > 2 > 1).

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

This problem combines number theory, dynamic programming, and digit manipulation - making it a favorite in technical interviews!

Input & Output

example_1.py — Basic Range in Base 10
$ Input: l = "1", r = "9", b = 10
Output: 9
💡 Note: All single digits (1,2,3,4,5,6,7,8,9) have non-decreasing digits trivially, so the answer is 9.
example_2.py — Two-digit Range
$ Input: l = "10", r = "15", b = 10
Output: 3
💡 Note: Numbers 11, 12, 13 have non-decreasing digits. Numbers 10, 14, 15 do not (1>0, 1<4 but then 4>5, 1<5 but we need to check: 10→digits[1,0]→1>0❌, 14→[1,4]→1≤4✅, 15→[1,5]→1≤5✅). So valid numbers are 11,12,13,14,15 = 5. Wait, let me recalculate: 10→[1,0]→invalid, 11→[1,1]→valid, 12→[1,2]→valid, 13→[1,3]→valid, 14→[1,4]→valid, 15→[1,5]→valid. Answer is 5.
example_3.py — Different Base
$ Input: l = "1", r = "10", b = 2
Output: 3
💡 Note: In base 2, we check numbers 1-10 (decimal). 1→[1]✅, 2→[1,0]❌, 3→[1,1]✅, 4→[1,0,0]❌, 5→[1,0,1]❌, 6→[1,1,0]❌, 7→[1,1,1]✅, 8→[1,0,0,0]❌, 9→[1,0,0,1]❌, 10→[1,0,1,0]❌. Valid: 1,3,7. Answer is 3.

Constraints

  • 1 ≤ l ≤ r ≤ 1015
  • 2 ≤ b ≤ 36
  • l and r are given as decimal strings
  • Answer must be returned modulo 109 + 7

Visualization

Tap to expand
Digit DP: Building Non-Decreasing NumbersRange: [123, 789] in base 10Position:01st digit12nd digit23rd digitValid Choices:1-7Range: 1≤d≤7≥prevNon-decreasing≥prevNon-decreasingExample: Building 2342342≥0✓3≥2✓4≥3✓Valid number found!🎯 Count all valid paths instead of enumerating numbersComplexity: O(d² × b) where d = digits, b = base
Understanding the Visualization
1
Initialize DP State
Start with position 0, no previous digit constraint, and tight bounds for both l and r
2
Choose Valid Digits
At each position, try all digits that satisfy: digit ≥ previous_digit and within bounds
3
Update Constraints
Update tight bounds and previous digit for the next recursive call
4
Count Valid Paths
Sum up all paths that lead to valid numbers within the range
Key Takeaway
🎯 Key Insight: Use Dynamic Programming to mathematically count valid digit patterns instead of checking every single number in the range - this transforms an O(r-l) brute force into an O(d² × b) elegant solution!
Asked in
Google 42 Amazon 35 Microsoft 28 Meta 22
47.7K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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