> For the complete documentation index, see [llms.txt](https://gl01.gitbook.io/gfg-editorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gl01.gitbook.io/gfg-editorials/2024/05-2024-may-4/18-find-the-highest-number.md).

# 18. Find the Highest number

The problem can be found at the following link: [Question Link](https://www.geeksforgeeks.org/problems/find-the-highest-number2259/1)

## 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++)

```cpp
class Solution {
public:
    int findPeakElement(vector<int>& a) 
    {
        int size=a.size();
        int low=0, high=size-1;
        while (low<=high)
        {
            int mid=(low+high)/2;
            if (a[mid]>a[mid-1] && a[mid]>a[mid+1])
                return a[mid];
            else if (a[mid]>a[mid-1] && a[mid]<a[mid+1])
                low=mid+1;
            else high=mid-1;
        }
        return a[size-1];
    }
};
```

## Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). 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](https://github.com/getlost01/gfg-potd) repository.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gl01.gitbook.io/gfg-editorials/2024/05-2024-may-4/18-find-the-highest-number.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
