How would you calculate the next number where the sum of odd positioned digits is equal to the sum of even positioned digits? A "nice" number is a number whose sum of even positioned digits is the same as the sum of odd positioned digits.

For example:
$12375$ is nice because $(1+3+5) = (2+7)$
$9876$ is not nice because $(9+7) \ne (8+6)$

I'm trying to figure out, given a number $n$, how would you find the smallest nice number greater than $n$ without just counting up by one and checking?
I have no idea what to do, any help would be appreciated. It feels like there should be an elegant solution, but I can't see it... I thought for a while about permutations and finding all possibilities for a given digitsum but there's always testcases where it falls apart. Any ideas?
 A: Let $n$ be given

*

*Let $a_ma_{m-1}\ldots a_1a_0$ be the decimal expansion of $n$.


*Let $S_0=a_0+a_2+a_4+\cdots$ and $S_1=a_1+a_3+\cdots$


*If $S_0<S_1$, find the minimal $k\ge0$ with $k$ odd  and $a_k<9$ or $k$ even and $a_k>0$. Set $a_k\leftarrow 9$ or $\leftarrow 0$, repectively, depending on the parity of $k$. If this makes $S_0\ge S_1$, set $a_k$ to the digit that makes $S_0=S_1$ instead and go to step 5. Otherwise repeat step 3.


*If $S_0>S_1$, find the minimal $k\ge0$ with $k$ odd  and $a_k>0$ or $k$ even and $a_k<9$. Set $a_k\leftarrow 9$ or $\leftarrow 0$, repectively, depending on the parity of $k$. If this makes $S_0\le S_1$, set $a_k$ to the digit that makes $S_0=S_1$ instead and go to step 5. Otherwise repeat step 4.


*Now $S_0=S_1$. If the current number $a_m\ldots a_0$ is $>n$, we are done.
Otherwise, it is necessary to increase $a_{k+1}$. So let $n'=(\lfloor n/10^{k+1}\rfloor +1)\cdot 10^{k+1}$, set $a_m\ldots a_0$ to the decimal expansion of $n'$ and go back to step 2
