SalesforceOA #30
Min Elements to Remove to Make Almost Sorted Array
Estimated MediumJudge-ready
Topics (AI-labelled)SubsequencesBinary Search
The array elements are distinct. An array is called almost increasing if and only if deleting at most one more element makes it strictly increasing. Find the minimum number of elements to delete from the given array so that the remaining array is almost increasing. The original text mistakenly joined [13] and [9,2] into [13,9,2] and called it valid; this is corrected according to the full definition and the identical problem 29: three decreasing items are not almost increasing.
Input
The input gives n and n distinct integers. The original gives no numeric bounds; this site adds 1≤n≤100000, −10^9≤a[i]≤10^9.
Output
Output one integer answer.
Example 1
Input:
5
3 4 2 5 1
Output:
1
Example 2
Input:
3
4 2 1
Output:
1
Example 3
Input:
1
13
Output:
0
Example notes
The outputs of the three public examples are, in order: 1; 1; 0. They have been checked by independent enumeration or direct simulation.
Editorial
Loading solution…