Loading Now

nextSameDigits

Given a positive integer, find the next larger integer that can be formed using only the digits from the original number. If not, return -1.

Example

  • For n = 115, the output should be nextSameDigits(n) = 151
  • For n = 221, the output should be nextSameDigits(n) = -1

Input/Output

  • [execution time limit] 2 seconds

  • [input] integer N

    0 <= N <= 100000

  • [output] integer

    • Next larger integer with the same digits and -1 if it doesn’t exist

Post Comment

Contact