For an array a: a1, a2, … ak, … an, ak is a peak if and only if ak-1 ≤ ak ≥
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".