Check if a decreasing Array can be sorted using Triple cyclic shift

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 

[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,
    1. Print YES
    2. Print the value of floor(N / 2), as floor(N / 2) is the number of Cyclic Right Swap Operations that are to be performed.
    3. Initialize a variable K to 1.
    4. 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.
    5. Keep performing step 4 till all the floor(N/2) operations are printed.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach 
#include <bits/stdc++.h> 
using namespace std; 
  
void sortarray(int arr[],int N) 
      
    // 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 
    if(N == 3) 
        cout << "NO" << endl; 
          
    // Check if its possible to sort 
    else if(N % 4 == 0 || N % 4 == 1) 
    
        cout << "YES" << endl; 
  
        // Number of swap is 
        // N / 2 always 
        // for this approach 
        cout << (N / 2) << endl; 
        int k = 1; 
          
        // Printing index of the 
        // cyclic right shift 
        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; 
  
// Driver code 
int main() 
    int N = 5; 
    int arr[] = { 5, 4, 3, 2, 1 }; 
      
    sortarray(arr, N); 
    return 0; 
  
// This code is contributed by divyeshrabadiya07 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach 
class GFG{ 
      
static void sortarray(int arr[], int N) 
      
    // 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 
    if(N == 3
    System.out.println("NO"); 
          
    // Check if its possible to sort 
    else if(N % 4 == 0 || N % 4 == 1
    
        System.out.println("YES"); 
  
        // Number of swap is 
        // N / 2 always 
        // for this approach 
        System.out.println(N / 2); 
        int k = 1, l; 
          
        // Printing index of the 
        // cyclic right shift 
        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"); 
  
// Driver code 
public static void main (String []args) 
    int N = 5
    int arr[] = { 5, 4, 3, 2, 1 }; 
      
    sortarray(arr, N); 
  
// This code is contributed by chitranayal 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach 
def sortarray(arr, N): 
      
    # 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 
    if(N == 3): 
        print("NO"
          
    # check if its possible to sort 
    elif(N % 4 == 0
        or N % 4 == 1): 
        print("YES"
  
        # Number of swap is 
        # N / 2 always 
        # for this approach 
        print(N // 2
        k = 1
          
        # printing index of the 
        # cyclic right shift 
        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"
          
# Driver code 
if __name__ == "__main__"
      
    N = 5
    arr = [5, 4, 3, 2, 1
    sortarray(arr, N) 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach 
using System;
  
class GFG{
      
static void sortarray(int[] arr, int N) 
      
    // 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 
    if(N == 3) 
        Console.WriteLine("NO"); 
      
    // Check if its possible to sort 
    else if(N % 4 == 0 || N % 4 == 1) 
    
        Console.WriteLine("YES"); 
  
        // Number of swap is 
        // N / 2 always 
        // for this approach 
        Console.WriteLine(N / 2); 
        int k = 1; 
      
        // Printing index of the 
        // cyclic right shift 
        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"); 
  
// Driver code 
public static void Main() 
{
    int N = 5; 
    int []arr = { 5, 4, 3, 2, 1 }; 
  
    sortarray(arr, N); 
}
}
  
// This code is contributed by sanjoy_62

chevron_right


Output: 

YES
2
1 2 5
2 5 4

 

Time complexity: O(N)
Auxiliary Space: O(1)
 

competitive-programming-img




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.