#C14108. Peak Element Finder
Peak Element Finder
Peak Element Finder
You are given an array of integers. A peak element is defined as an element that is strictly greater than its neighbors. For corner elements, we only consider one neighbor. Your task is to find the index of one peak element in the array using an algorithm with an expected time complexity of \(O(\log n)\).
If there are multiple peaks, the binary search approach may return any one of them. In particular, if the array contains multiple peaks, the answer may not be the first occurrence from the left. If the array is empty, return \(-1\).
Input Details: The array is described as follows. The first line contains an integer \(n\) indicating the number of elements in the array. If \(n > 0\), the second line contains \(n\) space-separated integers representing the elements of the array.
Output Details: Output a single integer representing the index of a peak element (using 0-based indexing), or \(-1\) if no peak exists.
inputFormat
The first line contains a single integer \(n\) (\(n \ge 0\)) representing the number of elements in the array. If \(n > 0\), the second line contains \(n\) space-separated integers.
outputFormat
Print a single integer: the index of a peak element. If the array is empty, print \(-1\).
## sample1
1
0