-
Notifications
You must be signed in to change notification settings - Fork 7
/
almost-sorted.cpp
58 lines (58 loc) · 997 Bytes
/
almost-sorted.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
//hackerrank almost sorted
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,l=1,r,i;
int arr[100005];
cin>>n;
for(i=1;i<=n;i++){
cin>>arr[i];
}
int store[3];
int count=0;
int x=0;
int flag1=0;
int flag2=0;
for(i=1;i<n;i++){
if(arr[i]>arr[i+1]){
count++;
}
if(count==1 && flag1==0){
store[x++]=i;
flag1=1;}
else if(count==2 && flag2==0) {store[x++]=i+1;
flag2=1;}
if(count>2) break;
}
int flag=0;
if(count==2){
swap(arr[store[0]],arr[store[1]]);
flag=1;
}
if(flag==0){
for(i=1;i<n;i++){
if(arr[i]>arr[i+1]){
l=i;
break;
}
}
for(i=l;i<n;i++){
if(arr[i]<arr[i+1]){
r=i;
break;
}
}
if(i==n) r=n;
reverse(arr+l,arr+r+1);
}
for(i=1;i<n;i++){
if(arr[i]>arr[i+1]) break;
}
if(i!=n) cout<<"no";
else if(l==r) cout<<"yes";
else if(l==r-1) cout<<"yes\nswap "<<l<<" "<<r;
else if(flag==1) cout<<"yes\nswap "<<store[0]<<" "<<store[1];
else cout<<"yes\nreverse "<<l<<" "<<r;
return 0;
}