# Find the Peak

There is an integer array which has the following features:

* The numbers in adjacent positions are different
* A\[0] < A\[1]  && A\[A.length - 2]  > A\[A.length - 1]

We define a position P is a peak if:

```
A[P] > A[P-1] && A[P] > A[P+1]
```

Find a peak element in this array. Return the index of the peak.

## Example

Given`[1, 2, 1, 3, 4, 5, 7, 6]`

Return index`1`(which is number 2) or`6`(which is number 7)

## Note

A\[0] < A\[1] && A\[A.length - 2] > A\[A.length - 1] 说明至少有一个峰

## ![](/files/-LSLueFBVY0_vd0lrs4l)

对于一个不是中间的数,有如下四种情况，第一种直接返回, 其他情况,保留先增后减的一半.

如果中间的数比后一位数大的话，peek点肯定在mid左边或是mid

如果中间的数比前一位数小的话，peek点肯定在mid右边或是mid

最后一种情况代表其他case，无所谓在哪边了

`start = 1, end = len - 2`

## Code

```java
public class Solution {
    /*
     * @param A: An integers array.
     * @return: return any of peek positions.
     */
    public int findPeak(int[] A) {
        // write your code here
        int start = 1, end = A.length - 2;
        while (start + 1 < end) {
            int mid = start + (end - start) / 2;
            if (A[mid] > A[mid - 1] && A[mid] > A[mid + 1]) {
                return mid;
            } else if (A[mid] < A[mid - 1]) {
                end = mid;
            } else if (A[mid] < A[mid + 1]) {
                start = mid;
            } else {
                end = mid;
            }
        }
        return A[start] > A[end] ? start : end;
    }
}
```


---

# Agent Instructions: 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://luj.gitbook.io/code/binary-search/find-the-peak.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.
