#K42157. Find Peak Element

    ID: 27025 Type: Default 1000ms 256MiB

Find Peak Element

Find Peak Element

You are given an array of integers. Your task is to find the index of a peak element. A peak element is defined as an element that is strictly greater than its neighbors. For the first and last element, we consider only one neighbor. If the array contains multiple peaks, returning the index of any one of them is acceptable.

The solution must run in O(log n) time which implies a binary search approach.

Additionally, be cautious with special cases where the array has only one element, two elements, or a plateau (i.e., adjacent equal elements). For plateau cases, return any index corresponding to one of the plateau positions that satisfies the peak condition.

Example:

  • Input: [1, 2, 3, 1] - Output: 2
  • Input: [1, 2, 1, 3, 5, 6, 4] - Output: 5 (or any valid peak index)

Note: Ensure that your solution reads from standard input and writes to standard output.

inputFormat

The first line of input contains an integer n denoting the number of elements in the array.

The second line contains n space-separated integers representing the elements of the array.

outputFormat

Output a single integer representing the index of one peak element found in the array.

## sample
4
1 2 3 1
2