This question evaluates proficiency in array search algorithms and algorithmic reasoning, specifically the use of binary search techniques and careful boundary/edge-case handling to identify first and last occurrences.
You are given an array of integers nums sorted in non-decreasing order and an integer target.
Return a pair of indices [first, last] such that:
first
is the smallest index
i
with
nums[i] == target
last
is the largest index
j
with
nums[j] == target
If target does not exist in the array, return [-1, -1].
nums: int[]
,
target: int
int[2]
representing
[first, last]
0 <= nums.length <= 10^5
nums
is sorted in non-decreasing order
nums = [5,7,7,8,8,10]
,
target = 8
→
[3,4]
nums = [1,2,3]
,
target = 4
→
[-1,-1]