Open In App

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:

Follow the steps below to solve the problem.

Below is the implementation of the above approach:




// 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




// 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




# 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)




// 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




<script>
 
// Javascript program for the above approach
 
function 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)
        document.write("NO<br>");
           
    // Check if its possible to sort
    else if(N % 4 == 0 || N % 4 == 1)
    {
        document.write("YES<br>");
   
        // Number of swap is
        // N / 2 always
        // for this approach
        document.write(Math.floor(N / 2)+"<br>");
        let k = 1, l;
           
        // Printing index of the
        // cyclic right shift
        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>");
}
 
// Driver code
let N = 5;
let arr=[ 5, 4, 3, 2, 1];
sortarray(arr, N);
 
 
// This code is contributed by avanitrachhadiya2155
</script>

Output: 
YES
2
1 2 5
2 5 4

 

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


Article Tags :