Check if it is possible to sort the array after rotating it
Last Updated :
03 Mar, 2022
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 the array to the front of the array to sort it.
- A = {1, 2, 3, 2} since we cannot sort it in one shuffle hence it’s not possible to sort the array.
Examples:
Input: arr[] = {1, 2, 3, 4}
Output: Possible
Since this array is already sorted hence no need for shuffle.
Input: arr[] = {6, 8, 1, 2, 5}
Output: Possible
Place last three elements at the front
in the same order i.e. {1, 2, 5, 6, 8}
Approach:
- Check if the array is already sorted or not. If yes return true.
- Else start traversing the array elements until the current element is smaller than next element. Store that index where arr[i] > arr[i+1].
- Traverse from that point and check if from that index elements are in increasing order or not.
- If above both conditions satisfied then check if last element is smaller than or equal to the first element of given array.
- Print “Possible” if above three conditions satisfied else print “Not possible” if any of the above 3 conditions failed.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPossible( int a[], int n)
{
if (is_sorted(a, a + n)) {
cout << "Possible" << endl;
}
else {
bool flag = true ;
int i;
for (i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
break ;
}
}
i++;
for ( int k = i; k < n - 1; k++) {
if (a[k] > a[k + 1]) {
flag = false ;
break ;
}
}
if (!flag)
return false ;
else {
if (a[n - 1] <= a[0])
return true ;
else
return false ;
}
}
}
int main()
{
int arr[] = { 3, 1, 2, 2, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
if (isPossible(arr, n))
cout << "Possible" ;
else
cout << "Not Possible" ;
return 0;
}
|
Java
class solution
{
static boolean is_sorted( int a[], int n)
{
int c1= 0 ,c2= 0 ;
for ( int i= 0 ;i<n- 1 ;i++)
{
if (a[i]<=a[i+ 1 ])
c1++;
}
for ( int i= 1 ;i<n;i++)
{
if (a[i]<=a[i- 1 ])
c2++;
}
if (c1==n||c2==n)
return true ;
return false ;
}
static boolean isPossible( int a[], int n)
{
if (is_sorted(a,n)) {
System.out.println( "Possible" );
}
else {
boolean flag = true ;
int i;
for (i = 0 ; i < n - 1 ; i++) {
if (a[i] > a[i + 1 ]) {
break ;
}
}
i++;
for ( int k = i; k < n - 1 ; k++) {
if (a[k] > a[k + 1 ]) {
flag = false ;
break ;
}
}
if (!flag)
return false ;
else {
if (a[n - 1 ] <= a[ 0 ])
return true ;
else
return false ;
}
}
return false ;
}
public static void main(String[] args)
{
int arr[] = { 3 , 1 , 2 , 2 , 3 };
int n = arr.length;
if (isPossible(arr, n))
System.out.println( "Possible" );
else
System.out.println( "Not Possible" );
}
}
|
Python 3
def is_sorted(a):
all (a[i] < = a[i + 1 ]
for i in range ( len (a) - 1 ))
def isPossible(a, n):
if (is_sorted(a)) :
print ( "Possible" )
else :
flag = True
for i in range (n - 1 ) :
if (a[i] > a[i + 1 ]) :
break
i + = 1
for k in range (i, n - 1 ) :
if (a[k] > a[k + 1 ]) :
flag = False
break
if ( not flag):
return False
else :
if (a[n - 1 ] < = a[ 0 ]):
return True
else :
return False
if __name__ = = "__main__" :
arr = [ 3 , 1 , 2 , 2 , 3 ]
n = len (arr)
if (isPossible(arr, n)):
print ( "Possible" )
else :
print ( "Not Possible" )
|
C#
using System;
class GFG
{
static bool is_sorted( int []a, int n)
{
int c1 = 0, c2 = 0;
for ( int i = 0; i < n - 1; i++)
{
if (a[i] <= a[i + 1])
c1++;
}
for ( int i = 1; i < n; i++)
{
if (a[i] <= a[i - 1])
c2++;
}
if (c1 == n || c2 == n)
return true ;
return false ;
}
static bool isPossible( int []a, int n)
{
if (is_sorted(a,n))
{
Console.WriteLine( "Possible" );
}
else
{
bool flag = true ;
int i;
for (i = 0; i < n - 1; i++)
{
if (a[i] > a[i + 1])
{
break ;
}
}
i++;
for ( int k = i; k < n - 1; k++)
{
if (a[k] > a[k + 1])
{
flag = false ;
break ;
}
}
if (!flag)
return false ;
else
{
if (a[n - 1] <= a[0])
return true ;
else
return false ;
}
}
return false ;
}
public static void Main()
{
int []arr = { 3, 1, 2, 2, 3 };
int n = arr.Length;
if (isPossible(arr, n))
Console.WriteLine( "Possible" );
else
Console.WriteLine( "Not Possible" );
}
}
|
PHP
<?php
function is_sorted( $a , $n )
{
$c1 = 0; $c2 = 0;
for ( $i = 0; $i < $n - 1; $i ++)
{
if ( $a [ $i ] <= $a [ $i + 1])
$c1 ++;
}
for ( $i = 1; $i < $n ; $i ++)
{
if ( $a [ $i ] <= $a [ $i - 1])
$c2 ++;
}
if ( $c1 == $n || $c2 == $n )
return true;
return false;
}
function isPossible( $a , $n )
{
if (is_sorted( $a , $n ))
{
echo "Possible" . "\n" ;
}
else
{
$flag = true;
$i ;
for ( $i = 0; $i < $n - 1; $i ++)
{
if ( $a [ $i ] > $a [ $i + 1])
{
break ;
}
}
$i ++;
for ( $k = $i ; $k < $n - 1; $k ++)
{
if ( $a [ $k ] > $a [ $k + 1])
{
$flag = false;
break ;
}
}
if (! $flag )
return false;
else
{
if ( $a [ $n - 1] <= $a [0])
return true;
else
return false;
}
}
}
$arr = array ( 3, 1, 2, 2, 3 );
$n = sizeof( $arr );
if (isPossible( $arr , $n ))
echo "Possible" ;
else
echo "Not Possible" ;
?>
|
Javascript
<script>
function is_sorted(a)
{
let c1=0,c2=0;
for (let i=0;i<n-1;i++)
{
if (a[i]<=a[i+1])
{
c1++;
}
}
for (let i=1;i<n;i++)
{
if (a[i]<=a[i-1])
c2++;
}
if (c1==n||c2==n)
{
return true ;
}
return false ;
}
function isPossible(a,n)
{
if (is_sorted(a,n))
{
document.write( "Possible" );
}
else
{
let flag = true ;
let i;
for (i = 0; i < n - 1; i++)
{
if (a[i] > a[i + 1])
{
break ;
}
}
i++;
for (let k = i; k < n - 1; k++)
{
if (a[k] > a[k + 1])
{
flag = false ;
break ;
}
}
if (!flag)
{
return false ;
}
else
{
if (a[n - 1] <= a[0])
return true ;
else
return false ;
}
}
return false ;
}
let arr=[3, 1, 2, 2, 3];
let n = arr.length;
if (isPossible(arr, n))
document.write( "Possible" );
else
document.write( "Not Possible" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...