Problem · Array

Leftmost Binary Search Match

Learn this problem
EasyByteDance logoByteDanceFULLTIMEPHONE SCREEN

Problem statement

Return the smallest index whose value equals target in the nondecreasing array nums, or -1 when the target is absent.

Function

leftmostIndex(nums: int[], target: int) → int

Examples

Example 1

nums = [1,2,2,2,4]target = 2return = 1

The first of three equal values is at index one.

Example 2

nums = [1,3,5]target = 2return = -1

The lower-bound position does not contain the target.

Constraints

  • 0 <= nums.length <= 100000
  • nums is sorted in nondecreasing order.
  • The required time complexity is O(log n).

More ByteDance problems

drafts saved locally
public int leftmostIndex(int[] nums, int target) {
  // Write your code here.
}
nums[1,2,2,2,4]
target2
expected1
checking account