18. Find the Highest number
The problem can be found at the following link: Question Link
My Approach
Initialize size as the size of the input vector a.
Set low to 0 and high to size - 1.
Enter a while loop that continues as long as low <= high.
Calculate mid as (low + high) / 2.
If a[mid] is greater than a[mid-1] and a[mid+1], return a[mid].
If a[mid] is greater than a[mid-1] but less than a[mid+1], set low to mid + 1.
Otherwise, set high to mid - 1.
If no peak element is found, return a[size - 1].
Time and Auxiliary Space Complexity
Time Complexity: The time complexity of this approach is
O(logN)
, where N is the number of elements in the array.Auxiliary Space Complexity: The auxiliary space complexity is
O(1)
.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. 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 getlost01/gfg-potd repository.
Last updated