01. Peak element
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
I used binary search to find the peak element. This method relies on the fact that there's a point where the sequence starts increasing and then starts decreasing.
We initialize two pointers, left
and right
, representing the start and end of the array, respectively.
We then enter a loop where we calculate the middle index.
If the middle element is less than its adjacent element to the right, it means the peak element must be on the right side of the middle element. So, we move left
to mid + 1
.
Otherwise, if the middle element is greater than or equal to its adjacent element to the right, it means the peak element must be on the left side of the middle element or could be the middle element itself. So, we move right
to mid
.
We keep doing this until left
is less than right
, at which point left
will be pointing to the peak element.
Time Complexity: The time complexity of the binary search approach is O(log n)
, where n is the number of elements in the array.
Auxiliary Space Complexity: The space complexity is O(1)
as we are using only a constant amount of extra space.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.