• + 3 comments

    I hope this explanation helps you to understand algorithm.

    If we draw a[]'s value into 'line graph', then the graph will looks like a mountain. (each a[n]'s value will indicate the height.)

    Imagine the mountain tracking from left to right, and 'max' stores the maximum height.

    starting point 'p' means go up / end point 'q+1' means go down. then, just adding p and q+1, we can know the current height(which is 'x'). and what we need to do is just find the maximum height. image

    (oh, this website's image upload is very bad..)