Skip to main content

Count Beautiful Numbers - Solution & Explanation

HardDynamic Programming3 min readAsked at: Google
Practice this problem

Problem Statement

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 < 109

Approach Overview

Problem 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

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force EnumerationO(n * d)O(1)Small ranges where direct validation of each number is feasible
Digit Dynamic ProgrammingO(d * state * 10)O(d * state)Large numeric ranges with digit-based constraints

Video Solution

3490. Count Beautiful Numbers (Leetcode Hard) • Programming Live with Larry • 527 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is Count Beautiful Numbers easy or hard?
Count Beautiful Numbers is categorized as a Hard problem. The difficulty comes from modeling the digit constraints correctly in a DP state and implementing a memoized digit DP that handles large numeric ranges efficiently.
Count Beautiful Numbers Python/Java solution
Python, Java, C++, and Go implementations follow the same idea: implement a recursive digit DP with memoization. The function tracks the current digit index, tight constraint, and digit-related state values, then iterates through valid digits and accumulates counts.
How to solve Count Beautiful Numbers in O(d * state)?
Model the number construction as a digit DP problem. Process digits from most significant to least significant, maintain a state containing the current index, a tight flag for the bound, and the required digit metrics. Use memoization when the prefix is not tight to reuse previously computed states.
What is the best approach for Count Beautiful Numbers?
The optimal approach is Digit Dynamic Programming (Digit DP). It constructs numbers digit by digit while tracking constraints in a DP state such as index, tight bound, and accumulated digit properties. This avoids iterating through every number in the range and reduces the complexity to roughly O(d * state * 10).
Is Count Beautiful Numbers asked at Google/Amazon/Meta?
Digit DP problems similar to Count Beautiful Numbers appear in interviews at companies like Google, Amazon, and Meta. These companies frequently test range counting problems with digit constraints because they evaluate dynamic programming and state modeling skills.
What data structure is used in Count Beautiful Numbers?
The solution primarily uses dynamic programming with memoization, typically implemented with arrays or hash maps to cache states. Recursion or iterative DP is used to explore digit transitions while respecting the upper bound constraint.
What is the time complexity of Count Beautiful Numbers?
The optimized solution using Digit DP runs in O(d * state * 10) time, where d is the number of digits in the upper bound and state represents tracked digit properties in the DP. Space complexity is O(d * state) due to memoization of DP states.

Ready to solve this problem?

Practice Count Beautiful Numbers with our built-in code editor and test cases.

Practice on FleetCode