#C10921. Finding the Peak in an Unimodal Array
Finding the Peak in an Unimodal Array
Finding the Peak in an Unimodal Array
You are given an array that first increases and then decreases (an unimodal array). Your task is to find the index of the peak element (the maximum element). In other words, find the smallest index ( i ) such that ( a_{i} ) is greater than or equal to its neighbors. For an array with one element, the answer is 0. An efficient solution involves using binary search with the strategy: while ( left < right ), compute ( mid = \lfloor (left+right)/2 \rfloor ); if ( a_{mid} < a_{mid+1} ) then the peak lies to the right, otherwise it lies on the left. This method yields a logarithmic time solution.
inputFormat
Input is read from standard input (stdin). The first line contains an integer ( n ) representing the number of elements in the array. The second line contains ( n ) space-separated integers representing the elements of the array.
outputFormat
Output the index (0-indexed) of the peak element to standard output (stdout).## sample
1
10
0