# Longest Peak

![](/files/KMTy6Ew2J6c4zbAuRRb9)

* N, 1

```jsx
function longestPeak(arr) {
  arr = [arr[0], ...arr, arr[arr.length - 1]]; // create bumper on before and after this arr

  let max = 0;
  let isPeak = false;
  
  let count = 0;
  for (let i = 1; i < arr.length - 1; i++) {
    const prev = arr[i - 1];
    const curr = arr[i];
    const next = arr[i + 1];

    const isPeak = prev < curr && curr > next;

    if (isPeak === false) continue;

    let leftIdx = i;
    while (leftIdx >= 1 && arr[leftIdx - 1] < arr[leftIdx]) {
      leftIdx--;
    }
    let rightIdx = i;
    while (rightIdx < arr.length - 1 && arr[rightIdx] > arr[rightIdx + 1]) {
      rightIdx++;
    }
    
    const currentPeak = rightIdx - leftIdx + 1;
    max = Math.max(max, currentPeak);
  }

  return max;
}

// Do not edit the line below.
exports.longestPeak = longestPeak;
```


---

# 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://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/longest-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.
