📌  相关文章
📜  检查从数组中删除的中间元素的序列是否已排序

📅  最后修改于: 2021-05-18 00:23:42             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是检查通过从给定数组arr []中重复删除中间元素形成的数字序列是否已排序。如果有两个中间元素,则删除其中任何一个。

例子:

天真的方法:解决给定问题的最简单方法是使用递归来生成删除数组元素的所有可能组合。对于具有两个中间元素的实例,需要进行两个递归调用,一个递归调用考虑第N / 2元素,另一个考虑第(N / 2 + 1)元素。完成递归后,检查由任何递归调用组成的数组是否已排序。如果发现是真的,则打印“”。否则,打印“否”

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

高效方法:可以基于以下观察条件对上述方法进行优化:观察到数组末尾的元素必须大于所有数组元素,才能获得越来越排序的数组。
请按照以下步骤解决问题:

  • 使用“是”初始化变量ans ,以检查所需的序列是否可以排序。
  • 初始化两个指针,将L表示0 ,将R表示(N – 1) ,以存储数组的开始索引和结束索引。
  • 迭代直到L小于R并执行以下步骤:
    • 如果arr [L]的值大于或等于arr [L + 1]arr [R – 1]的最大值,并且arr [R]的值大于或等于arr [L ]的最小值+ 1]arr [R – 1] ,然后将L的值增加1,并将R的值减少1
    • 否则,将ans的值更新为“ No”并退出循环。
  • 完成上述步骤后,输出ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if sequence of
// removed middle elements from an
// array is sorted or not
bool isSortedArray(int arr[], int n)
{
    // Points to the ends
    // of the array
    int l = 0, r = (n - 1);
 
    // Iterate l + 1 < r
    while ((l + 1) < r) {
 
        // If the element at index L and
        // R is greater than (L + 1)-th
        // and (R - 1)-th elements
        if (arr[l] >= max(arr[l + 1],
                          arr[r - 1])
            && arr[r] >= max(arr[r - 1],
                             arr[l + 1])) {
 
            // If true, then decrement R
            // by 1 and increment L by 1
            l++;
            r--;
        }
 
        // Otherwise, return false
        else {
            return false;
        }
    }
 
    // If an increasing sequence is
    // formed, then return true
    return true;
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 3, 1, 2, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    if (isSortedArray(arr, N))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
  
// Function to check if sequence of
// removed middle elements from an
// array is sorted or not
static boolean isSortedArray(int []arr, int n)
{
     
    // Points to the ends
    // of the array
    int l = 0, r = (n - 1);
 
    // Iterate l + 1 < r
    while ((l + 1) < r)
    {
         
        // If the element at index L and
        // R is greater than (L + 1)-th
        // and (R - 1)-th elements
        if (arr[l] >= Math.max(arr[l + 1],
                               arr[r - 1]) &&
            arr[r] >= Math.max(arr[r - 1],
                               arr[l + 1]))
        {
             
            // If true, then decrement R
            // by 1 and increment L by 1
            l++;
            r--;
        }
 
        // Otherwise, return false
        else
        {
            return false;
        }
    }
 
    // If an increasing sequence is
    // formed, then return true
    return true;
}
 
// Driver Code
public static void main(String args[])
{
    int []arr = { 4, 3, 1, 2, 5 };
    int N = arr.length;
 
    if (isSortedArray(arr, N))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by bgangwar59


Python3
# Python3 program for the above approach
 
# Function to check if sequence of
# removed middle elements from an
# array is sorted or not
def isSortedArray(arr, n):
 
    # Points toa the ends
    # of the array
    l = 0
    r = (n - 1)
 
    # Iterate l + 1 < r
    while ((l + 1) < r):
 
        # If the element at index L and
        # R is greater than (L + 1)-th
        # and (R - 1)-th elements
        if (arr[l] >= max(arr[l + 1],
                          arr[r - 1])
            and arr[r] >= max(arr[r - 1],
                              arr[l + 1])):
 
            # If true, then decrement R
            # by 1 and increment L by 1
            l += 1
            r -= 1
 
        # Otherwise, return false
        else:
            return False
 
    # If an increasing sequence is
    # formed, then return true
    return True
 
# Driver Code
if __name__ == "__main__":
 
    arr = [ 4, 3, 1, 2, 5 ]
    N = len(arr)
 
    if (isSortedArray(arr, N)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by ukasp


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if sequence of removed
// middle elements from an array is sorted or not
static bool isSortedArray(int []arr, int n)
{
     
    // Points to the ends
    // of the array
    int l = 0, r = (n - 1);
 
    // Iterate l + 1 < r
    while ((l + 1) < r)
    {
         
        // If the element at index L and
        // R is greater than (L + 1)-th
        // and (R - 1)-th elements
        if (arr[l] >= Math.Max(arr[l + 1],
                               arr[r - 1]) &&
            arr[r] >= Math.Max(arr[r - 1],
                               arr[l + 1]))
        {
             
            // If true, then decrement R
            // by 1 and increment L by 1
            l++;
            r--;
        }
 
        // Otherwise, return false
        else
        {
            return false;
        }
    }
 
    // If an increasing sequence is
    // formed, then return true
    return true;
}
 
// Driver Code
public static void Main(string[] args)
{
    int []arr = { 4, 3, 1, 2, 5 };
    int N = arr.Length;
 
    if (isSortedArray(arr, N))
       Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by sanjoy_62


Javascript
// JavaScript program for the above approach
 
// Function to check if sequence of
// removed middle elements from an
// array is sorted or not
function isSortedArray(arr, n){
 
    // Points toa the ends
    // of the array
    var l = 0
    var r = (n - 1)
 
    // Iterate l + 1 < r
    while ((l + 1) < r) {
 
        // If the element at index L and
        // R is greater than (L + 1)-th
        // and (R - 1)-th elements
        if (arr[l] >= Math.max(arr[l + 1],
                          arr[r - 1])
            && arr[r] >= Math.max(arr[r - 1],
            arr[l + 1])){
 
            // If true, then decrement R
            // by 1 and increment L by 1
            l += 1
            r -= 1
    }
 
        // Otherwise, return false
        else
            return false
    }
 
    // If an increasing sequence is
    // formed, then return true
    return true
}
 
// Driver Code
 
var arr = [ 4, 3, 1, 2, 5 ]
var N = arr.length
 
if (isSortedArray(arr, N))
      console.log("Yes")
else
      console.log("No")
 
// This code is contributed by AnkThon


输出:
Yes

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