You are given two integers l and r.
An integer is called good if its digits form a strictly monotone sequence, meaning the digits are strictly increasing or strictly decreasing. All single-digit integers are considered good.
An integer is called fancy if it is good, or if the sum of its digits is good.
Return an integer representing the number of fancy integers in the range [l, r] (inclusive).
A sequence is said to be strictly increasing if each element is strictly greater than its previous one (if exists).
A sequence is said to be strictly decreasing if each element is strictly less than its previous one (if exists).
Example 1:
Input: l = 8, r = 10
Output: 3
Explanation:
[1, 0], which form a strictly decreasing sequence, so 10 is good and thus fancy.Therefore, the answer is 3.
Example 2:
Input: l = 12340, r = 12341
Output: 1
Explanation:
[1, 2, 3, 4, 0] is not strictly monotone.1 + 2 + 3 + 4 + 0 = 10.[1, 0], which is strictly decreasing. Therefore, 12340 is fancy.[1, 2, 3, 4, 1] is not strictly monotone.1 + 2 + 3 + 4 + 1 = 11.[1, 1], which is not strictly monotone. Therefore, 12341 is not fancy.Therefore, the answer is 1.
Example 3:
Input: l = 123456788, r = 123456788
Output: 0
Explanation:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 8 = 44.[4, 4], which is not strictly monotone. Therefore, 123456788 is not fancy.Therefore, the answer is 0.
Constraints:
1 <= l <= r <= 1015Problem Overview: Given two integers representing a range [low, high], count how many numbers inside the range satisfy the definition of a fancy number. The constraint is defined by rules on the digits of the number, which makes brute force checking impractical for large ranges.
Approach 1: Brute Force Enumeration (O(n * d) time, O(1) space)
The most direct method iterates through every number from low to high. For each value, extract its digits and verify whether it satisfies the fancy number conditions. This typically involves repeated division/modulo operations to inspect digits and apply the rule. The approach is easy to implement but quickly becomes infeasible when the range spans millions or billions of numbers.
Approach 2: Digit Dynamic Programming (O(d * states * 10) time, O(d * states) space)
A much more scalable method uses Digit DP. Instead of checking each number individually, treat the number as a sequence of digits and build valid numbers digit by digit. Convert the upper bound into a digit array and recursively construct numbers while tracking states such as current position, whether the prefix is still constrained by the bound (tight flag), and other rule-specific states related to the fancy condition. Memoization stores results for repeated states, avoiding recomputation.
To count numbers in a range, compute count(high) and subtract count(low - 1). Each DP state explores digits 0–9 and transitions based on the rule that defines a fancy number. Because the number of digits d is small (usually ≤ 19 for 64-bit integers), the state space remains manageable.
This pattern appears frequently in problems involving digit constraints such as limiting digit frequency, preventing adjacent matches, or enforcing divisibility properties. If you want to get comfortable with this technique, review problems under dynamic programming and math. Digit-based counting problems often combine both concepts.
Recommended for interviews: Interviewers expect the Digit DP approach. Starting with brute force shows you understand the property being checked, but recognizing that the range can be huge and switching to a digit-level dynamic programming solution demonstrates stronger algorithmic thinking.
We first define a function check(s) to determine whether an integer s is a good number. For s < 100, we only need to check whether s is a multiple of 11; if so, s is not a good number. For s geq 100, we need to check whether the digits of s form a strictly monotonic sequence, i.e., strictly increasing or strictly decreasing. Since the range of digit sums is small, when the digit sum exceeds 100, we only need to check the relationship between the tens digit and the units digit of the digit sum.
Next, we use digit DP to count the number of fancy numbers in the interval [l, r]. We define a recursive function dfs(pos, s, prev, st, lim), where:
pos represents the current digit position being processed, from high to low.s represents the current digit sum.prev represents the value of the previous digit.st represents the state of the current digit sequence, where state 0 means there is at most one digit so far, state 1 means strictly increasing, state 2 means strictly decreasing, and state 3 means not strictly monotonic.lim indicates whether the current digit is constrained by the upper bound.In the recursive function, if pos exceeds the length of the number, we have finished processing a number. If the state st is not equal to 3, the number is a good number and we return 1; otherwise, we call check(s) to determine whether the digit sum is a good number, returning 1 if it is, and 0 otherwise.
During the recursion, we enumerate the range of the current digit: if lim is true, the range is [0, num[pos]]; otherwise, it is [0, 9]. For each value, we update the state st and recursively call dfs to process the next digit.
Finally, we compute the count of fancy numbers in [0, r] and [0, l-1] separately, and the answer is their difference.
The time complexity is O(D^3 times log^2 r) and the space complexity is O(D^2 times log^2 r), where D = 10.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(n * d) | O(1) | Very small ranges where direct checking is acceptable |
| Digit Dynamic Programming | O(d * states * 10) | O(d * states) | Large ranges with digit-based constraints |
Q4. Count Fancy Numbers in a Range || Digit Dynamic Programming || Leetcode Biweekly 178 || Watch2X🚀 • Rajan Keshari ( CSE - IIT Dhanbad ) • 841 views views
Watch 3 more video solutions →Practice Count Fancy Numbers in a Range with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor