给定大小为N的数组arr [] ,任务是检查数组是否螺旋排序。如果发现是真的,则打印“是” 。否则,打印“否” 。
注意:如果arr [0]≤arr [N – 1]≤arr [1]≤arr [N – 2],则对数组进行螺旋排序。
例子:
Input: arr[] = { 1, 10, 14, 20, 18, 12, 5 }
Output: YES
Explanation:
arr[0] < arr[6]
arr[1] < arr[5]
arr[2] < arr[4]
Therefore, the required output is YES.
Input: arr[] = { 1, 2, 4, 3 }
Output: NO
方法:想法是遍历数组,对于每个数组元素(例如arr [i]) ,检查arr [i]是否小于或等于arr [N – i – 1]和arr [N – i – 1]小于或等于arr [i + 1] 。如果发现错误,则打印“否” 。否则,如果所有数组元素都满足条件,则打印“ YES” 。请按照以下步骤解决问题:
- 初始化两个变量,例如start和end ,以存储给定数组的开始索引和结束索引。
- 在start小于end时迭代一个循环,并检查arr [start]小于或等于arr [end]以及arr [end]小于或等于arr [start + 1] 。如果发现错误,则打印“否” 。
- 否则,打印“是” 。
下面是上述方法的实现:
C++14
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to check if the array is
// spirally sorted or not
bool isSpiralSorted(int arr[], int n)
{
// Stores start index
// of the array
int start = 0;
// Stores end index
// of an array
int end = n - 1;
while (start < end) {
// If arr[start]
// greater than arr[end]
if (arr[start] > arr[end]) {
return false;
}
// Update start
start++;
// If arr[end]
// greater than arr[start]
if (arr[end] > arr[start]) {
return false;
}
// Update end
end--;
}
return true;
}
// Driver Code
int main()
{
int arr[] = { 1, 10, 14, 20, 18, 12, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
if (isSpiralSorted(arr, N))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to check if the array is
// spirally sorted or not
static boolean isSpiralSorted(int[] arr, int n)
{
// Stores start index
// of the array
int start = 0;
// Stores end index
// of an array
int end = n - 1;
while (start < end)
{
// If arr[start]
// greater than arr[end]
if (arr[start] > arr[end])
{
return false;
}
// Update start
start++;
// If arr[end]
// greater than arr[start]
if (arr[end] > arr[start])
{
return false;
}
// Update end
end--;
}
return true;
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 1, 10, 14, 20, 18, 12, 5 };
int N = arr.length;
// Function Call
if (isSpiralSorted(arr, N) != false)
System.out.print("YES");
else
System.out.print("NO");
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program to implement
# the above approach
# Function to check if the array is
# spirally sorted or not
def isSpiralSorted(arr, n) :
# Stores start index
# of the array
start = 0;
# Stores end index
# of an array
end = n - 1;
while (start < end) :
# If arr[start]
# greater than arr[end]
if (arr[start] > arr[end]) :
return False;
# Update start
start += 1;
# If arr[end]
# greater than arr[start]
if (arr[end] > arr[start]) :
return False;
# Update end
end -= 1;
return True;
# Driver Code
if __name__ == "__main__" :
arr = [ 1, 10, 14, 20, 18, 12, 5 ];
N = len(arr);
# Function Call
if (isSpiralSorted(arr, N)) :
print("YES");
else :
print("NO");
# This code is contributed by AnkThon
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to check if the array is
// spirally sorted or not
static bool isSpiralSorted(int[] arr, int n)
{
// Stores start index
// of the array
int start = 0;
// Stores end index
// of an array
int end = n - 1;
while (start < end)
{
// If arr[start]
// greater than arr[end]
if (arr[start] > arr[end])
{
return false;
}
// Update start
start++;
// If arr[end]
// greater than arr[start]
if (arr[end] > arr[start])
{
return false;
}
// Update end
end--;
}
return true;
}
// Driver code
static void Main()
{
int[] arr = { 1, 10, 14, 20, 18, 12, 5 };
int N = arr.Length;
// Function Call
if (isSpiralSorted(arr, N))
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
}
// This code is contributed bydivyesh072019
Javascript
输出:
YES
时间复杂度: O(N)
辅助空间: O(1)