📌  相关文章
📜  计算以非递增顺序对给定数组进行排序所需的旋转次数

📅  最后修改于: 2021-09-04 11:26:04             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是按逆时针旋转的最小次数以非递增顺序对数组进行排序。如果无法对数组进行排序,则打印“-1” 。否则,打印旋转次数。

例子:

方法:思想是遍历给定的数组arr[]并计算满足arr[i + 1] > arr[i]的索引数量。请按照以下步骤解决问题:

  • arr[i + 1] > arr[i]的计数存储在一个变量中,并在arr[i+1] > arr[i]时存储索引。
  • 如果count 的值为N – 1 ,则数组按非递减顺序排序。所需的步骤正好是(N – 1)
  • 如果count 的值为0 ,则数组已按非递增顺序排序。
  • 如果count 的值为1arr[0] ≤ arr[N – 1] ,则所需的旋转次数等于(index + 1) ,通过将所有数字移至该索引。此外,检查arr[0] ≤ arr[N – 1]以确保序列是否非递增。
  • 否则,无法按非递增顺序对数组进行排序。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count minimum anti-
// clockwise rotations required to
// sort the array in non-increasing order
void minMovesToSort(int arr[], int N)
{
    // Stores count of arr[i + 1] > arr[i]
    int count = 0;
 
    // Store last index of arr[i+1] > arr[i]
    int index;
 
    // Traverse the given array
    for (int i = 0; i < N - 1; i++) {
 
        // If the adjacent elements are
        // in increasing order
        if (arr[i] < arr[i + 1]) {
 
            // Increment count
            count++;
 
            // Update index
            index = i;
        }
    }
 
    // Print the result according
    // to the following conditions
    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;
    }
 
    // Otherwise, it is not
    // possible to sort the array
    else {
        cout << "-1";
    }
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 2, 1, 5, 4, 2 };
    int N = sizeof(arr)
            / sizeof(arr[0]);
 
    // Function Call
    minMovesToSort(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
   
class GFG{
   
// Function to count minimum anti-
// clockwise rotations required to
// sort the array in non-increasing order
static void minMovesToSort(int arr[], int N)
{
     
    // Stores count of arr[i + 1] > arr[i]
    int count = 0;
  
    // Store last index of arr[i+1] > arr[i]
    int index = 0;
  
    // Traverse the given array
    for(int i = 0; i < N - 1; i++)
    {
         
        // If the adjacent elements are
        // in increasing order
        if (arr[i] < arr[i + 1])
        {
             
            // Increment count
            count++;
  
            // Update index
            index = i;
        }
    }
  
    // Print the result according
    // to the following conditions
    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);
    }
  
    // Otherwise, it is not
    // possible to sort the array
    else
    {
        System.out.print("-1");
    }
}
   
// Driver Code
public static void main(String[] args)
{
     
    // Given array
    int[] arr = { 2, 1, 5, 4, 2 };
    int N = arr.length;
     
    // Function Call
    minMovesToSort(arr, N);
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python program for the above approach
  
# Function to count minimum anti-
# clockwise rotations required to
# sort the array in non-increasing order
def minMovesToSort(arr, N) :
     
    # Stores count of arr[i + 1] > arr[i]
    count = 0
  
    # Store last index of arr[i+1] > arr[i]
    index = 0
  
    # Traverse the given array
    for i in range(N-1):
  
        # If the adjacent elements are
        # in increasing order
        if (arr[i] < arr[i + 1]) :
  
            # Increment count
            count += 1
  
            # Update index
            index = i
         
    # Prthe result according
    # to the following conditions
    if (count == 0) :
        print("0")
     
    elif (count == N - 1) :
        print( N - 1)
     
    elif (count == 1
            and arr[0] <= arr[N - 1]) :
        print(index + 1)
     
    # Otherwise, it is not
    # possible to sort the array
    else :
        print("-1")
  
# Driver Code
 
# Given array
arr = [ 2, 1, 5, 4, 2 ]
N = len(arr)
  
# Function Call
minMovesToSort(arr, N)
 
# This code i contributed by sanjoy_62.


C#
// C# program for the above approach
using System;
    
class GFG{
    
// Function to count minimum anti-
// clockwise rotations required to
// sort the array in non-increasing order
static void minMovesToSort(int[] arr, int N)
{
     
    // Stores count of arr[i + 1] > arr[i]
    int count = 0;
   
    // Store last index of arr[i+1] > arr[i]
    int index = 0;
   
    // Traverse the given array
    for(int i = 0; i < N - 1; i++)
    {
         
        // If the adjacent elements are
        // in increasing order
        if (arr[i] < arr[i + 1])
        {
             
            // Increment count
            count++;
   
            // Update index
            index = i;
        }
    }
   
    // Print the result according
    // to the following conditions
    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);
    }
   
    // Otherwise, it is not
    // possible to sort the array
    else
    {
        Console.Write("-1");
    }
}
    
// Driver Code
public static void Main()
{
     
    // Given array
    int[] arr = { 2, 1, 5, 4, 2 };
    int N = arr.Length;
      
    // Function Call
    minMovesToSort(arr, N);
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
2

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live