Watch 7 video solutions for Number of Beautiful Integers in the Range, a hard level problem involving Math, Dynamic Programming. This walkthrough by codingMohan has 1,961 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given positive integers low, high, and k.
A number is beautiful if it meets both of the following conditions:
k.Return the number of beautiful integers in the range [low, high].
Example 1:
Input: low = 10, high = 20, k = 3 Output: 2 Explanation: There are 2 beautiful integers in the given range: [12,18]. - 12 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3. - 18 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3. Additionally we can see that: - 16 is not beautiful because it is not divisible by k = 3. - 15 is not beautiful because it does not contain equal counts even and odd digits. It can be shown that there are only 2 beautiful integers in the given range.
Example 2:
Input: low = 1, high = 10, k = 1 Output: 1 Explanation: There is 1 beautiful integer in the given range: [10]. - 10 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 1. It can be shown that there is only 1 beautiful integer in the given range.
Example 3:
Input: low = 5, high = 5, k = 2 Output: 0 Explanation: There are 0 beautiful integers in the given range. - 5 is not beautiful because it is not divisible by k = 2 and it does not contain equal even and odd digits.
Constraints:
0 < low <= high <= 1090 < k <= 20Problem Overview: Given two integers low and high and an integer k, count how many numbers in the range satisfy two conditions: the number of even digits equals the number of odd digits, and the number is divisible by k. The challenge is that the range can be large, so checking every number directly is inefficient.
Approach 1: Brute Force Enumeration (Time: O(N * D), Space: O(1))
Iterate through every integer from low to high. For each number, extract digits and count how many are even and how many are odd. If the counts match, compute num % k to check divisibility. Digit processing takes O(D) where D is the number of digits, so the full scan costs O(N * D) where N = high - low + 1. This approach is easy to implement but becomes impractical when the range spans millions or billions of numbers.
Approach 2: Digit Dynamic Programming (Digit DP) (Time: O(D^2 * K), Space: O(D * K * D))
Instead of checking every number, build valid numbers digit by digit using dynamic programming. Convert the upper bound into a digit array and recursively construct numbers while tracking state: current digit index, difference between even and odd counts, current remainder modulo k, and whether the prefix is still constrained by the limit (tight flag). This technique is known as Digit DP and is common in counting problems involving digit constraints.
At each position, try placing digits 0–9. Update the even/odd balance and compute the new remainder using (prev_remainder * 10 + digit) % k. Memoize states that are no longer tight so repeated subproblems are computed once. Finally compute count(high) - count(low - 1) to get the result for the range. The number of states is bounded by digit length, remainder modulo k, and the even/odd balance, giving roughly O(D^2 * K) complexity.
This approach relies on ideas from math (modular arithmetic) and dynamic programming, especially the digit-state technique used in many range counting problems.
Recommended for interviews: Start by explaining the brute force method to show you understand the constraints and validation logic. Then move to Digit DP, which avoids scanning the entire range and counts valid numbers using state transitions. Interviewers expect the Digit DP optimization for large ranges because it reduces the search space dramatically while handling multiple digit constraints.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(N * D) | O(1) | Small ranges where high - low is limited and performance is not critical |
| Digit Dynamic Programming (Digit DP) | O(D^2 * K) | O(D * K * D) | Large numeric ranges with digit constraints and divisibility checks |