Skip to content
AI360Xpert
Beta
Difficulty: MediumBinary Search

Find Peak Element

Problem in Plain English

Given an array with no adjacent equals, return the index of any element strictly greater than its neighbours. Following the uphill slope always leads to a peak, which a binary search exploits in logarithmic time.

Problem Statement

Given a 0-indexed integer array nums, find a peak element, and return its index. A peak element is an element that is strictly greater than its neighbors.

If the array contains multiple peaks, return the index to any of the peaks. You may imagine that nums[-1] = nums[n] are −∞-\infty, and you must write an algorithm that runs in O(log⁡n)O(\log n) time.

Constraints

  • 1 <= nums.length <= 1000
  • -2^31 <= nums[i] <= 2^31 - 1
  • nums[i] != nums[i + 1] for all valid i.

Examples

Example 1

Input
nums = [1,2,3,1]
Output
2

Index 2 holds 3, strictly above its immediate neighbours 2 and 1. Return index 2 rather than value 3.

Example 2

Input
nums = [1,2,1,3,5,6,4]
Output
5

Indices 1 and 5 are both valid peaks: 2 exceeds its neighbours 1 and 1, while 6 exceeds 5 and 4. Either index is accepted; the illustrated output chooses 5.

Example 3

Input
nums = [-2147483648]
Output
0

The sole element is above both imaginary neighbours, even at the smallest allowed integer value. Return its index 0.

Intuition

A peak is higher than both immediate neighbours; missing neighbours are considered lower than every input value. In [1, 2, 3, 1], index 2 holds 3, above 2 and 1. Following a rising step must eventually reach a fall or the right boundary, so some peak lies in that direction. We only need one peak, allowing binary search to retain a half with a guaranteed peak even if other peaks are discarded.

Approaches

First Drop Scan

Solution Details

Reveal First Drop Scan: intuition, complexity, and code

Hints

Hint 1
Sortedness is not required; a reliable halving decision is enough for binary search.
Hint 2
Compare the middle with its right neighbour to learn whether the slope rises or falls there.
Hint 3
An uphill middle guarantees a peak to the right, and a downhill middle guarantees one at or left of it.

Edge Cases

  • A single element is a peak by the boundary rule, and the loop returns index 0 without iterating.
  • A strictly rising array climbs all the way, so the answer is the last index.
  • A strictly falling array drops immediately, so the answer is index 0.
  • Nonadjacent repeated values are valid: [1, 3, 1, 3, 1] has peaks at indices 1 and 3; equal adjacent values are excluded.
  • At integer limits, compare directly rather than subtracting: [-2147483648, 2147483647] returns 1 without arithmetic overflow.

Common Mistakes and Interview Tips

  • Reading nums[mid + 1] with left <= right can go out of bounds. With left < right, the lower middle always has a right neighbour.
  • Setting right = mid - 1 can discard a peak at mid; setting left = mid can stall on a two-element rising interval.
  • Requiring a particular index on multi-peak inputs is incorrect. Return any index strictly above its immediate neighbours.
  • Treating this as a global-maximum problem misses the local requirement: the first-drop scan may return a smaller peak.
  • Using a numeric sentinel equal to the smallest integer does not model negative infinity; handle missing neighbours by their positions.
  • Subtracting neighbours to test a slope may overflow Java int at the allowed limits. Compare the values directly.
  • Interview tip: explain the boundary conditions that keep at least one peak in the interval. The input need not be sorted, and the discarded half can still have peaks.

Key Takeaway

Binary search needs a halving decision, not a sorted array: a local slope that guarantees a peak in one half is enough.