Check if a decreasing Array can be sorted using Triple cyclic shift
Last Updated :
08 Jun, 2021
Given an arr[] of size N whose elements are sorted in descending order. The task is to find if the given array can be sorted in ascending order by performing a minimum number of triple cyclic right swaps. Print the indexes involved in each of the triple cyclic right swap.
Triple Cyclic Right Swap refers to the triple cyclic right shift in which:
arr[i] -> arr[j] -> arr[k] -> arr[i], where 0 <= i, j, k < N and i, j and k must be different.
Note: Following examples have 1-based indexing.
Examples:
Input: arr[] = {100, 90, 80, 70, 60}
Output: YES
2
[1 2 5]
[2 5 4]
Explanation:
For the first operation the indexes chosen are 1, 2 and 5.
arr[1] = arr[5] = 60
arr[2] = arr[1] = 100
arr[5] = arr[2] = 90
The updated array after 1st cyclic right shift is {60 100 80 70 90}
For the first operation the indexes chosen are 2, 5 and 4.
arr[2] = arr[4] = 70
arr[5] = arr[2] = 100
arr[4] = arr[5] = 90
The updated array after 2nd cyclic right shift is {60 70 80 90 100}
Thus the array is sorted by just 2 cyclic right swap operations.
Input: arr[] = {7, 6, 5, 4, 3, 2, 1}
Output: NO
Explanation:
It is not possible to perform any cyclic right shift operation on this array and thus it cannot be sorted in ascending order.
Approach:
The following observations are to be made:
- For array with 3 elements the answer is NO. Because the middle element is in its correct position and we will be left with two elements whereas three elements are needed for cyclic right shift.
- If (N % 4) == 0 or (N % 4) == 1 then its possible to sort else it is not possible to sort.
From the above approach it could be seen that at every two cyclic right shifts four elements are sorted.
- When N % 4 == 0:
Let us consider array [4 3 2 1]. After two shift for indexes 1 2 4 and 2 4 3 (in order) the array is sorted as 1 2 3 4.
- When N % 4 == 1:
The above mentioned example 1 using 5 elements in the array is applicable. The element at index 3 remains as such whereas the other 4 elements are sorted in 2 cyclic right shifts.
- When N % 4 == 2:
It is not possible to sort the array as after sorting groups of 4 elements, finally 2 elements will be left in incorrect order which can never be sorted as for sorting exactly 3 elements are needed.
- When N % 4 == 3:
It is not possible to sort the array as after groups of 4 elements, finally 3 elements will be left in incorrect order which can never be sorted as the middle element out of these 3 unsorted elements remains at its correct position right from the beginning. This leaves us with 2 elements which can never be sorted as for sorting exactly 3 elements are needed.
Follow the steps below to solve the problem.
- If the value of N modulo 4 is 2 or 3, print NO.
- If the value of N modulo 4 is 0 or 1 then,
- Print YES
- Print the value of floor(N / 2), as floor(N / 2) is the number of Cyclic Right Swap Operations that are to be performed.
- Initialize a variable K to 1.
- The cyclic right swap operations can be performed in pairs only. The pairs are [K, K+1, N] and [K+1, N, N-1]. As soon as the pairs are printed, increment the value of K by 2 and decrement the value of N by 2.
- Keep performing step 4 till all the floor(N/2) operations are printed.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
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);
}
}
|
Python3
def sortarray(arr, N):
if (N = = 3 ):
print ( "NO" )
elif (N % 4 = = 0
or N % 4 = = 1 ):
print ( "YES" )
print (N / / 2 )
k = 1
for l in range (N / / 4 ):
print (k, k + 1 , N)
print (k + 1 , N, N - 1 )
k = k + 2
N = N - 2
else :
print ( "NO" )
if __name__ = = "__main__" :
N = 5
arr = [ 5 , 4 , 3 , 2 , 1 ]
sortarray(arr, N)
|
C#
using System;
class GFG{
static void sortarray( int [] arr, int N)
{
if (N == 3)
Console.WriteLine( "NO" );
else if (N % 4 == 0 || N % 4 == 1)
{
Console.WriteLine( "YES" );
Console.WriteLine(N / 2);
int k = 1;
for ( int l = 0; l < (N / 4); l++)
{
Console.WriteLine(k + " " + (k + 1) +
" " + N);
Console.WriteLine(k + 1 + " " + N +
" " + (N - 1));
k = k + 2;
N = N - 2;
}
}
else
Console.WriteLine( "NO" );
}
public static void Main()
{
int N = 5;
int []arr = { 5, 4, 3, 2, 1 };
sortarray(arr, N);
}
}
|
Javascript
<script>
function sortarray(arr,N)
{
if (N == 3)
document.write( "NO<br>" );
else if (N % 4 == 0 || N % 4 == 1)
{
document.write( "YES<br>" );
document.write(Math.floor(N / 2)+ "<br>" );
let k = 1, l;
for (l = 0; l < Math.floor(N / 4); l++)
{
document.write(k + " " + (k + 1) +
" " + N+ "<br>" );
document.write(k + 1 + " " +
N + " " + (N - 1)+ "<br>" );
k = k + 2;
N = N - 2;
}
}
else
document.write( "NO<br>" );
}
let N = 5;
let arr=[ 5, 4, 3, 2, 1];
sortarray(arr, N);
</script>
|
Output:
YES
2
1 2 5
2 5 4
Time complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...