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.
constNO='NO',YES='YES';functionlarrysArray(A:number[]):string{/** * idea: create a [num] increament from 1, find index that contains [num] index and * always larger than 2 be cause i will rotate [index,index-2,index-1] to move * [num] toward start of array. once [num] is at start of array, [num] increate * and shift array. re do this till array have 2 element, if 2 element is not * incrementing by 1, return NO else YES. */constrotate=(A:number[],index:number):number[]=>{consta=[...A]return[...a.slice(0,index-2),...[a[index],a[index-2],a[index-1]],...A.slice(index+1)]}letis_larry_array=NOletnum=1while(true){if(A.length<3){if(A[0]==A[1]-1)is_larry_array=YESbreak}if(A[0]==num){A.shift();num++;continue}letindex=A.indexOf(num);while(index<2)index++A=rotate(A,index)}returnis_larry_array}
Larry's Array
You are viewing a single comment's thread. Return to all comments →
my answer in Typescript