Given n integers, arranged in a circle, show an efficient algorithm that can find one peak. A peak is a number that is not less than the two numbers next to it.
One way is to go through all the integers and check each one to see whether it is a peak. That yields O(n) time. It seems like there should be some way to divide and conquer to be more efficient though.
Here’s a recursive
O(log n)algorithm.Suppose we have an array of numbers, and we know that the middle number of that segment is no smaller than the endpoints:
for i,j indexes into an array, and
m=(i+j)/2. Examine the elements midway between the endpoints and the midpoint, i.e. those at indexesx=(3*i+j)/4andy=(i+3*j)/4. IfA[x]>=A[m], then recurse on the interval[i,m]. IfA[y]>=A[m], then recurse on the interval[m,j]. Otherwise, recurse on the interval[x,y].In every case, we maintain the invariant on the interval above. Eventually we get to an interval of size 2 which means we’ve found a peak (which will be
A[m]).To convert the circle to an array, take 3 equidistant samples and orient yourself so that the largest (or one tied for the largest) is in the middle of the interval and the other two points are the endpoints. The running time is
O(log n)because each interval is half the size of the previous one.I’ve glossed over the problem of how to round when computing the indexes, but I think you could work that out successfully.