-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNext Permutation.cpp
46 lines (39 loc) · 982 Bytes
/
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
/*
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
Input:
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.
Output:
Print the array of next permutation in a separate line.
Constraints:
1 ≤ T ≤ 40
1 ≤ N ≤ 100
0 ≤ A[i] ≤ 100
Example:
Input:
1
6
1 2 3 6 5 4
Output:
1 2 4 3 5 6 */
#include <bits/stdc++.h>
using namespace std;
int main() {
//code
int t,n;
cin>>t;
while(t--)
{
cin>>n;
vector<int> v(n,0);
for(auto &i:v)
cin>>i;
next_permutation(v.begin(),v.end());
for(auto &i:v)
cout<<i<<" ";
cout<<endl;
}
return 0;
}