Count Symmetric Integers - Problem

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].

Input & Output

Example 1 — Small Range
$ Input: low = 1, high = 100
Output: 9
💡 Note: Symmetric numbers: 11 (1=1), 22 (2=2), 33 (3=3), 44 (4=4), 55 (5=5), 66 (6=6), 77 (7=7), 88 (8=8), 99 (9=9). All single digits and odd-length numbers are not symmetric.
Example 2 — Four Digit Range
$ Input: low = 1200, high = 1230
Output: 1
💡 Note: Only 1221 is symmetric in this range: left sum = 1+2 = 3, right sum = 2+1 = 3. Numbers like 1234 have left sum = 1+2 = 3, right sum = 3+4 = 7.
Example 3 — No Symmetric Numbers
$ Input: low = 1, high = 10
Output: 0
💡 Note: No symmetric numbers exist. Single digit numbers (1-9) have odd length, and 10 has left sum = 1, right sum = 0.

Constraints

  • 1 ≤ low ≤ high ≤ 109
  • Numbers with odd digits are never symmetric
  • Only even-length numbers can be symmetric

Visualization

Tap to expand
Count Symmetric Integers in Range [1, 100]12223455771≠22=2 ✓3≠45=5 ✓7=7 ✓Split each even-length number: compare left half sum vs right half sumSymmetric numbers: 11, 22, 33, 44, 55, 66, 77, 88, 99Result: Count = 9
Understanding the Visualization
1
Input Range
Given range [1, 100] to check for symmetric numbers
2
Check Symmetry
Split even-length numbers and compare digit sums
3
Count Results
Count numbers where left sum = right sum
Key Takeaway
🎯 Key Insight: Only even-length numbers can be symmetric - split in half and compare digit sums
Asked in
Google 25 Microsoft 20 Amazon 15
18.5K Views
Medium Frequency
~15 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