Given an array of numbers, find a peak element. A peak element is one which is not smaller than its neighbours.
Best Case: Ω(1)
Average Case: θ(log n)
Worst Case: O(log n)
where n represents the size of the array.
Worst Case: O(1)
Finds a peak element in the array. The function find_peak
has the following parameter(s):
a
: a vector of integers
Return value: an integer representing the zero-based indexing position of a peak element.
The first line contains an integer n
, the size of array.
The next line contains n
space-separated integers a[i]
where 0 ≤ i
< n
.
An integer representing the zero-based indexing position of a peak element.
5
0 1 2 3 4
4
Possible peak: 4
Respective position: 4
Since the array is sorted, 4 is the only peak possible.
5
3 1 2 5 4
3
Possible peaks: 3, 5
Respective positions: 0, 3
Any of the above outputs are valid.