-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNext Permutation.cpp
49 lines (43 loc) · 1 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
#include<bits/stdc++.h>
using namespace std;
void nextPermutation(int);
int main(){
int t, n;
cin >> t;
while(t--){
cin >> n;
nextPermutation(n);
cout << endl;
}
return 0;
}
void nextPermutation(int n){
int arr[n];
bool flag = false;
for(int i = 0; i < n; i++)
cin >> arr[i];
for(int i = 1; i < n; i++)
if(arr[i - 1] < arr[i])
flag = true;
if(flag == false){
for(int i = n - 1; i >= 0; i--)
cout << arr[i] << " ";
return;
}
for(int i = n - 2; i >= 0; i--){
if(arr[i] < arr[i + 1]){
int a = i, min = INT_MAX, b;
for(int j = i + 1; j < n; j++){
if(min > arr[j] && arr[j] >= arr[i]){
min = arr[j];
b = j;
}
}
swap(arr[a], arr[b]);
sort(arr + a + 1, arr + n);
break;
}
}
for(int i = 0; i < n; i++)
cout << arr[i] << " ";
}