给定一个由N 个整数组成的数组arr[] ,任务是检查通过重复从给定数组arr[] 中删除中间元素而形成的数字序列是否已排序。如果有两个中间元素,则删除其中任何一个。
例子:
Input: arr[] = {4, 3, 1, 2, 5}
Output: Yes
Explanation:
The order of removing of elements are:
Middle element of the array = arr[2]. Removing arr[2] modifies the array to {4, 3, 2, 5}.
Middle elements of the array are arr[1] and arr[2]. Removing arr[2] modifies the array to {4, 3, 5}.
Middle element of the array is arr[1]. Removing arr[1] modifies the array to {4, 5}.
Similarly, arr[0] and arr[1] are removed from the array.
Finally, the sequence of removed array elements are {1, 2, 3, 4, 5}, which is sorted.
Input: arr[] = {5, 4, 1, 2, 3}
Output: No
朴素方法:解决给定问题的最简单方法是使用递归来生成移除数组元素的所有可能组合。对于有两个中间元素的实例,需要进行两次递归调用,一个考虑第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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live