#2843
Easy Algorithms Count symmetric integers
Math Enumeration
83.1% acceptance
Feb 25, 2026
647
65
You are given two positive integers low and high.
An integer x consisting of 2 * n digits is symmetric if the sum of the first n digits of x is equal to the sum of the last n digits of x. Numbers with an odd number of digits are never symmetric.
Return the number of symmetric integers in the range [low, high].
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn count_symmetric_integers(low: i32, high: i32) -> i32 {
(low..=high).filter(|&n| {
let s = n.to_string();
let len = s.len();
if len % 2 != 0 { return false; }
let b = s.as_bytes();
let half = len / 2;
let left: i32 = b[..half].iter().map(|&c| (c - b'0') as i32).sum();
let right: i32 = b[half..].iter().map(|&c| (c - b'0') as i32).sum();
left == right
}).count() as i32
}
}