-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14.cpp
More file actions
35 lines (34 loc) · 650 Bytes
/
14.cpp
File metadata and controls
35 lines (34 loc) · 650 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
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main(){
//program to find next permutation.
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int ind = -1;
for(int i=n-1;i>=0;i--){
if(i-1>=0 && arr[i-1]<arr[i]){
ind = i-1;
break;
}
}
if(ind>=0){
for(int i=n-1;i>ind;i--){
if(arr[i]>arr[ind]){
swap(arr[i], arr[ind]);
break;
}
}
}
int j = n-1, i=ind+1;
while(i<j){
swap(arr[i], arr[j]);
j--;
i++;
}
return 0;
}