Count Symmetric Integers - Problem
Count Symmetric Integers

You are given two positive integers low and high. Your task is to count how many symmetric integers exist in the range [low, high] (inclusive).

A number is symmetric if:
โ€ข It has an even number of digits (numbers with odd digits are never symmetric)
โ€ข The sum of the first half of its digits equals the sum of the second half

For example, 1221 is symmetric because it has 4 digits, and 1+2 = 2+1. The number 123321 is also symmetric: 1+2+3 = 3+2+1 = 6.

Goal: Return the count of symmetric integers in the given range.

Input & Output

example_1.py โ€” Basic Range
$ Input: low = 1, high = 100
โ€บ Output: 9
๐Ÿ’ก Note: The symmetric integers in range [1,100] are: 11, 22, 33, 44, 55, 66, 77, 88, 99. Each has equal digit sums (1+1=2, 2+2=4, etc.)
example_2.py โ€” Larger Range
$ Input: low = 1200, high = 1230
โ€บ Output: 4
๐Ÿ’ก Note: The symmetric integers are: 1210 (1+2=2+1+0), 1221 (1+2=2+1), 1232 (1+2=3+2), but only 1221 works since 1+2=3 and 2+1=3. Actually: 1210, 1221, 1232 don't all work. Only 1221 is symmetric.
example_3.py โ€” Small Range
$ Input: low = 1, high = 9
โ€บ Output: 0
๐Ÿ’ก Note: All numbers from 1 to 9 have only 1 digit (odd), so none can be symmetric. Symmetric numbers must have even digit count.

Visualization

Tap to expand
Symmetric Number as Balance Scale12214-digit number1+2=32+1=3โš–๏ธ Balanced!12344-digit number1+2=33+4=7โšก Not Balanced!Key InsightA number is symmetric when its digit halves have equal sumsLike a perfectly balanced scale with equal weights on both sides
Understanding the Visualization
1
Check Scale Type
Only even-digit numbers can be balanced (odd-digit numbers have no center point)
2
Split the Weights
Divide digits into left half and right half
3
Weigh Both Sides
Sum the digits on left side and right side
4
Check Balance
If sums are equal, the scale is perfectly balanced (symmetric)
Key Takeaway
๐ŸŽฏ Key Insight: Converting the problem to a balance scale analogy helps visualize that we need equal 'weight' (digit sums) on both halves of even-digit numbers.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n ร— d)

Where n is the range size (high - low) and d is average number of digits per number

n
2n
โœ“ Linear Growth
Space Complexity
O(d)

Space for string representation of each number with d digits

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค low โ‰ค high โ‰ค 104
  • Numbers with odd digit count are never symmetric
  • Both low and high are inclusive in the range
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 6
23.4K Views
Medium Frequency
~12 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen