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 INPUT Range: [low, high] 1 100 2-digit numbers (symmetric?): 11 22 33 ... low = 1 high = 100 1-9: odd digits (skip) 10-99: 2 digits (check) 100: odd digits (skip) ALGORITHM STEPS 1 Check digit count Skip if odd digits 2 Split number First n and last n digits 3 Sum each half Calculate digit sums 4 Compare sums If equal, count++ Example: 33 digits = 2 (even) OK first half: 3 last half: 3 3 == 3 Symmetric! FINAL RESULT Symmetric integers found: 11 22 33 44 55 66 77 88 99 Output: 9 9 symmetric integers in range [1, 100] OK Key Insight: Only numbers with EVEN digit count can be symmetric. For 2-digit numbers (10-99), a number is symmetric when first digit equals second digit (11, 22, ... 99). Time: O(n*d) TutorialsPoint - Count Symmetric Integers | Optimal Solution
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