#C9369. Find a Peak Element
Find a Peak Element
Find a Peak Element
You are given an array of integers. A peak element is defined as an element that is strictly greater than its immediate neighbors. If the array is empty, the answer is defined as -1, and if there is only one element, that element is considered a peak.
Your task is to find any peak element from the array using an algorithm with time complexity better than O(n), hinting at a binary search approach.
Note: It is guaranteed that the input will be given in the following format: the first number is an integer n representing the number of elements in the array. If n > 0, the next line contains n space-separated integers. Use standard input (stdin) for reading input and standard output (stdout) for printing the result.
For example:
Input: 6 1 3 20 4 1 0</p>Output: 20
inputFormat
The first line contains a single integer n
(n ≥ 0), which is the number of elements in the list. If n > 0
, the second line contains n
space-separated integers representing the array elements.
outputFormat
Print a single integer – one peak element from the array. If the list is empty, print -1.
## sample6
1 3 20 4 1 0
20