> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/162-find-peak-element.md).

# 162 Find Peak Element

A peak element is an element that is greater than its neighbors.

Given an input array where`num[i] ≠ num[i+1]`, find a peak element and return its index.

The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.

You may imagine that`num[-1] = num[n] = -∞`.

For example, in array`[1, 2, 3, 1]`, 3 is a peak element and your function should return the index number 2.

**The Idea:** Binary search until we converge onto a peak. We can detect whether we are on either the left or right side of the peak. On the left side of a peak, the element to the right is greater. On the left, the opposite is true. If we are on the left side, we want to move right, and on the right left we want to conversely move left. Eventually we will converge to the top of *some* local peak.

**Complexity:** O(logn) time and space

```python
class Solution:
    def findPeakElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        return self.b_search(0, len(nums) - 1, nums)

    def b_search(self, left, right, nums):
        if left == right:
            return right # or left
        else:
            mid1 = int(left + (right - left) / 2)
            mid2 = mid1 + 1

        # left end of the peak -> move right
        if (nums[mid1] < nums[mid2]):
            return self.b_search(mid1 + 1, right, nums)

        # otherwise on the right end of the peak
        else:
            return self.b_search(left, mid1, nums)
```


---

# 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, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/162-find-peak-element.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
