The problem. A peak is an element strictly greater than its neighbours. Given an array nums where no two neighbours are equal, return the index of any peak. Treat the positions just outside the array as -∞, so the first or last element can be a peak too. Your solution must run in O(log n) time.
Input: nums = [1, 2, 3, 1]
Output: 2 nums[2] = 3 is bigger than 2 and 1
Input: nums = [1, 2, 1, 3, 5, 6, 4]
Output: 1 or 5 both 2 and 6 are peaks; either index is acceptedPicture the array as a mountain range: each value is a height. A peak is any point higher than the points on both sides. Because the edges count as -∞, a peak always exists — even a steadily rising array ends in one (its last element).
The problem asks for any peak, not the highest one. That freedom is what makes an O(log n) solution possible.
Test the definition directly: an index is a peak if it beats its left neighbour and its right neighbour, where a missing neighbour (off the edge) counts as -∞.
function findPeakElement(nums) { const n = nums.length; for (let i = 0; i < n; i++) { const leftOk = i === 0 || nums[i] > nums[i - 1]; // edges count as -infinity const rightOk = i === n - 1 || nums[i] > nums[i + 1]; if (leftOk && rightOk) return i; } return -1; // unreachable: a peak always exists}Time O(n), space O(1). Correct, but it ignores the problem's demand for O(log n).
We don't need to check the left neighbour at all. Scan from the left: as long as the values keep rising, we're climbing. The first time nums[i] > nums[i + 1], index i is a peak — we got there uphill, and the next step goes down. If that never happens, the array only rises and the last index is the peak.
function findPeakElement(nums) { for (let i = 0; i < nums.length - 1; i++) { if (nums[i] > nums[i + 1]) return i; // we climbed to i, and the next step goes down } return nums.length - 1; // it only ever rose: the last element is a peak}Time O(n), space O(1). Still linear in the worst case, but it uses one comparison per step — and a much more useful idea: if the line is going up, a peak lies ahead. The best solution takes that idea and jumps instead of walking.
Stand at the middle index mid and look one step right. There are only two possibilities:
nums[mid] < nums[mid + 1]): keep climbing right and you must reach a peak — either the line turns down somewhere, or you reach the last element, which beats the -∞ beyond it. So a peak exists in the right half: low = mid + 1.nums[mid] > nums[mid + 1]): by the same argument walking left, a peak exists at mid or to its left: high = mid. (mid itself stays in range — it might be the peak.)Each step throws away half of the range, and a peak is always kept inside it. When low meets high, that index is a peak. This is binary search — not on sorted values, but on the direction of the slope.
The bars show the heights. Watch the range (unfaded bars) halve each step. Try an always-rising array: the search runs straight to the last index.
mid + 1 is always a valid index here: the loop only runs while low < high, so mid is at most high - 1. In Java and C++, low + (high - low) / 2 avoids overflow on huge arrays.
Time O(log n), space O(1). The range halves every step: for a million elements that's about 20 steps instead of a million.
O(n)Each of the n indexes is compared with its two neighbours.
O(1)Just an index.
O(n)[10, 20, 30, 40, 50]: the last index is the peak.[100, 80, 60, 50, 20]: the first index is the peak.[7]: index 0.Binary search works whenever you can look at the middle and decide which half must contain an answer. Searching on a property rather than on sorted values also finds the minimum of a rotated sorted array, and the smallest capacity or speed that "works" in scheduling problems.
In the worst case the line rises to the last element, so every step is walked.
O(1)Just an index.
O(log n)Each comparison halves the range, so about log₂ n steps.
O(1)Only low and high.