Problem Description
Given a range [low, high], count how many integers in this range are symmetric. An integer is symmetric if it has an even number of digits (2 * n) and the sum of its first n digits equals the sum of its last n digits. Numbers with an odd number of digits are automatically not symmetric.
Key Insights
- Only integers with an even number of digits can be symmetric.
- Convert each integer to a string to easily split it into two halves.
- Calculate the sum of the first half and the second half digits.
- Count the integer only if both sums are equal.
- With high up to 10^4, iterating over the range is computationally efficient.
Space and Time Complexity
Time Complexity: O(n * d) where n is the number of integers in [low, high] and d is the number of digits per integer (constant given the constraints).
Space Complexity: O(1)
Solution
We solve the problem by iterating over each number in the given range. For every integer, we check if it has an even number of digits. If it does, the number is converted to a string and split into two halves. We calculate the sum of the digits in the first half and the second half; if they are equal, we increment our count. This approach uses simple string manipulation, arithmetic, and a loop iteration which are efficient given the constraints.