-
Notifications
You must be signed in to change notification settings - Fork 110
/
solution.cpp
46 lines (45 loc) · 1.15 KB
/
solution.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
/* Problem Link:-
https://www.hackerrank.com/challenges/almost-sorted
*/
#include<bits/stdc++.h>
using namespace std;
// Sort and compare.
int main()
{
int n;
int arr[100000];
scanf("%d",&n);
for (int i=0;i<n;i++) //taking input for the array
scanf("%d",&arr[i]);
int sorted[100000];
for (int i=0;i<n;i++)
sorted[i]=arr[i];
sort(sorted,sorted+n); //sorting
vector<int> diff;
//main logic
for (int i=0;i<n;i++)
if (sorted[i]!=arr[i])
diff.push_back(i);
if (diff.size()==0)
printf("yes\n");
else if (diff.size()==2)
printf("yes\nswap %d %d\n",diff[0]+1,diff[1]+1); //Index starts at 1!!!
else //Let's try reverse it!
{
int st = diff[0], ed = diff[diff.size()-1];
while (st<ed)
{
swap(arr[st],arr[ed]);
st++;
ed--;
}
int flag=1;
for (int i=0;i<n;i++)
if (sorted[i]!=arr[i])
{
printf("no\n");
return 0;
}
printf("yes\nreverse %d %d\n",diff[0]+1,diff[diff.size()-1]+1); //Index starts at 1!!!
}
}