For an array a: a1, a2, … ak, … an, ak is a peak if and only if ak-1 ≤ ak ≥ ak+1 when 1 < k and k < n. a1 is a peak if a1 ≥ a2, and an is a peak if an-1 ≤ an. The goal is to find one peak from the array.
A divide and conquer algorithm is given as follows:
find_peak(a,low,high):
mid = (low+high)/2
if a[mid-1] <= a[mid] >= a[mid+1] return mid // this is a peak;
if a[mid] < a[mid-1]
return find_peak(a,low,mid-1) // a peak must exist in A[low..mid-1]
if a[mid] < a[mid+1]
return find_peak(a,mid+1,high) // a peak must exist in A[mid+1..high]
Why this algorithm is correct? I think it may suffer from losing half of the array in which a peak exists.
The peak element of an array can be found using the naive approach of linear search with time complexity O(N) or the optimized divide and conquer approach with time complexity O(logn).
Algorithm/Insights Linear approach: One way to solve this problem is to iterate over the array and find an element that is greater than or equal to its neighbors and return it.
Cooley–Tukey Fast Fourier Transform (FFT) algorithm is the most common algorithm for FFT. It is a divide and conquer algorithm which works in O(N log N) time.
The algorithm is correct, although it requires a bit of calculus to prove. First case is trivial → peak. Second case is a "half peak", meaning that it has the down slope, but not up.
We have 2 possibilities here:
Similar argument can be applied for the third case with the opposite "half peak".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With