Count Symmetric Integers (LeetCode 2843): Solution and Explanation
Given low and high, count the integers in [low, high] that have an even number of digits and whose first half digit sum equals the second half digit sum.
Intuition
Think of each number as a balance scale. The first half of its digits sits on the left pan and the second half on the right pan. The number is symmetric when the scale is level.
A number with an odd digit count has a middle digit with nowhere to go, so it can never balance. That rules out every 1, 3 and 5 digit number at once.
With high at most 10^4 there are at most ten thousand numbers to weigh, and each has at most four digits, so checking every one directly is cheap.
Approach: Enumerate and compare half sums
- Set
count = 0. - For each
xfromlowtohigh, turn it into the stringsand letn = len(s). - If
nis odd, skipx. - Add up the digits of
s[:n/2]and ofs[n/2:]. - If the two sums are equal, add one to
count. - Return
count.
Step-by-step example
low = 1, high = 100
- 1 to 9 have one digit, an odd count, so all nine are skipped.
- 10 splits into 1 | 0. The sums are 1 and 0, so it is not symmetric.
- 11 splits into 1 | 1. Both sums are 1, so count becomes 1.
- Continuing through the two digit numbers, only the ones with equal digits balance: 11, 22, ..., 99, for a count of 9.
- 100 has three digits and is skipped. The answer is 9.
Code: Python, Java, C++
1class Solution:
2 def countSymmetricIntegers(self, low: int, high: int) -> int:
3 count = 0
4 for x in range(low, high + 1):
5 s = str(x)
6 n = len(s)
7 if n % 2 == 1:
8 continue
9 half = n // 2
10 left = sum(int(c) for c in s[:half])
11 right = sum(int(c) for c in s[half:])
12 if left == right:
13 count += 1
14 return count1class Solution {
2 public int countSymmetricIntegers(int low, int high) {
3 int count = 0;
4 for (int x = low; x <= high; x++) {
5 String s = Integer.toString(x);
6 int n = s.length();
7 if (n % 2 == 1) {
8 continue;
9 }
10 int left = 0, right = 0;
11 for (int i = 0; i < n / 2; i++) {
12 left += s.charAt(i) - '0';
13 right += s.charAt(n / 2 + i) - '0';
14 }
15 if (left == right) {
16 count++;
17 }
18 }
19 return count;
20 }
21}1class Solution {
2public:
3 int countSymmetricIntegers(int low, int high) {
4 int count = 0;
5 for (int x = low; x <= high; x++) {
6 string s = to_string(x);
7 int n = s.size();
8 if (n % 2 == 1) {
9 continue;
10 }
11 int left = 0, right = 0;
12 for (int i = 0; i < n / 2; i++) {
13 left += s[i] - '0';
14 right += s[n / 2 + i] - '0';
15 }
16 if (left == right) {
17 count++;
18 }
19 }
20 return count;
21 }
22};Time and space complexity
O((high - low + 1) * d)
Every number in the range is visited once, and each check touches its d digits, where d is at most 5.
O(d)
Only the string form of the current number is kept.
Other approaches
Arithmetic digit split (time O(high - low + 1), space O(1))
Skip the string: for two digit numbers check x % 11 == 0, and for four digit numbers compare x / 1000 + x / 100 % 10 with x / 10 % 10 + x % 10. Same complexity, fewer allocations.
Precompute a prefix count (time O(10^4) once, O(1) per query, space O(10^4))
Mark every symmetric number up to 10^4 once and build a prefix sum. Each query is then prefix[high] - prefix[low - 1], which pays off when many ranges are asked.
Common mistakes
- Counting odd-length numbers by putting the middle digit on one side. They are never symmetric.
- Splitting at the wrong index for four digit numbers. Use
n / 2, not a fixed position. - Stopping the loop at
high - 1. The range is inclusive on both ends.
Frequently asked questions
Why are odd-length numbers never symmetric?
The definition only applies to numbers with 2 * n digits. A number with an odd digit count cannot be cut into two equal halves, so it is excluded.
Which two digit numbers are symmetric?
Exactly the ones whose two digits are equal: 11, 22, up to 99. Those are the multiples of 11 below 100.
Is brute force acceptable here?
Yes. The range holds at most 10^4 numbers, so the direct check is both the simplest and a fully accepted solution.