The left side of peak element is in increasing order and right side is in decreasing order
Intuition
A revised binary search based on few invariant conditions.
The peak element has property that it is greater on both sides, so we try to check if the mid element is largest available in given window. If the condition is satisfied then we return the max element in the given window.
Else, we let the loop complete and at the time of completion, the start and end would be pointing to same index. The given array misses all the conditions we placed in while loop for finding max, which means, the current index is the max
Implementation
When we don't have target to search for, avoid using <= in while loop
There is a window in which we need to check if the given
mid-1 | mid | mid+1 |
Since the array is rotated, we try to check if the element on the left side is greater
Invariant conditions
Check for avoiding ArrayOutOfBounds and checking if the left side element is greater. If it is greater, then that's the maximum element
And if the max element is not found in the iteration, then we need to decrease the search space and check
As we have checked if there are any max elements are present on the left side, and they are not found, check towards the right. Here the nums[start]<=nums[end] the <= the operator is used as the mid has already been checked for if it is the maximum element with the above invariant conditions