← Back to challenges

Next Largest Number

JavaScriptHardnumberssorting

Instructions

Write a function that returns the next number that can be created from the same digits as the input.

Examples

nextNumber(19) ➞ 91

nextNumber(3542) ➞ 4235

nextNumber(5432) ➞ 5432

nextNumber(58943) ➞ 59348

Notes

  • If no larger number can be formed, return the number itself.
  • Bonus: See if you can do this without generating all digit permutations.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.