Find Peak Element
mediumBinary Search+20 XPTime limit 2s
The array is bitonic: it strictly increases up to one highest value (the peak) and then strictly decreases. Either side may be empty. Print the index of the peak.
Input:
- line 1:
n - line 2:
ndistinct integers
Output: the 0-based index of the peak.
Constraints: 1 ≤ n ≤ 10⁵, −10⁴ ≤ a[i] ≤ 10⁴.
Think about it: a linear scan works — but you can do O(log n). Look at a[mid] and a[mid + 1]: if the array is still going up, the peak is to the right; otherwise it is at mid or to the left. (The same idea finds a peak in any array where neighbours differ.)
Sample input 1
4 1 2 3 1
Sample output 1
2
Sample input 2
7 1 2 3 5 6 4 0
Sample output 2
4
📖 Stuck? Learn the pattern
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).