Count rotations required to sort given array in non-increasing order
Last Updated :
29 Sep, 2022
Given an array arr[] consisting of N integers, the task is to sort the array in non-increasing order by minimum number of anti-clockwise rotations. If it is not possible to sort the array, then print “-1”. Otherwise, print the count of rotations.
Examples:
Input: arr[] = {2, 1, 5, 4, 3}
Output: 2
Explanation: Two anti-clockwise rotations are required to sort the array in decreasing order, i.e. {5, 4, 3, 2, 1}
Input: arr[] = {2, 3, 1}
Output: -1
Approach: The idea is to traverse the given array arr[] and count the number of indices satisfying arr[i + 1] > arr[i]. Follow the steps below to solve the problem:
- Store the count of arr[i + 1] > arr[i] in a variable and also store the index when arr[i+1] > arr[i].
- If the value of count is N – 1, then the array is sorted in non-decreasing order. The required steps are exactly (N – 1).
- If the value of count is 0, then the array is already sorted in non-increasing order.
- If the value of count is 1 and arr[0] ? arr[N – 1], then the required number of rotations is equal to (index + 1), by performing shifting of all the numbers upto that index. Also, check if arr[0] ? arr[N – 1] to ensure if the sequence is non-increasing.
- Otherwise, it is not possible to sort the array in non-increasing order.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minMovesToSort( int arr[], int N)
{
int count = 0;
int index;
for ( int i = 0; i < N - 1; i++) {
if (arr[i] < arr[i + 1]) {
count++;
index = i;
}
}
if (count == 0) {
cout << "0" ;
}
else if (count == N - 1) {
cout << N - 1;
}
else if (count == 1
&& arr[0] <= arr[N - 1]) {
cout << index + 1;
}
else {
cout << "-1" ;
}
}
int main()
{
int arr[] = { 2, 1, 5, 4, 2 };
int N = sizeof (arr)
/ sizeof (arr[0]);
minMovesToSort(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void minMovesToSort( int arr[], int N)
{
int count = 0 ;
int index = 0 ;
for ( int i = 0 ; i < N - 1 ; i++)
{
if (arr[i] < arr[i + 1 ])
{
count++;
index = i;
}
}
if (count == 0 )
{
System.out.print( "0" );
}
else if (count == N - 1 )
{
System.out.print(N - 1 );
}
else if (count == 1 &&
arr[ 0 ] <= arr[N - 1 ])
{
System.out.print(index + 1 );
}
else
{
System.out.print( "-1" );
}
}
public static void main(String[] args)
{
int [] arr = { 2 , 1 , 5 , 4 , 2 };
int N = arr.length;
minMovesToSort(arr, N);
}
}
|
Python3
def minMovesToSort(arr, N) :
count = 0
index = 0
for i in range (N - 1 ):
if (arr[i] < arr[i + 1 ]) :
count + = 1
index = i
if (count = = 0 ) :
print ( "0" )
elif (count = = N - 1 ) :
print ( N - 1 )
elif (count = = 1
and arr[ 0 ] < = arr[N - 1 ]) :
print (index + 1 )
else :
print ( "-1" )
arr = [ 2 , 1 , 5 , 4 , 2 ]
N = len (arr)
minMovesToSort(arr, N)
|
C#
using System;
class GFG{
static void minMovesToSort( int [] arr, int N)
{
int count = 0;
int index = 0;
for ( int i = 0; i < N - 1; i++)
{
if (arr[i] < arr[i + 1])
{
count++;
index = i;
}
}
if (count == 0)
{
Console.Write( "0" );
}
else if (count == N - 1)
{
Console.Write(N - 1);
}
else if (count == 1 &&
arr[0] <= arr[N - 1])
{
Console.Write(index + 1);
}
else
{
Console.Write( "-1" );
}
}
public static void Main()
{
int [] arr = { 2, 1, 5, 4, 2 };
int N = arr.Length;
minMovesToSort(arr, N);
}
}
|
Javascript
<script>
function minMovesToSort(arr, N)
{
let count = 0;
let index = 0;
for (let i = 0; i < N - 1; i++)
{
if (arr[i] < arr[i + 1])
{
count++;
index = i;
}
}
if (count == 0)
{
document.write( "0" );
}
else if (count == N - 1)
{
document.write(N - 1);
}
else if (count == 1 &&
arr[0] <= arr[N - 1])
{
document.write(index + 1);
}
else
{
document.write( "-1" );
}
}
let arr = [2, 1, 5, 4, 2];
let N = arr.length;
minMovesToSort(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...