Find Peak Element
You get an array where no two neighbours are equal. A peak is a number bigger than its neighbours; past both ends there is −∞. Return the index of any peak in O(log n) time. Our example: [2, 5, 9, 4, 3, 6, 8, 1] — 9 at index 2 and 8 at index 6 are peaks; the run returns 2.
- input
nums: integers, no two neighbours equal. Past both ends ofnumsis minus infinity.- returns
- The index of a peak: a number greater than both its neighbours. If there are several peaks, the index of any of them. LeetCode wants it in O(log n).
- constraints
- 1 ≤ nums.length ≤ 1000
- −2³¹ ≤ nums[i] ≤ 2³¹ − 1
- nums[i] ≠ nums[i + 1]
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {number[]} nums
* @return {number}
*/
function findPeakElement(nums) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more binary search problems
- 704Binary SearchEasysolve
- 35Search Insert PositionEasysolve
- 69Sqrt(x)Easysolve
- 153Find Minimum in Rotated Sorted ArrayMediumsolve
- 33Search in Rotated Sorted ArrayMediumsolve
- 875Koko Eating BananasMediumsolve
- 1011Capacity To Ship Packages Within D DaysMediumsolve
- 274H-IndexMediumsolve
- 74Search a 2D MatrixMediumsolve
- 34Find First and Last Position of Element in Sorted ArrayMediumsolve
- 4Median of Two Sorted ArraysHardsolve
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.