Check if a decreasing Array can be sorted using Triple cyclic shift | Java 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
class GFG { static void sortarray ( int arr [ ] , int N ) { if ( N == 3 ) System . out . println ( " NO " ) ; else if ( N % 4 == 0 N % 4 == 1 ) { System . out . println ( " YES " ) ; System . out . println ( N / 2 ) ; int k = 1 , l ; for ( l = 0 ; l < ( N / 4 ) ; l ++ ) { System . out . println ( k + " ▁ " + ( k + 1 ) + " ▁ " + N ) ; System . out . println ( k + 1 + " ▁ " + N + " ▁ " + ( N - 1 ) ) ; k = k + 2 ; N = N - 2 ; } } else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int N = 5 ; int arr [ ] = { 5 , 4 , 3 , 2 , 1 } ; sortarray ( arr , N ) ; } }