Peak Element Finder - Problem
A peak element in an array is an element that is greater than both of its neighbors. Given an integer array nums, find all peak elements and return their indices.
For edge elements (first and last elements), they are considered peaks if they are greater than their single neighbor.
Special cases:
- If the array has only one element, it is always a peak
- The first element is a peak if it's greater than the second element
- The last element is a peak if it's greater than the second-to-last element
Input & Output
Example 1 — Multiple Peaks
$
Input:
nums = [5,2,8,3,7,1]
›
Output:
[0,2,4]
💡 Note:
Index 0: 5 > 2 (first element peak), Index 2: 8 > 2 and 8 > 3 (middle peak), Index 4: 7 > 3 and 7 > 1 (middle peak)
Example 2 — Edge Peaks Only
$
Input:
nums = [4,1,2,3]
›
Output:
[0,3]
💡 Note:
Index 0: 4 > 1 (first element is peak), Index 3: 3 > 2 (last element is peak)
Example 3 — Single Element
$
Input:
nums = [7]
›
Output:
[0]
💡 Note:
Single element is always a peak by definition
Constraints
- 1 ≤ nums.length ≤ 1000
- -106 ≤ nums[i] ≤ 106
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code