Check if a decreasing Array can be sorted using Triple cyclic shift | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortarray ( int arr [ ] , int N ) { if ( N == 3 ) cout << " NO " << endl ; else if ( N % 4 == 0 N % 4 == 1 ) { cout << " YES " << endl ; cout << ( N / 2 ) << endl ; int k = 1 ; for ( int l = 0 ; l < ( N / 4 ) ; l ++ ) { cout << k << " ▁ " << k + 1 << " ▁ " << N << endl ; cout << k + 1 << " ▁ " << N << " ▁ " << N - 1 << endl ; k = k + 2 ; N = N - 2 ; } } else cout << " NO " << endl ; } int main ( ) { int N = 5 ; int arr [ ] = { 5 , 4 , 3 , 2 , 1 } ; sortarray ( arr , N ) ; return 0 ; }