Remove One Element to Make the Array Strictly Increasing
When we traverse the list and find a number which is not larger than the previous one, we have two options: either remove the current number, or the previous number. We should always prefer removing the current number, because the number before the previous one might be less than the current number. Therefore, we remove the previous number only when it is not less than the current one.
We use a variable removed
to record if we have removed a number. If we find a second number that requires removal, we return False. If we traverse the whole list without finding a second removal, we return True.
Here is the Python code that represents this approach:


This code checks if a number is less than or equal to the previous one. If yes, we need to consider removing a number. If we have already removed a number (tracked by the variable removed
), we return False because we can only remove exactly one number. Otherwise, we check if the current number is less than or equal to the number before the previous one, if yes, we remove the previous one by replacing it with the current one. Finally, we mark removed
as True.
In each iteration, the operation is done in constant time, hence the total time complexity of the solution is O(n), where n is the length of nums
. The space complexity is O(1) because we only use a few variables to track the information.
The basic building blocks are Neighbor Comparison and Flagging.
In this problem, we are using a “Neighbor Comparison” mechanism where we are constantly comparing an element with its previous neighbor in the array. This helps us detect any anomalies in the strictly increasing order.
The “Flagging” mechanism comes into play when we encounter an anomaly. The flag (in this case, the removed
variable) is used to keep track if we have removed an element or not. If we have already removed an element and we encounter another anomaly, we know that we cannot make the array strictly increasing by removing exactly one element, so we return False.
These building blocks are fundamental in solving a wide range of arraybased problems. Understanding and recognizing when to use them is crucial for problemsolving in coding interviews or competitive programming.
Q&A
Can someone tell me why this code in C++ is showing time limit exceeded it is O(n) then also ?


Although your code seems to have a linear complexity O(n) due to the single loop, it actually has a time complexity of O(n^2) because of the nums.erase(nums.begin() + i);
operation.
In C++, erasing an element from a vector involves shifting all the elements after it to fill the gap, which is an O(n) operation. So when you’re doing this inside a loop that also runs in O(n), your overall time complexity becomes O(n^2).
That’s why your code is exceeding the time limit, it’s not as efficient as it seems at first glance.
A better approach would be to not alter the original array by erasing elements, but instead count the number of anomalies (pairs of elements where nums[i] >= nums[i+1]
) and also check if it’s possible to avoid each anomaly by comparing nums[i1]
and nums[i+1]
. This will result in an O(n) time complexity without the hidden costs.
We iterate over the array, count anomalies, and check if they can be fixed by changing either the current or the previous number.


This code increments anomalyCount
whenever it finds a pair of numbers out of order. It tries to fix the anomaly by replacing the current number with the previous one if the one before the previous is also larger than the current. Otherwise, it replaces the previous number with the current. At the end, it checks if there was more than one anomaly, returning false
if so, true
otherwise. This guarantees that we only change one number at most.
I wrote a code in c++ and this is showing runtime error if anyone can tell why it is showing runtime error. It will be very helpful for me my code :


The runtime error in your code is likely due to the usage of sizeof(nums)
. In C++, sizeof
doesn’t give you the size of the vector, it gives you the size of the vector object itself, which is not what you want. You should use nums.size()
instead to get the number of elements in the vector.
Additionally, your condition inside the loop seems off, you’re checking nums[i] > nums[i+1]
while your loop runs till i < size1
, this would cause an outofbounds error.
Here is the corrected version of your code:


But please note, this code won’t work as expected for the problem statement. The problem requires that you ensure the sequence can be strictly increasing by modifying at most one element. The condition inside your forloop doesn’t cater to this requirement. You should consider the explanation given earlier for a correct approach.