Open In App

Check whether an array can be made strictly increasing by removing at most one element

Given an array arr[] consisting of N integers, the task is to check whether it is possible to make the given array strictly increasing by removing at most one element. If it is possible to make the array strictly increasing, then print “Yes”. Otherwise, print “No”.

Examples:



Input: arr[] = {1, 1, 2}
Output: Yes
Explanation: Removing an occurrence of 1 modifies the array to {1, 2}, which is strictly increasing array.

Input: arr[] = {2, 2, 3, 4, 5, 5} 
Output: No



Approach: The given problem can be solved by traversing the array arr[] and count the indices satisfying the condition arr[i-1] >= arr[i]. Follow the steps below to solve the problem:

Below is the implementation of the above approach:




// C++ program for the above approach
 
#include <bits/stdc++.h>
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; 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 (index - 2 >= 0 && arr[index - 2] < arr[index])
        return true;
       
      // if there is no element to compare
      if(index < 0)
          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 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; 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 (index - 2 >= 0 && arr[index - 2] < arr[index])
        return true;
       
      // if there is no element to compare
      if(index < 0)
          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 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):
 
        # 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# 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; 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




<script>
 
// Javascript program for the above approach
 
// Function to find if is it possible
// to make the array strictly increasing
// by removing at most one element
function check(arr , n)
{
     
    // Stores the count of numbers that
    // are needed to be removed
    var count = 0;
 
    // Store the index of the element
    // that needs to be removed
    var index = -1;
 
    // Traverse the range [1, N - 1]
    for(i = 1; i < n; 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
 
var arr = [ 1, 2, 5, 3, 5 ];
var N = arr.length;
 
if (check(arr, N))
    document.write("Yes");
else
    document.write("No");
 
 
// This code contributed by shikhasingrajput
 
</script>

Output
Yes

Time Complexity: O(N)
Auxiliary Space: O(1)


Article Tags :