-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathM0012.cpp
54 lines (49 loc) · 1008 Bytes
/
M0012.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
/*
Problem Statement: https://www.hackerrank.com/challenges/almost-sorted/problem
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool check(vector<int> &arr) {
for (int i = 0; i < arr.size() - 1; i++)
if (arr[i] > arr[i + 1])
return false;
return true;
}
void almostSorted(vector<int> &arr) {
int i, j;
for (i = 0; i < arr.size() - 1; i++)
if (arr[i] > arr[i + 1])
break;
for (j = arr.size() - 1; j > 0; j--)
if (arr[j - 1] > arr[j])
break;
if (i > j) {
cout << "yes" << endl;
return;
}
swap(arr[i], arr[j]);
if (check(arr)) {
cout << "yes" << endl;
cout << "swap " << i + 1 << " " << j + 1 << endl;
return;
}
reverse(arr.begin() + i + 1, arr.begin() + j);
if (check(arr)) {
cout << "yes" << endl;
cout << "reverse " << i + 1 << " " << j + 1 << endl;
}
else
cout << "no" << endl;
return;
}
int main() {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++)
cin >> arr[i];
almostSorted(arr);
return 0;
}