📌  相关文章
📜  检查是否可以通过仅选择角数组元素来对数组进行排序

📅  最后修改于: 2021-09-03 04:15:00             🧑  作者: Mango

给定一个由N 个元素组成的数组arr[] ,任务是检查给定的数组是否可以通过只选择角元素来排序,即可以选择数组左侧或右侧的元素。

例子:

方法:解决这个问题,需要用到一个类似于Bitonic Sequence的概念。按照下面的步骤来解决这个问题:

  • 遍历数组并检查数组元素的序列是否递减,即如果下一个元素小于前一个元素,则所有剩余元素也应递减或相等。
  • 也就是说,如果序列是非递增非递减或非递减后跟非递增,则只有这样才能按给定的操作对数组进行排序。

以下是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if an array can
// be sorted using given operations
bool check(int arr[], int n)
{
    int i, g;
    g = 0;
    for (i = 1; i < n; i++) {
 
        // If sequence becomes increasing
        // after an already non-decreasing to
        // non-increasing pattern
        if (arr[i] - arr[i - 1] > 0 && g == 1)
            return false;
 
        // If a decreasing pattern is observed
        if (arr[i] - arr[i - 1] < 0)
            g = 1;
    }
    return true;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 2, 3, 4, 10, 4, 3, 1 };
    int n = sizeof(arr) / sizeof(int);
    if (check(arr, n) == true)
        cout << "Yes"
                "\n";
    else
        cout << "No"
             << "\n";
 
    return 0;
}


Java
// Java program to implement
// the above approach
class GFG{
 
// Function to check if an array can
// be sorted using given operations
static boolean check(int arr[], int n)
{
    int i, g;
    g = 0;
 
    for(i = 1; i < n; i++)
    {
         
        // If sequence becomes increasing
        // after an already non-decreasing to
        // non-increasing pattern
        if (arr[i] - arr[i - 1] > 0 && g == 1)
            return false;
 
        // If a decreasing pattern is observed
        if (arr[i] - arr[i - 1] < 0)
            g = 1;
    }
    return true;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 2, 3, 4, 10, 4, 3, 1 };
    int n = arr.length;
     
    if (check(arr, n) == true)
    {
        System.out.println("Yes");
    } else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 program to implement
# the above approach
 
# Function to check if an array can
# be sorted using given operations
def check(arr, n):
 
    g = 0
     
    for i in range(1, n):
 
        # If sequence becomes increasing
        # after an already non-decreasing to
        # non-increasing pattern
        if(arr[i] - arr[i - 1] > 0 and g == 1):
            return False
 
        # If a decreasing pattern is observed
        if(arr[i] - arr[i] < 0):
            g = 1
 
    return True
 
# Driver Code
arr = [ 2, 3, 4, 10, 4, 3, 1 ]
n = len(arr)
 
if(check(arr, n) == True):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to check if an array can
// be sorted using given operations
static bool check(int []arr, int n)
{
    int i, g;
    g = 0;
 
    for(i = 1; i < n; i++)
    {
         
        // If sequence becomes increasing
        // after an already non-decreasing to
        // non-increasing pattern
        if (arr[i] - arr[i - 1] > 0 && g == 1)
            return false;
 
        // If a decreasing pattern is observed
        if (arr[i] - arr[i - 1] < 0)
            g = 1;
    }
    return true;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 2, 3, 4, 10, 4, 3, 1 };
    int n = arr.Length;
     
    if (check(arr, n) == true)
    {
        Console.WriteLine("Yes");
    } else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
Yes

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

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