Find the least next N-digit number with the same sum of digits.

2.3k Views Asked by At

Given a number of N-digits A, I want to find the next least N-digit number B having the same sum of digits as A, if such a number exists. The original number A can start with a 0. For ex: A-> 111 then B-> 120, A->09999 B-> 18999, A->999 then B-> doesn't exist.

2

There are 2 best solutions below

4
On BEST ANSWER

You get the required number by adding $9$, $90$, $900$ etc to $A$, depending on the digits of $A$.

First Case If $A$ does not end in a row of $9$s find the first (starting at the units end) non-zero digit. Write a $9$ under that digit and $0$s under all digits to the right of it. Add the two and you get $B$.

Example: $A=3450$. The first non-zero digit is the 5 so we write a $9$ under that and a $0$ to its right and add:
\begin{align} 3450\\ 90\\ \hline 3540 \end{align}

There is a problem if the digit to the left of the chosen non-zero digit is a $9$. In this case we write a $9$ under that $9$ and $0$s to its right. And if there are several $9$ we put our $9$ under the highest one.

Example: $A=3950$. The first non-zero digit is the 5 but there is a $9$ to its left. We write a $9$ under that $9$ instead and $0$s to its right and add:
\begin{align} 3950\\ 900\\ \hline 4850 \end{align}

Second case If $A$ does end in a row of $9$s write a $9$ under the highest of the row of $9$s and $0$s under all digits to the right of it. Add the two and you get $B$. As you say, if $A$ is entirely $9$s there is no solution.

Example: $A=3999$. The highest $9$ is in the hundreds column so we write a $9$ under that and $9$s to its right and add:
\begin{align} 3999\\ 900\\ \hline 4899 \end{align}

Edit This method does not always give the correct result. I will try and fix it in due course.
Edit 2 I have corrected my method and posted it in a new answer, so that this question's comment remain relevant.

4
On

Divide the number into four regions:
Region 1: Trailing zeros.
Region 2: The lowest digit not in Region 1.
Region 3: Consecutive $9$s starting with the digit above Region 2.
Region 4: All remaining digits.

Region 1 and Region 3 may be empty. Region 4 may also be empty: if it is assume that it has value 0.

The required number is made up from bolting together the values $$\text{Region 4} + 1\quad\text{Region 1} \quad\text{Region 2} - 1 \quad\text{Region 3.}$$ It is obvious that the number of digits is the same because the $1$ added to Region 4 is cancelled out by the $1$ subtracted from Region 2 and all the other digits are the same, if in a different order.

Example 1: $217$ has no Region 1 or Region 3, Region 2 is $7$ and Region 4 is $21$. The required number is made up from $21+1$ and $7-1$, or $226$.

Example 2: $197$ has no Region 1, Region 2 is $7$, Region 3 is $9$ and Region 4 is $1$. The required number is made up from $1+1$ and $7-1$ and $9$, or $269$.

Example 3: $97$ has no Region 1, Region 2 is $7$, Region 3 is $9$ and Region 4 is empty so is assigned $0$. The required number is made up from $0+1$ and $7-1$ and $9$, or $169$.

Example 4: $199$ has no Region 1, Region 2 is $9$, Region 3 is $9$ and Region 4 is $1$. The required number is made up from $1+1$ and $9-1$ and $9$, or $289$.

Example 5: $468992000$ Region 1 is $000$, Region 2 is $2$, Region 3 is $99$ and Region 4 is $468$. The required number is made up from $468+1$ and $000$ and and $2-1$ and $99$, or $469000199$.