You are given two integers low and high.
An integer is called balanced if it satisfies both of the following conditions:
Return an integer representing the number of balanced integers in the range [low, high] (both inclusive).
Example 1:
Input: low = 1, high = 100
Output: 9
Explanation:
The 9 balanced numbers between 1 and 100 are 11, 22, 33, 44, 55, 66, 77, 88, and 99.
Example 2:
Input: low = 120, high = 129
Output: 1
Explanation:
Only 121 is balanced because the sum of digits at even and odd positions are both 2.
Example 3:
Input: low = 1234, high = 1234
Output: 0
Explanation:
1234 is not balanced because the sum of digits at odd positions (1 + 3 = 4) does not equal the sum at even positions (2 + 4 = 6).
Constraints:
1 <= low <= high <= 1015Problem Overview: Given two integers low and high, count how many numbers in this range are balanced. A balanced integer typically has an even number of digits where the sum of the first half equals the sum of the second half. Brute forcing the range quickly becomes infeasible when the bounds grow large.
Approach 1: Brute Force Enumeration (O(N * d) time, O(1) space)
The most direct method iterates through every number from low to high. Convert each number to digits, check whether the digit count is even, then compute the sum of the first half and the second half. If the sums match, increment the result. Each check costs O(d) where d is the number of digits, so the total runtime becomes O(N * d) where N = high - low. This approach is simple and useful for validating logic on small ranges, but it fails for large constraints because the range can reach billions.
Approach 2: Digit Dynamic Programming (Digit DP) (O(d * sum * states) time, O(d * sum * states) space)
The efficient solution uses Digit DP, a technique built on dynamic programming for counting numbers with digit constraints. Instead of iterating through every integer, construct numbers digit by digit while tracking the difference between the first-half digit sum and the second-half digit sum. The DP state typically includes the current digit index, whether the current prefix is tight with the upper bound, the current sum difference, and whether the number has started.
When the position is in the first half of the digits, add the digit value to the running sum difference. When in the second half, subtract it. A number is valid if the final difference equals zero and the length is even. Compute the count of valid numbers up to high and subtract the count up to low - 1. This transforms a huge search space into a manageable state space bounded by digit count and possible sums.
Digit DP avoids enumerating each number individually. Instead, it reuses previously computed states through memoization, dramatically reducing work. This technique frequently appears in advanced counting problems involving ranges and digit constraints.
Recommended for interviews: Start by explaining the brute force idea to show you understand the definition of a balanced number. Then transition to Digit DP, which is the expected optimal solution. Interviewers look for recognition that iterating over the entire range is too slow and that digit-by-digit counting with memoized states solves the problem efficiently.
First, if high < 11, there are no balanced integers in the range, so we directly return 0. Otherwise, we update low to max(low, 11).
Then we design a function dfs(pos, diff, lim), which represents processing the pos-th digit of the number, where diff is the difference between the sum of digits at odd positions and the sum of digits at even positions, and lim indicates whether the current digit is constrained by the upper bound. The function returns the number of balanced integers that can be constructed from the current state.
The execution logic of the function is as follows:
pos exceeds the length of the number, it means all digits have been processed. If diff = 0, the current number is a balanced integer, return 1; otherwise, return 0.up for the current digit. If constrained, it equals the current digit of the number; otherwise, it is 9.i for the current position. For each digit i, recursively call dfs(pos + 1, diff + i times (1 if pos \% 2 == 0 else -1), lim \&\& i == up), and accumulate the results.We first calculate the number of balanced integers a in the range [1, low - 1], then calculate the number of balanced integers b in the range [1, high], and finally return b - a.
To avoid redundant calculations, we use memoization to store previously computed states.
The time complexity is O(log^2 M times D^2), and the space complexity is O(log^2 M times D). Here, M is the value of high, and D = 10.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(N * d) | O(1) | Small ranges or quick correctness checks |
| Digit Dynamic Programming (Digit DP) | O(d * sum * states) | O(d * sum * states) | Large ranges where direct enumeration is infeasible |
Leetcode 3791 | Number of Balanced Integers in a Range | Complete Explanation | Beginner Friendly • CodeWithMeGuys • 553 views views
Watch 3 more video solutions →Practice Number of Balanced Integers in a Range with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor