Count Symmetric Integers

You are given two positive integers `low` and `high`. An integer `x` with `2 * n` digits is **symmetric** if the sum of its first `n` digits equals the sum of its last `n` digits. Numbers with an odd number of digits are never symmetric. Return the number of symmetric integers in the range `[low, high]`.

30:00

Count Symmetric Integers
easy
Free written solution
Topics
Companies

You are given two positive integers low and high.

An integer x with 2 * n digits is symmetric if the sum of its first n digits equals the sum of its last n digits. Numbers with an odd number of digits are never symmetric.

Return the number of symmetric integers in the range [low, high].

Example 1:
Input: {"low":1,"high":100}
Output: 9
Constraints:
  • 1≤low≤high≤1041 \leq low \leq high \leq 10^4

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"low":1,"high":100}

Scan every x from 1 to 100. Only numbers with an even digit count can balance, so split each one in half and compare the two digit sums.

low 1

high 100

1d skip

2d

3d skip

striped blocks have an odd digit count; green dots are recent symmetric hits

----

left right

count

0

Variables
VariableValue
low1
high100
count0
DepthFunction Call
Stack empty
0/74