Given q explaination and cpp soln

Given an array Arr[], Treat each element of the array as the digit and whole array as the number. 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. Note: The replacement must be in-place, do not allocate extra memory.

Input Format

The First Line contains the Number of test cases T.
Next Line contains an Integer N, number of digits of the number.
Next Line contains N-space separated integers which are elements of the array ‘Arr’.
Constraints

1 <= T <= 100
1 <= N <= 1000
0 <= Ai <= 9
Output Format

Print the Next Permutation for each number separated by a new Line.
Sample Input

2
3
1 2 3
3
3 2 1

Sample Output

1 3 2
1 2 3

Explanation

Possible permutations for {1,2,3} are {1,2,3} , {1,3,2} , {2,1,3} , {2,3,1}, {3,1,2} and {3,2,1}. {1,3,2} is the immediate next permutation after {1,2,3}.
For the second testcase , {3,2,1} is the last configuration so we print the first permutation as its next permutation.

Hey @Kash-Moulik-3715574511847721
How can i help?

Hey @Kash-Moulik-3715574511847721
I am suggesting you an approach, please try to implement it yourself.
image
if you don’t get any p in the first step… that is the corner case… you just need to reverse the array and print it.
:slight_smile:

Hey @Kash-Moulik-3715574511847721
Do u need anymore help in this ?

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.