Smallest Greater Integer With Same Digit Sum
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.
N = 28 return = 37
The 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.
N = 734 return = 743
The 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.
N = 1990 return = 2089
The 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.
N = 1000 return = 10000
The 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.
Assume that:
Nis an integer within the range[1..50,000].
public int solution(int N) {
// write your code here
}