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
// 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);
})
();
Time Complexity: O(N)
Auxiliary Space: O(1), As no extra space is required.
Similar Reads
Check if Array can be sorted in non-decreasing order using given operations Given an array arr[] of size N consisting of positive integers, the task is to check if the array can be sorted in non-decreasing order by performing the following operations: Select two adjacent elements.Swap the elements and invert their signs.All elements in the sorted array received at the end,
8 min read
Minimum operations required to Sort the Array using following operations Given an array arr[] of size N. Make the array arr[] sorted in non-decreasing order in the minimum number of operations where you can choose any integer x and replace elements arr[i] == x equal to 0 for, 0 ? i ? N - 1. Examples: Input: n = 3, arr[] = {3, 3, 2}Output: 1Explanation: If you choose x =
10 min read
Check if it is possible to sort the array after rotating it Given an array of size N, the task is to determine whether its possible to sort the array or not by just one shuffle. In one shuffle, we can shift some contiguous elements from the end of the array and place it in the front of the array.For eg: A = {2, 3, 1, 2}, we can shift {1, 2} from the end of t
8 min read
Minimize given operations required to sort the stream array Given an array stream of n integers in the form of a stream, the task is to find the minimum number of operations that are required to sort the stream (increasing order) following the below steps: In one operation you can pick up the first element and then put that element into the xth index. All th
6 min read
Check if it is possible to sort the array without swapping adjacent elements Given an array arr[] of size N, check if it is possible to sort arr[] without swapping adjacent elements. In other words, check if it is possible to sort arr[] by swapping elements but swapping two consecutive element is not allowed. Examples: Input: N = 4, arr[] = {2, 3, 1, 4}Output: YESExplanation
5 min read
Check if the array can be sorted using swaps between given indices only Given an array arr[] of size N consisting of distinct integers from range [0, N - 1] arranged in a random order. Also given a few pairs where each pair denotes the indices where the elements of the array can be swapped. There is no limit on the number of swaps allowed. The task is to find if it is p
15+ min read
Check if it is possible to sort an array with conditional swapping of adjacent allowed We are given an unsorted array of integers in the range from 0 to n-1. We are allowed to swap adjacent elements in array many number of times but only if the absolute difference between these element is 1. Check if it is possible to sort the array.If yes then print "yes" else "no". Examples: Input :
5 min read
Operations to Sort an Array in non-decreasing order Given an array arr[] of integers of size n, the task is to check if we can sort the given array in non-decreasing order(i, e.arr[i] ⤠arr[i+1]) by using two types of operation by performing any numbers of time: You can choose any index from 1 to n-1(1-based indexing) and increase arr[i] and arr[i+1]
7 min read
Check if it is possible to sort an array with conditional swapping of elements at distance K Given an array arr[] of n elements, we have to swap an index i with another index i + k any number of times and check whether it is possible to sort the given array arr[]. If it is then print âyesâ otherwise print ânoâ.Examples: Input: K = 2, arr = [4, 3, 2, 6, 7] Output: Yes Explanation: Choose ind
6 min read
Javascript Program to Check if it is possible to sort the array after rotating it Given an array of size N, the task is to determine whether its possible to sort the array or not by just one shuffle. In one shuffle, we can shift some contiguous elements from the end of the array and place it in the front of the array.For eg: A = {2, 3, 1, 2}, we can shift {1, 2} from the end of t
3 min read