A parts warehouse issues serial numbers to incoming components as consecutive positive integers, always printed with no leading zeros. Quality control has a superstition: a serial number is called mirror-balanced if, written out in decimal, it has an even number of digits and the sum of the digits in its first half exactly equals the sum of the digits in its second half. A serial number with an odd number of digits (including every single-digit number) can never be mirror-balanced.
Given the range of serial numbers [low, high] issued in a shipment, count how many of them are mirror-balanced.
A single line with two space-separated integers low and high.
Print a single integer: the count of mirror-balanced integers x with low <= x <= high.
Example 1
Input
10 50
Expected
4
Explanation
Only 2-digit numbers can be mirror-balanced in this range, and a 2-digit number is mirror-balanced exactly when its two digits are equal. In [10, 50] those are 11, 22, 33, and 44 (55 would be next but it exceeds 50). That gives 4 mirror-balanced numbers.
Example 2
Input
1200 1230
Expected
4
Explanation
Every number in this range has 4 digits, so a number "abcd" is mirror-balanced when a+b = c+d. Checking 1200 through 1230: 1203 (1+2=3, 0+3=3), 1212 (1+2=3, 1+2=3), 1221 (1+2=3, 2+1=3), and 1230 (1+2=3, 3+0=3) all qualify; no other value in the range does. That gives 4 mirror-balanced numbers.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →