You are given two positive integers, l and r. A positive integer is called beautiful if the product of its digits is divisible by the sum of its digits.
Return the count of beautiful numbers between l and r, inclusive.
Example 1:
Input: l = 10, r = 20
Output: 2
Explanation:
The beautiful numbers in the range are 10 and 20.
Example 2:
Input: l = 1, r = 15
Output: 10
Explanation:
The beautiful numbers in the range are 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10.
Constraints:
1 <= l <= r < 109Problem Overview: You need to count how many integers within a given range satisfy a specific set of digit-based conditions that define a beautiful number. The constraints depend on properties of the digits themselves, so checking each number independently quickly becomes too slow for large ranges.
Approach 1: Brute Force Enumeration (O(n * d) time, O(1) space)
The most direct idea is to iterate through every number in the range and check whether it satisfies the beauty condition. For each number, extract its digits and compute the required properties (such as digit sums, divisibility checks, or other digit relationships). This approach is easy to implement and helps validate the rules of the problem. However, when the range grows to large values (for example up to 1018), iterating through every candidate becomes infeasible. Time complexity is O(n * d), where d is the number of digits.
Approach 2: Digit Dynamic Programming (Digit DP) (O(d * state * 10) time, O(d * state) space)
The efficient solution treats the number as a sequence of digits and builds it from left to right using dynamic programming. Instead of iterating through every number, you count valid constructions digit by digit. The DP state typically includes the current digit index, whether the prefix is still tight with the upper bound, and additional values needed to verify the beauty condition (for example accumulated digit metrics or remainders). At each position, you try digits 0–9, update the state, and recursively continue.
Memoization stores results for states where the prefix is no longer restricted, which dramatically reduces repeated work. This converts an exponential search over numbers into a manageable state exploration proportional to the number of digits. Digit DP is a common pattern in range-counting problems that involve digit rules and is closely related to techniques in dynamic programming and combinatorial state transitions.
Recommended for interviews: Interviewers expect the Digit DP approach. Brute force demonstrates you understand the definition of a beautiful number, but it fails under large constraints. Digit DP shows you can model number construction as a state machine and cache overlapping subproblems—an essential technique for advanced dynamic programming problems involving digit constraints.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(n * d) | O(1) | Small ranges where direct validation of each number is feasible |
| Digit Dynamic Programming | O(d * state * 10) | O(d * state) | Large numeric ranges with digit-based constraints |
Count Beautiful Numbers (Leetcode Weekly 441) • Soumya Bhattacharjee • 499 views views
Watch 2 more video solutions →Practice Count Beautiful Numbers with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor