We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
# Making a list compare_arr to incorporate the changes in the initial array and compare the units of displacement
n=len(q)
compare_arr=[i+1 for i in range(n)]
count=0
chaotic=0
for i in range(n-1):
if q[i]==compare_arr[i]:
continue
elif q[i]==compare_arr[i+1]:
compare_arr[i], compare_arr[i+1]= compare_arr[i+1], compare_arr[i]
count+=1
elif q[i]==compare_arr[i+2]:
compare_arr[i], compare_arr[i+2]= compare_arr[i+2], compare_arr[i]
compare_arr[i+2], compare_arr[i+1]= compare_arr[i+1], compare_arr[i+2]
count+=2
else:
chaotic=1
break
if chaotic==1:
print('Too chaotic')
else:
print(count)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
New Year Chaos
You are viewing a single comment's thread. Return to all comments →
'Python 3.9' Solution: