3791. Number of Balanced Integers in a Range Hard
1/**
2 * [3791] Number of Balanced Integers in a Range
3 *
4 * You are given two integers low and high.
5 * An integer is called balanced if it satisfies both of the following conditions:
6 *
7 * It contains at least two digits.
8 * The sum of digits at even positions is equal to the sum of digits at odd positions (the leftmost digit has position 1).
9 *
10 * Return an integer representing the number of balanced integers in the range [low, high] (both inclusive).
11 *
12 * <strong class="example">Example 1:
13 * <div class="example-block">
14 * Input: <span class="example-io">low = 1, high = 100</span>
15 * Output: <span class="example-io">9</span>
16 * Explanation:
17 * The 9 balanced numbers between 1 and 100 are 11, 22, 33, 44, 55, 66, 77, 88, and 99.
18 * </div>
19 * <strong class="example">Example 2:
20 * <div class="example-block">
21 * Input: <span class="example-io">low = 120, high = 129</span>
22 * Output: <span class="example-io">1</span>
23 * Explanation:
24 * Only 121 is balanced because the sum of digits at even and odd positions are both 2.
25 * </div>
26 * <strong class="example">Example 3:
27 * <div class="example-block">
28 * Input: <span class="example-io">low = 1234, high = 1234</span>
29 * Output: <span class="example-io">0</span>
30 * Explanation:
31 * 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).
32 * </div>
33 *
34 * Constraints:
35 *
36 * 1 <= low <= high <= 10^15
37 *
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/number-of-balanced-integers-in-a-range/
42// discuss: https://leetcode.com/problems/number-of-balanced-integers-in-a-range/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47 pub fn count_balanced(low: i64, high: i64) -> i64 {
48
49 }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn test_3791() {
60 }
61}
62Back
© 2026 bowen.ge All Rights Reserved.