SDE Path

Find Peak Element

Medium

Find Peak Element

Find an index whose value is greater than both its neighbors, in O(log N).

Input format

The first line contains N. The second line contains N integers that strictly increase and then strictly decrease (either part may be empty), so there is exactly one peak.

Output format

Print the 0-based index of the peak element.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
5
1 3 8 4 2
Expected output
2
Example 2
Input
3
1 2 3
Expected output
2