All problems

Problem 662

Partition Array into Disjoint Intervals

Medium · 20 pts · Array, Prefix Sum

Problem

Split the array into a non-empty left part and a non-empty right part, so that every element of the left part is less than or equal to every element of the right part. Among all valid splits, find the smallest possible length of the left part. A valid split always exists in the input. Precompute the minimum of every suffix; then the left part can end at index i exactly when the maximum of A[0..i] is at most the minimum of A[i+1..N-1].

Input

The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1]. A valid split always exists.

Output

Print the smallest possible length of the left part.

Constraints

2 <= N <= 100000; 0 <= A[i] <= 10^6

Example input

5
5 0 3 8 6

Example output

3

Explanation

In 5 0 3 8 6, the left part 5 0 3 has maximum 5 and the right part 8 6 has minimum 6, so the split is valid. Shorter left parts fail because 5 is larger than 0 or 3, so the answer is 3.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Minimum Domino Rotations For Equal Row Next: Maximum Score From Removing Stones