Problem · Array
Find the Pivot Element
Learn this problemProblem statement
Given an array nums of distinct nonnegative integers, find an element whose every element to the left is strictly smaller and whose every element to the right is strictly greater.
Only an internal element may be a pivot, so its index must be between 1 and nums.length - 2. If several elements qualify, return the leftmost pivot value. Return -1 when no pivot exists.
Function
findPivotElement(nums: int[]) → intExamples
Example 1
nums = [5,1,4,3,6,8,10,7,9]return = 6Every value before 6 is smaller, and every value after it is greater.
Example 2
nums = [1,2,3,4,5]return = 2Several internal values qualify; 2 is the leftmost one.
Example 3
nums = [4,2,5,1,7]return = -1No internal value is greater than every value on its left and smaller than every value on its right.
Constraints
3 <= nums.length <= 20000 <= nums[i] <= 10^9- All values in
numsare distinct.