给定一个大小为N的数组arr[] (由重复项组成),任务是检查给定的数组是否可以通过旋转来转换为非递减数组。如果不可能这样做,则打印“否”。否则,打印“是”。
例子:
Input: arr[] = {3, 4, 5, 1, 2}
Output: Yes
Explanation: After 2 right rotations, the array arr[] modifies to {1, 2, 3, 4, 5}
Input: arr[] = {1, 2, 4, 3}
Output: No
方法:这个想法是基于这样一个事实:通过旋转给定的数组并检查每个单独的旋转数组,最多可以得到N 个不同的数组,它是否是非递减的。请按照以下步骤解决问题:
- 初始化一个向量,比如v,并将原始数组的所有元素复制到其中。
- 对向量v 进行排序。
- 遍历原始数组并执行以下步骤:
- 在每次迭代中旋转 1。
- 如果数组变得等于向量v ,则打印“是”。否则,打印“否”。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if a
// non-decreasing array can be obtained
// by rotating the original array
void rotateArray(vector& arr, int N)
{
// Stores copy of original array
vector v = arr;
// Sort the given vector
sort(v.begin(), v.end());
// Traverse the array
for (int i = 1; i <= N; ++i) {
// Rotate the array by 1
rotate(arr.begin(),
arr.begin() + 1, arr.end());
// If array is sorted
if (arr == v) {
cout << "YES" << endl;
return;
}
}
// If it is not possible to
// sort the array
cout << "NO" << endl;
}
// Driver Code
int main()
{
// Given array
vector arr = { 3, 4, 5, 1, 2 };
// Size of the array
int N = arr.size();
// Function call to check if it is possible
// to make array non-decreasing by rotating
rotateArray(arr, N);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if a
// non-decreasing array can be obtained
// by rotating the original array
static void rotateArray(int[] arr, int N)
{
// Stores copy of original array
int[] v = arr;
// Sort the given vector
Arrays.sort(v);
// Traverse the array
for (int i = 1; i <= N; ++i) {
// Rotate the array by 1
int x = arr[N - 1];
i = N - 1;
while(i > 0){
arr[i] = arr[i - 1];
arr[0] = x;
i -= 1;
}
// If array is sorted
if (arr == v) {
System.out.print("YES");
return;
}
}
// If it is not possible to
// sort the array
System.out.print("NO");
}
// Driver Code
public static void main(String[] args)
{
// Given array
int[] arr = { 3, 4, 5, 1, 2 };
// Size of the array
int N = arr.length;
// Function call to check if it is possible
// to make array non-decreasing by rotating
rotateArray(arr, N);
}
}
// This code is contributed by splevel62.
Python3
# Python 3 program for the above approach
# Function to check if a
# non-decreasing array can be obtained
# by rotating the original array
def rotateArray(arr, N):
# Stores copy of original array
v = arr
# Sort the given vector
v.sort(reverse = False)
# Traverse the array
for i in range(1, N + 1, 1):
# Rotate the array by 1
x = arr[N - 1]
i = N - 1
while(i > 0):
arr[i] = arr[i - 1]
arr[0] = x
i -= 1
# If array is sorted
if (arr == v):
print("YES")
return
# If it is not possible to
# sort the array
print("NO")
# Driver Code
if __name__ == '__main__':
# Given array
arr = [3, 4, 5, 1, 2]
# Size of the array
N = len(arr)
# Function call to check if it is possible
# to make array non-decreasing by rotating
rotateArray(arr, N)
# This code is contributed by ipg2016107.
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to check if a
// non-decreasing array can be obtained
// by rotating the original array
static void rotateArray(int[] arr, int N)
{
// Stores copy of original array
int[] v = arr;
// Sort the given vector
Array.Sort(v);
// Traverse the array
for (int i = 1; i <= N; ++i) {
// Rotate the array by 1
int x = arr[N - 1];
i = N - 1;
while(i > 0){
arr[i] = arr[i - 1];
arr[0] = x;
i -= 1;
}
// If array is sorted
if (arr == v) {
Console.Write("YES");
return;
}
}
// If it is not possible to
// sort the array
Console.Write("NO");
}
// Driver code
public static void Main()
{
// Given array
int[] arr = { 3, 4, 5, 1, 2 };
// Size of the array
int N = arr.Length;
// Function call to check if it is possible
// to make array non-decreasing by rotating
rotateArray(arr, N);
}
}
// This code is contributed by susmitakundugoaldanga.
输出
YES
时间复杂度: O(N 2 )
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live