Page 1 of 1

Function to return the minimal next N-digit number with the same sum

Posted 19 July 2019 - 04:34 AM

Hello I am trying to understand this coding challenge

write a function in Python which receives a natural decimal N-digit number
(0<N<15) as an input and returns the minimal next N-digit number with the same sum
of digits or -1 if there isn't one.
Examples:
Input: Output:
123 132
0200 1001
09999999999999 18999999999999
90 -1
9999 -1

I am unsure what exactly the question is asking, can someone clarify and shed light/ I don't understand the part about next minimal

N digit number.

Is This A Good Question/Topic? 1

Replies To: Function to return the minimal next N-digit number with the same sum

#2 andrewsw Reputation: 6818
• Posts: 28,231
• Joined: 12-December 12

Re: Function to return the minimal next N-digit number with the same sum

Posted 19 July 2019 - 04:53 AM

Take the example 123. The sum of these digits is 6. Starting from the next number, 124, find the next number that also has digits which sum to 6.

124 is not correct, because their sum is 7. Progressing in this way you should discover the first available number is 132.

Re: Function to return the minimal next N-digit number with the same sum

Posted 19 July 2019 - 05:23 AM andrewsw, on 19 July 2019 - 04:53 AM, said:

Take the example 123. The sum of these digits is 6. Starting from the next number, 124, find the next number that also has digits which sum to 6.

124 is not correct, because their sum is 7. Progressing in this way you should discover the first available number is 132.

Hi Thanks for the reply.

I did understand the part about sum but if I follow what your saying, that means

I wont ever get to 132

because

124 = 7
133 = 7
223 = 7

I think this would be impossible to code, cause it does not specify if you only change just one of the numbers, and keep the rest the same.

#4 astonecipher Reputation: 2943
• Posts: 11,435
• Joined: 03-December 12

Re: Function to return the minimal next N-digit number with the same sum

Posted 19 July 2019 - 05:41 AM lychee123, on 19 July 2019 - 07:23 AM, said:

I wont ever get to 132

because

124 = 7
133 = 7
223 = 7

I think this would be impossible to code

Quote

or -1 if there isn't one.

#5 andrewsw Reputation: 6818
• Posts: 28,231
• Joined: 12-December 12

Re: Function to return the minimal next N-digit number with the same sum

Posted 19 July 2019 - 06:20 AM lychee123, on 19 July 2019 - 12:23 PM, said:

Hi Thanks for the reply.

I did understand the part about sum but if I follow what your saying, that means

I wont ever get to 132

because

124 = 7
133 = 7
223 = 7

I think this would be impossible to code, cause it does not specify if you only change just one of the numbers, and keep the rest the same.

I don't follow this.

You would check all three digit numbers after 123, so 124,125,126,etc. You will reach 132 and realise that it sums to 6, so stop. If you reach 999 and haven't found a solution then you would also stop.

Quote

I think this would be impossible to code

Does that seem likely...? The more likely case is that you haven't yet fully understood the question.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }