Find Peak Element
Medium Binary Search
nums is a list of whole numbers in which no two numbers next to each other are equal.
A peak is a position whose number is larger than the numbers on either side of it. Beyond both ends of the list, count the outside as lower than every number: the first position need only beat the second, the last only the one before it, and a list of one number has its peak at position 0.
Return the position of a peak, counting from 0. Every such list has at least one, and when it has several, any of them is right.
Example 1
Position 1 holds 8, larger than the 3 and the 5 on either side of it, so it is a peak. So is position 4, which holds 6, and either answer is right.
Example 2
The numbers only rise, and the outside past the end counts as lower, so the last position is the one peak.
Example 3
Constraints
1 ≤ nums.length ≤ 4 × 10⁵-2³¹ ≤ nums[i] ≤ 2³¹ - 1nums[i] ≠ nums[i + 1]: numbers next to each other differ
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundMore Binary Search
- Median of Two Sorted ArraysAI round · Hard
- Search in Rotated Sorted ArrayAI round · Medium
- Find First and Last Position of Element in Sorted ArrayAI round · Medium
- Koko Eating BananasAI round · Medium
- Search Insert PositionEasy