📌  相关文章
📜  检查是否可以通过删除最多一个元素来使数组严格递增

📅  最后修改于: 2021-09-04 09:31:08             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是检查是否可以通过删除最多一个元素来使给定数组严格递增。如果可以使数组严格递增,则打印“Yes” 。否则,打印“否”

例子:

方法:可以通过遍历数组arr[]并计算满足条件arr[i-1] ≥ arr[i]的索引来解决给定的问题。请按照以下步骤解决问题:

  • 初始化两个变量,比如count0index-1 ,分别存储需要删除的元素的计数和删除元素的索引。
  • [1, N – 1]范围内遍历给定数组,如果arr[i – 1] 的值至少为arr[i] ,则将count的值增加1并将索引的值更新为i
  • 如果count的值大于1 ,则 打印“否”并返回
  • 否则,请检查以下条件:
    • 如果count的值等于0 ,则打印“Yes”并返回
    • 如果索引等于(N – 1)或等于0 ,则打印“是”并返回。
    • 检查删除索引处的元素是否使arr[index – 1] < arr[index + 1] ,然后打印“是”并返回
    • 检查删除index – 1处的元素是否使arr[index – 2] < arr[index] ,然后打印“Yes”并返回
  • 完成上述步骤后,如果以上情况都不满足,则打印“No”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find if is it possible
// to make the array strictly increasing
// by removing at most one element
bool check(int arr[], int n)
{
    // Stores the count of numbers that
    // are needed to be removed
    int count = 0;
 
    // Store the index of the element
    // that needs to be removed
    int index = -1;
 
    // Traverse the range [1, N - 1]
    for (int i = 1; i < n - 1; i++) {
 
        // If arr[i-1] is greater than
        // or equal to arr[i]
        if (arr[i - 1] >= arr[i]) {
 
            // Increment the count by 1
            count++;
 
            // Update index
            index = i;
        }
    }
 
    // If count is greater than one
    if (count > 1)
        return false;
 
    // If no element is removed
    if (count == 0)
        return true;
 
    // If only the last or the
    // first element is removed
    if (index == n - 1 || index == 1)
        return true;
 
    // If a[index] is removed
    if (arr[index - 1] < arr[index + 1])
        return true;
 
    // If a[index - 1] is removed
    if (arr[index - 2] < arr[index])
        return true;
 
    return false;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 5, 3, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    if (check(arr, N))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to find if is it possible
// to make the array strictly increasing
// by removing at most one element
public static boolean check(int arr[], int n)
{
     
    // Stores the count of numbers that
    // are needed to be removed
    int count = 0;
 
    // Store the index of the element
    // that needs to be removed
    int index = -1;
 
    // Traverse the range [1, N - 1]
    for(int i = 1; i < n - 1; i++)
    {
         
        // If arr[i-1] is greater than
        // or equal to arr[i]
        if (arr[i - 1] >= arr[i])
        {
             
            // Increment the count by 1
            count++;
 
            // Update index
            index = i;
        }
    }
 
    // If count is greater than one
    if (count > 1)
        return false;
 
    // If no element is removed
    if (count == 0)
        return true;
 
    // If only the last or the
    // first element is removed
    if (index == n - 1 || index == 1)
        return true;
 
    // If a[index] is removed
    if (arr[index - 1] < arr[index + 1])
        return true;
 
    // If a[index - 1] is removed
    if (arr[index - 2] < arr[index])
        return true;
 
    return false;
}
 
// Driver Code
public static void main(String args[])
{
    int []arr = { 1, 2, 5, 3, 5 };
    int N = arr.length;
     
    if (check(arr, N))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by SoumikMondal


Python3
# Python3 program for the above approach
 
# Function to find if is it possible
# to make the array strictly increasing
# by removing at most one element
def check(arr, n):
   
    # Stores the count of numbers that
    # are needed to be removed
    count = 0
 
    # Store the index of the element
    # that needs to be removed
    index = -1
 
    # Traverse the range [1, N - 1]
    for i in range(1, n - 1):
 
        # If arr[i-1] is greater than
        # or equal to arr[i]
        if (arr[i - 1] >= arr[i]):
            # Increment the count by 1
            count += 1
 
            # Update index
            index = i
 
    # If count is greater than one
    if (count > 1):
        return False
 
    # If no element is removed
    if (count == 0):
        return True
 
    # If only the last or the
    # first element is removed
    if (index == n - 1 or index == 1):
        return True
 
    # If a[index] is removed
    if (arr[index - 1] < arr[index + 1]):
        return True
 
    # If a[index - 1] is removed
    if (arr[index - 2] < arr[index]):
        return True
 
    return False
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 5, 3, 5]
    N = len(arr)
    if (check(arr, N)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by mohitkumar 29.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find if is it possible
// to make the array strictly increasing
// by removing at most one element
public static bool check(int[] arr, int n)
{
     
    // Stores the count of numbers that
    // are needed to be removed
    int count = 0;
 
    // Store the index of the element
    // that needs to be removed
    int index = -1;
 
    // Traverse the range [1, N - 1]
    for(int i = 1; i < n - 1; i++)
    {
         
        // If arr[i-1] is greater than
        // or equal to arr[i]
        if (arr[i - 1] >= arr[i])
        {
             
            // Increment the count by 1
            count++;
 
            // Update index
            index = i;
        }
    }
 
    // If count is greater than one
    if (count > 1)
        return false;
 
    // If no element is removed
    if (count == 0)
        return true;
 
    // If only the last or the
    // first element is removed
    if (index == n - 1 || index == 1)
        return true;
 
    // If a[index] is removed
    if (arr[index - 1] < arr[index + 1])
        return true;
 
    // If a[index - 1] is removed
    if (arr[index - 2] < arr[index])
        return true;
 
    return false;
}
 
// Driver Code
public static void Main(string[] args)
{
    int[] arr = { 1, 2, 5, 3, 5 };
    int N = arr.Length;
 
    if (check(arr, N))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by ukasp


Javascript


输出:
Yes

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

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