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

# 162. Find Peak Element

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.

click to show spoilers.

Note: Your solution should be in logarithmic complexity.

Credits:Special thanks to @ts for adding this problem and creating all test cases.

## Solution <a href="#solution" id="solution"></a>

```java
public class Solution {
    public int findPeakElement(int[] nums) {
        if (nums == null || nums.length < 2) return 0;
        if (nums[0] > nums[1]) return 0;
        if (nums[nums.length - 1] > nums[nums.length - 2]) return nums.length - 1;


        int start = 1;
        int end = nums.length - 2;

        while (start <= end) {
            int middle = start + (end - start) / 2;
            if (nums[middle] > nums[middle + 1] && nums[middle] > nums[middle - 1]) return middle;

            if (nums[middle - 1] > nums[middle]) {
                end = middle - 1;
                continue;
            }

            if (nums[middle + 1] > nums[middle]) {
                start = middle + 1;
                continue;
            }
        }

        return 0;
    }
}
```
