Smallest Greater Integer With Same Digit Sum
Learn this problemProblem statement
Write a function solution that, given an integer N, returns the smallest integer that is greater than N and the sum of whose digits is equal to the sum of the digits of N.
Function
solution(N: int) → intExamples
Example 1
N = 28return = 37The sum of the digits of 28 is equal to 2 + 8 = 10.
The subsequent numbers are, with the sum of their digits in brackets: 29 (11), 30 (3), 31 (4), 32 (5), 33 (6), 34 (7), 35 (8), 36 (9), and 37 (10).
37 is the smallest number bigger than 28 whose digits add up to 10.
Example 2
N = 734return = 743The sum of the digits of 734 and 743 are equal to 7 + 3 + 4 = 7 + 4 + 3 = 14. No other integer between 735 and 742 adds up to 14.
Example 3
N = 1990return = 2089The sum of the digits of both numbers is equal to 19, and there is no other integer between them with the same sum of digits.
Example 4
N = 1000return = 10000The sum of the digits of both numbers is equal to 1, and there is no other integer between them with the same sum of digits.
Constraints
Assume that:
Nis an integer within the range[1..50,000].