Open In App

Check if it is possible to sort X[] using given operations

Last Updated : 05 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array X[] of length N, the task is to find the minimum number of times the operation is used to sort X[], if it is not possible to sort return “NO“. Where operations are as followed:

  • Select a sub-array such that the last index must be the same as both sub-array and X[].
  • Delete that sub-array from its original position and prepend it on X[].

Examples:

Input: N = 4, X[] = {3, 4, 1, 2}
Output: YES 1
Explanation: Only 1 operation is required to sort X[]. Choose sub-array {A3 . . . A4} = {1, 2}. The last index of the subarray and X[] is the same, which is 4. Now, delete that sub-array and prepend it on X[]. So that the updated X[]: 
{1, 2, 3, 4}. Now X[] is sorted the minimum number of operations required is 1.

Input: N = 7, X[] = {4, 1, 2, 7, 2, 5, 6}
Output: NO
Explanation: It can be verified that sorting X[] using given operations is not possible.  

Approach: Implement the idea below to solve the problem

The problem is observations and Greedy logic based. This problem can be solved using implementing those observations into code. 

Steps were taken to solve the problem:

  • Create a variable let’s say I and initialize it equal to 1.
  • Run a while loop with condition (i < N && X[i – 1] ≤ X[i]) and follow the below-mentioned steps under the scope of a loop:
    • I++
    • If (I == N) print YES 0
    • Else:
      • ++I
      • Run a while loop with condition (i < N && X[i – 1] ≤ X[i]) and ++I
      • If (I == N) 
        • if (X[N – 1] ≤ X[0]) print YES 1.
        • else print NO.
      • Else print NO.

Below is the code to implement the approach:

Java




// Java code for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Driver Function
    public static void main(String[] args)
    {
 
        // Inputs
        int N = 7;
        int X[] = { 4, 1, 2, 7, 2, 5, 6 };
 
        // Function call
        PossibleToSort(N, X);
    }
 
    // Method for checking, Is it possible
    // to sort X[] By using given operation
    static void PossibleToSort(int N, int X[])
    {
 
        // Integer i initialized to 1
        int i = 1;
 
        // While loop for traversing on
        // X[] and checking condition
        while (i < N && X[i - 1] <= X[i])
            i++;
        if (i == N)
            System.out.println("YES\n0");
        else {
            ++i;
            while (i < N && X[i - 1] <= X[i])
                ++i;
            if (i == N) {
                if (X[N - 1] <= X[0])
                    System.out.println("YES\n1");
                else
                    System.out.println("NO");
            }
            else
                System.out.println("NO");
        }
    }
}


C++




// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
 
// Method for checking, Is it possible
// to sort X[] By using given operation
void PossibleToSort(int N, int X[])
{
 
    // Integer i initialized to 1
    int i = 1;
 
    // While loop for traversing on
    // X[] and checking condition
    while (i < N && X[i - 1] <= X[i])
        i++;
    if (i == N)
        cout << "YES\n0\n";
    else {
        ++i;
        while (i < N && X[i - 1] <= X[i])
            ++i;
        if (i == N) {
            if (X[N - 1] <= X[0])
                cout << "YES\n1\n";
            else
                cout << "NO\n";
        }
        else
            cout << "NO\n";
    }
}
 
int main()
{
 
    // Inputs
    int N = 7;
    int X[] = { 4, 1, 2, 7, 2, 5, 6 };
 
    // Function call
    PossibleToSort(N, X);
}


Python3




# Python code for the above approach
 
# Method for checking, Is it possible
# to sort X[] by using given operation
 
 
def PossibleToSort(N, X):
 
    # Integer i initialized to 1
    i = 1
 
    # While loop for traversing on
    # X[] and checking condition
    while i < N and X[i - 1] <= X[i]:
        i += 1
    if i == N:
        print("YES\n0")
    else:
        i += 1
        while i < N and X[i - 1] <= X[i]:
            i += 1
        if i == N:
            if X[N - 1] <= X[0]:
                print("YES\n1")
            else:
                print("NO")
        else:
            print("NO")
 
 
# Driver Function
if __name__ == '__main__':
 
    # Inputs
    N = 7
    X = [4, 1, 2, 7, 2, 5, 6]
 
    # Function call
    PossibleToSort(N, X)


C#




// C# code for the above approach
using System;
 
class GFG {
 
    // Method for checking, Is it possible
    // to sort X[] By using given operation
    static void PossibleToSort(int N, int[] X)
    {
 
        // Integer i initialized to 1
        int i = 1;
 
        // While loop for traversing on
        // X[] and checking condition
        while (i < N && X[i - 1] <= X[i])
            i++;
        if (i == N)
            Console.WriteLine("YES\n0");
        else {
            ++i;
            while (i < N && X[i - 1] <= X[i])
                ++i;
            if (i == N) {
                if (X[N - 1] <= X[0])
                    Console.WriteLine("YES\n1");
                else
                    Console.WriteLine("NO");
            }
            else
                Console.WriteLine("NO");
        }
    }
 
    // Driver Function
    static void Main(string[] args)
    {
 
        // Inputs
        int N = 7;
        int[] X = { 4, 1, 2, 7, 2, 5, 6 };
 
        // Function call
        PossibleToSort(N, X);
    }
}


Javascript




// Method for checking, Is it possible
// to sort X[] By using given operation
function PossibleToSort(N, X) {
 
    // Integer i initialized to 1
    let i = 1;
 
    // While loop for traversing on
    // X[] and checking condition
    while (i < N && X[i - 1] <= X[i])
        i++;
    if (i == N)
        console.log("YES\n0");
    else {
        ++i;
        while (i < N && X[i - 1] <= X[i])
            ++i;
        if (i == N) {
            if (X[N - 1] <= X[0])
                console.log("YES\n1");
            else
                console.log("NO");
        }
        else
            console.log("NO");
    }
}
 
// Driver Function
(function() {
 
    // Inputs
    let N = 7;
    let X = [4, 1, 2, 7, 2, 5, 6];
 
    // Function call
    PossibleToSort(N, X);
})
();


Output

NO

Time Complexity: O(N)
Auxiliary Space: O(1), As no extra space is required.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads