Element in the Middle
MediumP3Coding
Find the first element of nums (smallest index) that is strictly greater than every element to its left and strictly smaller than every element to its right. The element must have at least one element on each side, so the first and last positions never qualify.
Return that element's value, or -1 if there is none.
Example 1
- Input
- nums = [4,2,5,7]
- Output
- 5
- Why
- 5 is greater than 4 and 2 on its left and less than 7 on its right (the value at index 1 fails because 4 is bigger).
Example 2
- Input
- nums = [1,2,3]
- Output
- 2
- Why
- 2 has 1 on its left and 3 on its right.
Example 3
- Input
- nums = [3,2,1]
- Output
- -1
Constraints
- 0 <= nums.length <= 10^5
- 1 <= nums[i] <= 10^9
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 11. ⌘Enter runs, ⌘⇧Enter submits.