Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
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].
Example 1:
Input: low = 1, high = 100 Output: 9 Explanation: There are 9 symmetric integers between 1 and 100: 11, 22, 33, 44, 55, 66, 77, 88, and 99.
Example 2:
Input: low = 1200, high = 1230 Output: 4 Explanation: There are 4 symmetric integers between 1200 and 1230: 1203, 1212, 1221, and 1230.
Constraints:
1 <= low <= high <= 104Problem summary: 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].
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
1 100
1200 1230
palindrome-number)sum-of-digits-in-base-k)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2843: Count Symmetric Integers
class Solution {
public int countSymmetricIntegers(int low, int high) {
int ans = 0;
for (int x = low; x <= high; ++x) {
ans += f(x);
}
return ans;
}
private int f(int x) {
String s = "" + x;
int n = s.length();
if (n % 2 == 1) {
return 0;
}
int a = 0, b = 0;
for (int i = 0; i < n / 2; ++i) {
a += s.charAt(i) - '0';
}
for (int i = n / 2; i < n; ++i) {
b += s.charAt(i) - '0';
}
return a == b ? 1 : 0;
}
}
// Accepted solution for LeetCode #2843: Count Symmetric Integers
func countSymmetricIntegers(low int, high int) (ans int) {
f := func(x int) int {
s := strconv.Itoa(x)
n := len(s)
if n&1 == 1 {
return 0
}
a, b := 0, 0
for i := 0; i < n/2; i++ {
a += int(s[i] - '0')
b += int(s[n/2+i] - '0')
}
if a == b {
return 1
}
return 0
}
for x := low; x <= high; x++ {
ans += f(x)
}
return
}
# Accepted solution for LeetCode #2843: Count Symmetric Integers
class Solution:
def countSymmetricIntegers(self, low: int, high: int) -> int:
def f(x: int) -> bool:
s = str(x)
if len(s) & 1:
return False
n = len(s) // 2
return sum(map(int, s[:n])) == sum(map(int, s[n:]))
return sum(f(x) for x in range(low, high + 1))
// Accepted solution for LeetCode #2843: Count Symmetric Integers
impl Solution {
pub fn count_symmetric_integers(low: i32, high: i32) -> i32 {
let mut ans = 0;
for x in low..=high {
ans += Self::f(x);
}
ans
}
fn f(x: i32) -> i32 {
let s = x.to_string();
let n = s.len();
if n % 2 == 1 {
return 0;
}
let bytes = s.as_bytes();
let mut a = 0;
let mut b = 0;
for i in 0..n / 2 {
a += (bytes[i] - b'0') as i32;
}
for i in n / 2..n {
b += (bytes[i] - b'0') as i32;
}
if a == b {
1
} else {
0
}
}
}
// Accepted solution for LeetCode #2843: Count Symmetric Integers
function countSymmetricIntegers(low: number, high: number): number {
let ans = 0;
const f = (x: number): number => {
const s = x.toString();
const n = s.length;
if (n & 1) {
return 0;
}
let a = 0;
let b = 0;
for (let i = 0; i < n >> 1; ++i) {
a += Number(s[i]);
b += Number(s[(n >> 1) + i]);
}
return a === b ? 1 : 0;
};
for (let x = low; x <= high; ++x) {
ans += f(x);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.