You are viewing a single comment's thread. Return to all comments →
def almostSorted(A): p=[] q=[] for i in range (len(A)-1): if A[i]>A[i+1]: q.append(i) p.append(i+1) if len(p)==2: A[q[0]],A[p[1]]=A[p[1]],A[q[0]] if A==sorted(A): print(f"yes\nswap {q[0]+1} {p[1]+1}") else: print('no') elif len(p)==1: A[q[0]],A[p[0]]=A[p[0]],A[q[0]] if A==sorted(A): print(f"yes\nswap {q[0]+1} {p[0]+1}") else: print('no') elif len(p)>2: pq=sorted(set(p)|set(q)) A[pq[0]:pq[len(pq)-1]+1]=A[pq[0]:pq[len(pq)-1]+1][::-1] if A==sorted(A): print('yes') print(f"reverse {pq[0]+1} {pq[-1]+1}") else: print('no')
Seems like cookies are disabled on this browser, please enable them to open this website
Almost Sorted
You are viewing a single comment's thread. Return to all comments →