-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext_greatest_permutation.cpp
More file actions
46 lines (35 loc) · 1.06 KB
/
Copy pathnext_greatest_permutation.cpp
File metadata and controls
46 lines (35 loc) · 1.06 KB
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
//https://ide.geeksforgeeks.org/yheZzAKMTH
//next greatest permutation
#include<bits/stdc++.h>
using namespace std;
int mod=1e9+7;
#define F(a,b,var) for(int var=a;var<b;var++)
#define FAST_INP ios_base::sync_with_stdio(false);cin.tie(NULL)
int main()
{
//code
FAST_INP;
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
vector<int> a(n);
for(int j=0; j<n; j++)cin>>a[j];
int i = a.size() - 2;
while(i>=0 && a[i] >= a[i+1]) i--;
if(i == -1) return 0;
/*if(a[i] < a[i+1]){
int got_it = a[i];
int ind = i;
}*/
/*Lambda functions in C++: https://stackoverflow.com/a/7627218/4588867*/
/*int find = *find_if(a.rbegin(), a.rend(), [&](int x){ return x > a[i]; });
swap(find, got_it);*/
swap(*find_if(a.rbegin(), a.rend(), [&](int x){ return x > a[i]; }), a[i]);
reverse(a.begin() + i + 1, a.end());
for(int i=0; i<n; i++)
cout<<a[i]<<" ";
}
}