-
Notifications
You must be signed in to change notification settings - Fork 2
/
next_permutation.cpp
65 lines (50 loc) · 1.44 KB
/
next_permutation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
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.
*/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int t, n, x;
cin >> t;
while(t > 0) {
cin >> n;
vector<int> v;
for(int i = 0; i < n; i++) {
cin >> x;
v.push_back(x);
}
next_permutation(v.begin(), v.end());
for(int i : v) {
cout << i << " ";
}
cout << endl;
t--;
}
return 0;
}