-
Notifications
You must be signed in to change notification settings - Fork 0
/
nextPermut.cpp
69 lines (56 loc) · 1.15 KB
/
nextPermut.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
66
67
68
69
//
// Created by Ankush on 23/02/22.
//
#include <vector>
using namespace std;
/// [1,1,9,8,5]
void findNextGreaterElem(int i, int n, vector<int> &A, int &ans, int num) {
int l = i;
int r = n;
while (l<=r) {
int m = l+(r-l)/2;
if (A[m]>num) {
ans = m;
l=m+1;
} else {
r=m-1;
}
}
}
void continuousSwap(int start, int end, vector<int> &A) {
while (start<end) {
swap(A[start],A[end]);
start++;
end--;
}
}
void nextPermutation(vector<int> &A) {
if (A.size()<=1) {
return ;
}
vector<int> ans;
int n= A.size()-1; /// last element
int flag=1;
for(int i=n-1;i>=0;i--) {
if(A[i]>=A[i+1]) {
flag*=1;
continue;
} else {
int ans=-1;
flag*=0;
findNextGreaterElem(i+1,n,A, ans, A[i]);
if(ans!=-1) {
swap(A[i],A[ans]);
continuousSwap(i+1,n,A);
}
}
}
if (flag) {
/// implies cant do next permutation
continuousSwap(0,n,A);
}
return;
}
int main() {
return 0;
}