Introduction
Find Peak Element means:
- finding element
greater than neighboring values
Peak Condition:
nums[i] > nums[i - 1] and nums[i] > nums[i + 1] Goal:
- efficiently locate peak
without full traversal
Example:
Array:[1,2,3,1]
Output:
Index 2
Explanation:
Element 3 is greater than its neighboring values. This problem is one of the most important applications of:
Binary SearchConstraints
1 <= Array Size <= 10^5 Approach : Binary Search Optimization
Explanations:
Explanation:
The idea is:
- compare middle element
with neighboring value - move toward increasing slope
- peak always exists
Steps:
- Find middle index.
- Compare adjacent elements.
- Detect increasing slope.
- Move search range.
- Continue until peak found.
Key Observation:
Peak exists on increasing side. Conditions:
nums[mid] < nums[mid + 1] → move right nums[mid] > nums[mid + 1] → move left This approach:
- uses binary search
- avoids linear traversal
Dry Run
Array:[1,2,3,1]
Middle:
2
3 > 1
Peak exists on left side.
Answer:
Index 2
Practice :