cswork / OAOpen practice workspace

SalesforceOA #29

Min Deletions to Almost Sorted

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. In the original example explanation, "delete 7" should be "delete 1".

Input

The input gives n and n distinct integers. Full original bounds: 1≤n≤100000, 1≤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…