ntree.ai

Local Minimum

mediumArraysBinary search

Given an array nums, return the index of any local minimum number.

A local minimum number is a number that is smaller than both of its neighbours, counting a missing neighbour as larger.

All elements in the array are unique.

Example 1:

Input:  nums = [9, 7, 4, 2, 5, 8]
Output: 3
Explanation: nums[3] == 2, smaller than 4 on its left and 5 on its right.

Example 2:

Input:  nums = [1, 2, 3]
Output: 0
Explanation: The first element has only one neighbour, so we compare only to it. 1 is smaller than 2, so it's a local minimum.

Constraints:

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9