Timer is Running

Next Permutation
Submissions: 24754   Accuracy:


  Difficulty: Medium   Marks: 4

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.

For example:
1,2,3 → 1,3,2

3,2,1 → 1,2,3

The first line contains an integer T, depicting total number of test cases. Then following T lines contains an integer N depicting the size of array and next line followed by the value of array.

Print the array of next permutation in a separate line.

1 ≤ T ≤ 40
1 ≤ N ≤ 100
0 ≤ A[i] ≤ 100


1 2 3 6 5 4

1 2 4 3 5 6

** For More Input/Output Examples Use 'Expected Output' option **

Author: shef5

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.