Binary Search Variants

Problem Implement binary search on a sorted array, then extend it to lower bound (first index of a target) and upper bound (last index of a target) on a sorted array that may contain duplicates.

Input / Output

  • Input: sorted array nums and a target value.
  • Output: whether target exists, plus the first and last index of target (or -1 if absent).

Constraints

  • 1 <= n <= 10^5.
  • Array sorted ascending; duplicates allowed.

Example

  • [1,2,2,2,3], target 2 -> first index 1, last index 3.
  • [1,2,2,2,3], target 4 -> not found.
added …
LeaderboardSalaryAccount