Count Symmetric Integers - Problem
Count Symmetric Integers
You are given two positive integers
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,
Goal: Return the count of symmetric integers in the given range.
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
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
โ Linear Growth
Space Complexity
O(d)
Space for string representation of each number with d digits
โ Linear Space
Constraints
- 1 โค low โค high โค 104
- Numbers with odd digit count are never symmetric
- Both low and high are inclusive in the range
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code