Find First and Last Position of Element
Given a non-decreasing sorted array nums and a target value, return the starting and ending index of target as [first, last]. If target is not present, return [-1, -1]. You must run in O(log n) time.
Open official problem prompt ↗Report the inclusive index span occupied by a target value in a sorted array, or signal its absence.
Like finding where a word's entries begin and end in a dictionary: you flip to the first page where it could appear and the first page after it stops, and everything between is that word.
- Input
- nums = [5, 7, 7, 8, 8, 10], target = 8
- Output
- [3, 4]
- Why
- The value 8 first appears at index 3 and last appears at index 4.
0 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9nums is a non-decreasing array-10^9 <= target <= 10^9