Skip to main content

Number of Balanced Integers in a Range - Video Solutions

HardDynamic Programming

Leetcode 3791 | Number of Balanced Integers in a Range | Complete Explanation | Beginner Friendly

CodeWithMeGuys
23:56583 views
5 video solutions available

Number of Balanced Integers in a Range - Video Solution

Watch 5 video solutions for Number of Balanced Integers in a Range, a hard level problem involving Dynamic Programming. This walkthrough by CodeWithMeGuys has 583 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

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
Read full problem with examples

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.

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