FastPrepFind the Pivot Element

Find the Pivot Element

Agoda logoAgoda● MediumFULLTIMEPHONE SCREEN
Learn

Problem 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[]) → int

Examples

Example 1

nums = [5,1,4,3,6,8,10,7,9]return = 6

Every value before 6 is smaller, and every value after it is greater.

Example 2

nums = [1,2,3,4,5]return = 2

Several internal values qualify; 2 is the leftmost one.

Example 3

nums = [4,2,5,1,7]return = -1

No internal value is greater than every value on its left and smaller than every value on its right.

Constraints

  • 3 <= nums.length <= 2000
  • 0 <= nums[i] <= 10^9
  • All values in nums are distinct.

More Agoda problems

See Agoda hiring insights
public int findPivotElement(int[] nums) {
    // write your code here
}
nums[5,1,4,3,6,8,10,7,9]
expected6
Checking account…