Skip to main content

Number of Balanced Integers in a Range - Solution & Explanation

HardDynamic Programming13 min read
Practice this problem

Problem Statement

You are given two integers low and high.

An integer is called balanced if it satisfies both of the following conditions:

  • It contains at least two digits.
  • The sum of digits at even positions is equal to the sum of digits at odd positions (the leftmost digit has position 1).

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 <= 1015

Approach Overview

Problem 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.

Solution

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:

  • If 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.
  • Calculate the upper bound up for the current digit. If constrained, it equals the current digit of the number; otherwise, it is 9.
  • Iterate through all possible digits 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.
  • Return the accumulated result.

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.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force EnumerationO(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

Video Solution

Leetcode 3791 | Number of Balanced Integers in a Range | Complete Explanation | Beginner Friendly • CodeWithMeGuys • 583 views views

Watch 4 more video solutions →

Frequently Asked Questions

Is Number of Balanced Integers in a Range easy or hard?
Number of Balanced Integers in a Range is categorized as a Hard problem. The difficulty comes from designing correct Digit DP states and handling constraints like digit position, prefix tightness, and balanced sum tracking.
Number of Balanced Integers in a Range Python/Java solution
Implement a Digit DP function that counts valid numbers up to a bound. The function processes digits from left to right, tracks the sum difference between halves, and respects the tight bound condition. The same logic translates cleanly to Python, Java, C++, Go, and TypeScript.
How to solve Number of Balanced Integers in a Range in O(d * states)?
Use Digit DP to count valid numbers up to a limit. Track the current digit position, tight constraint with the bound, and the difference between the first-half and second-half digit sums. Evaluate counts for high and subtract counts for low - 1 to get the final answer.
What is the best approach for Number of Balanced Integers in a Range?
Digit Dynamic Programming (Digit DP) is the most efficient approach. Instead of iterating through every number in the range, the algorithm builds numbers digit by digit while tracking the difference between the first-half and second-half digit sums. Memoization avoids recomputation of identical states, reducing the search space dramatically.
Is Number of Balanced Integers in a Range asked at Google/Amazon/Meta?
Digit DP counting problems similar to this frequently appear in interviews at companies like Google, Amazon, and Meta. Interviewers use them to test dynamic programming skills, state design, and the ability to optimize brute-force enumeration.
What data structure is used in Number of Balanced Integers in a Range?
The solution relies on dynamic programming with memoization, typically implemented using arrays or hash maps for DP states. Recursion or iterative DP is used to traverse digits while caching computed states.
What is the time complexity of Number of Balanced Integers in a Range?
The optimal Digit DP solution runs in roughly O(d * sum * states) time where d is the number of digits in the bound and sum represents possible digit sums. The brute force method takes O(N * d) time where N is the size of the range, which becomes impractical for large inputs.

Ready to solve this problem?

Practice Number of Balanced Integers in a Range with our built-in code editor and test cases.

Practice on FleetCode