#C2150. Find First Peak Element

    ID: 45435 Type: Default 1000ms 256MiB

Find First Peak Element

Find First Peak Element

You are given an array of integers. An element is called a peak element if it is strictly greater than its neighbors. More formally, an element at index (i) (0-indexed) is a peak if

[ \text{arr}[i] > \text{arr}[i-1] \quad (\text{if } i > 0) \quad \text{and} \quad \text{arr}[i] > \text{arr}[i+1] \quad (\text{if } i < n-1)]

where (n) is the length of the array. If the array has only one element, that element is considered a peak. Your task is to find a peak element using a binary search algorithm with an expected time complexity of (O(\log n)). If no peak exists, output (-1).

Note: In some arrays, due to equal adjacent elements, no valid peak may exist. In that case, you should return (-1).

inputFormat

The input is read from standard input (stdin) and consists of two lines:

  1. The first line contains an integer (n) representing the number of elements in the array.
  2. The second line contains (n) space-separated integers representing the array elements.

outputFormat

Output a single integer to standard output (stdout) which is the value of the found peak element. If no peak exists, output (-1).## sample

6
1 3 20 4 1 0
20