Next Permutation

Implement the next permutation, which rearranges numbers into the numerically next greater permutation of numbers.

If such arrangement is not possible, it must be rearranged as the lowest possible order ie, sorted in an ascending order.

The replacement must be in-place, do not allocate extra memory.

Examples:

1,2,3 → 1,3,2

3,2,1 → 1,2,3

1,1,5 → 1,5,1

20, 50, 113 → 20, 113, 50

Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.

Warning : DO NOT USE LIBRARY FUNCTION FOR NEXT PERMUTATION.

Use of Library functions will disqualify your submission retroactively and will give you penalty points.

Interview Code Editor
Hints
  • Solution Approach
  • Complete Solution
5270 successful submissions.
Asked In:
  • Microsoft
  • Amazon
Click here to jump start your coding interview preparation