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.
n^2 solution using recursion by sorting smallest element at begining:
deflarrysArray(A):n=len(A)defget_location(x):foriinrange(n):ifA[i]==x:returnidefmove(x,pos):targeted_pos=x-1iftargeted_pos==pos:returnTrue# decide which triplet to selecti,j,k=0,0,0# only 1 awayifpos-targeted_pos==1:i,j,k=targeted_pos,pos,pos+1ifk>=n:returnFalseleftshift(i,j,k)returnTrue# leftshift til x reach targeted posi,j,k=pos-2,pos-1,posleftshift(i,j,k,2)returnmove(x,i)defleftshift(i,j,k,repeat=1):i_p=i+((0-repeat)%3)j_p=i+((1-repeat)%3)k_p=i+((2-repeat)%3)ai,aj,ak=A[i],A[j],A[k]A[i_p]=aiA[j_p]=ajA[k_p]=akreturnforiinrange(1,n+1):i_pos=get_location(i)ifnotmove(i,i_pos):return'NO'return'YES'
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Larry's Array
You are viewing a single comment's thread. Return to all comments →
n^2 solution using recursion by sorting smallest element at begining: