Check if it is possible to sort X[] using given operations
Last Updated :
05 Apr, 2023
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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
int N = 7 ;
int X[] = { 4 , 1 , 2 , 7 , 2 , 5 , 6 };
PossibleToSort(N, X);
}
static void PossibleToSort( int N, int X[])
{
int i = 1 ;
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++
#include <bits/stdc++.h>
using namespace std;
void PossibleToSort( int N, int X[])
{
int i = 1;
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()
{
int N = 7;
int X[] = { 4, 1, 2, 7, 2, 5, 6 };
PossibleToSort(N, X);
}
|
Python3
def PossibleToSort(N, X):
i = 1
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" )
if __name__ = = '__main__' :
N = 7
X = [ 4 , 1 , 2 , 7 , 2 , 5 , 6 ]
PossibleToSort(N, X)
|
C#
using System;
class GFG {
static void PossibleToSort( int N, int [] X)
{
int i = 1;
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" );
}
}
static void Main( string [] args)
{
int N = 7;
int [] X = { 4, 1, 2, 7, 2, 5, 6 };
PossibleToSort(N, X);
}
}
|
Javascript
function PossibleToSort(N, X) {
let i = 1;
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" );
}
}
( function () {
let N = 7;
let X = [4, 1, 2, 7, 2, 5, 6];
PossibleToSort(N, X);
})
();
|
Time Complexity: O(N)
Auxiliary Space: O(1), As no extra space is required.
Share your thoughts in the comments
Please Login to comment...