forked from Srinivas11789/AlgorithmNuggets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalmost.py
More file actions
42 lines (37 loc) · 1004 Bytes
/
almost.py
File metadata and controls
42 lines (37 loc) · 1004 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
36
37
38
39
40
41
#!/bin/python
import math
import os
import random
import re
import sys
# 100 pass
# Complete the almostSorted function below.
def almostSorted(arr):
n = len(arr)
sort_arr = sorted(arr)
# Swap 2 elements
diff = 0
result = []
if arr == sort_arr:
print "yes"
for i in range(n):
if arr[i] != sort_arr[i]:
result.append(i)
diff += 1
if diff == 2:
arr[result[0]],arr[result[1]] = arr[result[1]], arr[result[0]]
if sort_arr == arr:
print "yes\n"+"swap "+str(result[0]+1)+" "+str(result[1]+1)
else:
print "no"
elif diff > 2:
if sort_arr == arr[:result[0]]+arr[result[0]:result[-1]+1][::-1]+arr[result[-1]+1:]:
print "yes\n"+"reverse "+str(result[0]+1)+" "+str(result[-1]+1)
else:
print "no"
else:
print "no"
if __name__ == '__main__':
n = int(raw_input())
arr = map(int, raw_input().rstrip().split())
almostSorted(arr)