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:
- Initialize two variables, say count as 0 and index as -1, to store the count of elements needed to be removed and the index of the removed element respectively.
- Traverse the given array over the range [1, N – 1] and if the value of arr[i – 1] is at least arr[i] increment the value of count by 1 and update the value of the index as i.
- If the value of count is greater than 1, then print “No” and return.
- Otherwise, check for the following conditions:
- If the value of count is equal to 0, then print “Yes” and return.
- If the index is either equal to (N – 1) or equal to 0, then print “Yes” and return.
- Check if removing the element at index makes the arr[index – 1] < arr[index + 1], then print “Yes” and return.
- Check if removing the element at index – 1 makes the arr[index – 2] < arr[index], where index – 2 >= 0, then print “Yes” and return.
- Check if index < 0, then print “Yes” and return.
- After completing the above steps, if none of the above cases are satisfied, then print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool check( int arr[], int n)
{
int count = 0;
int index = -1;
for ( int i = 1; i < n; i++) {
if (arr[i - 1] >= arr[i]) {
count++;
index = i;
}
}
if (count > 1)
return false ;
if (count == 0)
return true ;
if (index == n - 1 || index == 1)
return true ;
if (arr[index - 1] < arr[index + 1])
return true ;
if (index - 2 >= 0 && arr[index - 2] < arr[index])
return true ;
if (index < 0)
return true ;
return false ;
}
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
class GFG{
public static boolean check( int arr[], int n)
{
int count = 0 ;
int index = - 1 ;
for ( int i = 1 ; i < n; i++)
{
if (arr[i - 1 ] >= arr[i])
{
count++;
index = i;
}
}
if (count > 1 )
return false ;
if (count == 0 )
return true ;
if (index == n - 1 || index == 1 )
return true ;
if (arr[index - 1 ] < arr[index + 1 ])
return true ;
if (index - 2 >= 0 && arr[index - 2 ] < arr[index])
return true ;
if (index < 0 )
return true ;
return false ;
}
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" );
}
}
|
Python3
def check(arr, n):
count = 0
index = - 1
for i in range ( 1 , n):
if (arr[i - 1 ] > = arr[i]):
count + = 1
index = i
if (count > 1 ):
return False
if (count = = 0 ):
return True
if (index = = n - 1 or index = = 1 ):
return True
if (arr[index - 1 ] < arr[index + 1 ]):
return True
if (arr[index - 2 ] < arr[index]):
return True
return False
if __name__ = = '__main__' :
arr = [ 1 , 2 , 5 , 3 , 5 ]
N = len (arr)
if (check(arr, N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
public static bool check( int [] arr, int n)
{
int count = 0;
int index = -1;
for ( int i = 1; i < n; i++)
{
if (arr[i - 1] >= arr[i])
{
count++;
index = i;
}
}
if (count > 1)
return false ;
if (count == 0)
return true ;
if (index == n - 1 || index == 1)
return true ;
if (arr[index - 1] < arr[index + 1])
return true ;
if (arr[index - 2] < arr[index])
return true ;
return false ;
}
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" );
}
}
|
Javascript
<script>
function check(arr , n)
{
var count = 0;
var index = -1;
for (i = 1; i < n; i++)
{
if (arr[i - 1] >= arr[i])
{
count++;
index = i;
}
}
if (count > 1)
return false ;
if (count == 0)
return true ;
if (index == n - 1 || index == 1)
return true ;
if (arr[index - 1] < arr[index + 1])
return true ;
if (arr[index - 2] < arr[index])
return true ;
return false ;
}
var arr = [ 1, 2, 5, 3, 5 ];
var N = arr.length;
if (check(arr, N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
22 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...