Check if a decreasing Array can be sorted using Triple cyclic shift | Javascript program for the above approach ; If array is 3 2 1 can ' t ▁ ▁ be ▁ sorted ▁ because ▁ 2 ▁ is ▁ in ▁ ▁ its ▁ correct ▁ position , ▁ 1 ▁ ▁ and ▁ 3 ▁ can ' t shift right because cyclic right shift takes place between 3 elements ; Check if its possible to sort ; Number of swap is N / 2 always for this approach ; Printing index of the cyclic right shift ; Driver code
function sortarray ( arr , N ) { if ( N == 3 ) document . write ( " " ) ; else if ( N % 4 == 0 N % 4 == 1 ) { document . write ( " " ) ; document . write ( Math . floor ( N / 2 ) + " " ) ; let k = 1 , l ; for ( l = 0 ; l < Math . floor ( N / 4 ) ; l ++ ) { document . write ( k + " " + ( k + 1 ) + " " + N + " " ) ; document . write ( k + 1 + " " + N + " " + ( N - 1 ) + " " ) ; k = k + 2 ; N = N - 2 ; } } else document . write ( " " ) ; } let N = 5 ; let arr = [ 5 , 4 , 3 , 2 , 1 ] ; sortarray ( arr , N ) ;