📌  相关文章
📜  通过仅选择角落的Array元素来检查Array是否可以排序

📅  最后修改于: 2021-05-07 18:38:03             🧑  作者: 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


输出:
Yes


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