forked from wahyuhjr/OpenEmailGenerator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNextPermutation.cpp
More file actions
37 lines (34 loc) · 849 Bytes
/
NextPermutation.cpp
File metadata and controls
37 lines (34 loc) · 849 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
#include<bits/stdc++.h>
using namespace std;
void nextPermutation(vector<int>& nums) {
int index = -1;
int n = nums.size();
for(int i=n-1; i>0;i--){
if(nums[i]>nums[i-1]){
index= i;
break;
}
}
if(index==-1)
reverse(nums.begin(), nums.end());
else{
int prev =index;
for(int i= index+1; i<n;i++){
if(nums[i]>nums[index-1] && nums[i]<=nums[prev]){
prev=i;
}
}
swap(nums[index-1], nums[prev]);
reverse(nums.begin()+index, nums.end());
}
}
int main(){
int n;
cin>>n;
vector <int> nums;
for(int i=0; i<n; i++){
nums.push_back(n);
}
nextPermutation(nums);
return 0;
}