-
Notifications
You must be signed in to change notification settings - Fork 217
/
Larry's-Array.py
53 lines (47 loc) · 1.46 KB
/
Larry's-Array.py
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
__username__ = 'aishitdua'
# Hackerrank problem url: https://www.hackerrank.com/challenges/larrys-array/problem
import math
import os
import random
import re
import sys
import copy
def larrysArray(realList):
"""
Input: List unsorted
Output: If list can be sorted using only rotation of 3 consecutive elements
"""
sortedList = copy.copy(realList)
sortedList.sort()
listLen = len(sortedList)
for i in range(listLen - 2):
if realList[i] == sortedList[i]:
continue
myInd = realList.index(sortedList[i])
while myInd > i:
if myInd == listLen - 1:
temp = realList[myInd - 2]
realList[myInd - 2] = realList[myInd - 1]
realList[myInd - 1] = realList[myInd]
realList[myInd] = temp
else:
temp = realList[myInd - 1]
realList[myInd - 1] = realList[myInd]
realList[myInd] = realList[myInd + 1]
realList[myInd + 1] = temp
myInd -= 1
if realList[listLen - 1] == sortedList[listLen - 1]:
#print("YES")
return "YES"
else:
#print("NO")
return "NO"
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
t = int(input())
for t_itr in range(t):
n = int(input())
A = list(map(int, input().rstrip().split()))
result = larrysArray(A)
fptr.write(result + '\n')
fptr.close()