forked from Sniper7sumit/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextPermutation.cpp
More file actions
46 lines (38 loc) · 914 Bytes
/
Copy pathNextPermutation.cpp
File metadata and controls
46 lines (38 loc) · 914 Bytes
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
//Next Permutations
//Find next greater number with same set of digits
#include <bits/stdc++.h>
using namespace std;
class Solution{
public:
vector<int> nextPermutation(int N, vector<int> arr)
{
//using inbuilt function
vector<int> ans(arr);
next_permutation(ans.begin(), ans.end());
if(ans != arr)
return ans;
else
{
//if the current permutation is the last permutation, then return sorted order
sort(ans.begin(), ans.end());
return ans;
}
}
};
int main(){
int t;
cin>>t;
while(t--){
int N;
cin>>N;
vector<int> arr(N);
for(int i = 0;i < N;i++)
cin>>arr[i];
Solution ob;
vector<int> ans = ob.nextPermutation(N, arr);
for(int u: ans)
cout<<u<<" ";
cout<<"\n";
}
return 0;
}