Find First and Last Position in Sorted Array
Problem
Given a sorted array of integers and a target value, find the index of the first and the last occurrence of the target. If the target is not present, return [-1, -1]. Aim for O(log n) time.
Input / Output
- Input: a non-decreasing integer array
numsand an integertarget. - Output:
[firstIndex, lastIndex], or[-1, -1]if the target is absent.
Constraints
0 <= nums.length <= 10^5- Array is sorted non-decreasing; duplicates may exist.
- Required time complexity: O(log n).
Example
nums = [5,7,7,8,8,10], target = 8->[3,4]nums = [5,7,7,8,8,10], target = 6->[-1,-1]
added …