Problem · Math

Smallest Greater Integer With Same Digit Sum

EasyTeslaOA

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.

Examples
01 · Example 1
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.

02 · Example 2
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.

03 · Example 3
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.

04 · Example 4
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.

Constraints

Assume that:

  • N is an integer within the range [1..50,000].
More Tesla problems
drafts saved locally
public int solution(int N) {
  // write your code here
}
N28
expected37
checking account