-
Notifications
You must be signed in to change notification settings - Fork 7
/
Solution.cs
65 lines (55 loc) · 1.69 KB
/
Solution.cs
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
59
60
61
62
63
64
65
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution {
static void almostSorted(int[] arr) {
var sorted = arr.OrderBy(x => x).ToArray();
var wrongIndices = new List<int>();
for(var i = 0; i<arr.Length; i++) {
if(arr[i] == sorted[i])
continue;
wrongIndices.Add(i);
}
if(!wrongIndices.Any()) {
Console.WriteLine("yes");
return;
}
if(wrongIndices.Count == 2) {
Console.WriteLine("yes");
Console.WriteLine($"swap {wrongIndices[0]+1} {wrongIndices[1]+1}");
return;
}
//check for reverse
var firstIndex = wrongIndices[0];
var lastIndex = wrongIndices.Last();
var isReversed = true;
for(var i = firstIndex; i<lastIndex; i++) {
if(arr[i] > arr[i+1])
continue;
isReversed = false;
break;
}
if(isReversed) {
Console.WriteLine("yes");
Console.WriteLine($"reverse {firstIndex+1} {lastIndex+1}");
return;
}
Console.WriteLine("no");
}
static void Main(string[] args) {
int n = Convert.ToInt32(Console.ReadLine());
int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arrTemp => Convert.ToInt32(arrTemp))
;
almostSorted(arr);
}
}