Python Program to find the peak value in an array | Binary Search

Given a list of distinct integers that increases and then decreases, find the peak value in that list (without using any builtin function)


(Note:There is only one peak)

Key Idea :

Example: Let the array be [2, 3, 4, 6, 9, 12, 13, 8, 6, 4, 1]. Then we must return the value 13 as the peak.

We can use binary search in this problem.

We can check if the ("mid value" is greater its previous index's value), if that's so then we will store the mid value and change our "left" to "mid +1" and then continue the operation in hope of getting a better value.

If it's not greater than previous value , then we can reduce our "right" to "mid-1". Finally we will return our answer.

Program :

Output :

Comments