34. Find First and Last Position of Element in Sorted Array
Practice Find First and Last Position of Element in Sorted Array for free on NudgeCode: Medium, Binary Search pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
nums is sorted ascending and may contain duplicates. Return [first, last]: the first and last index where target appears, or [-1,-1] if it doesn't appear. Target 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]
Constraints
- 0 <= nums.length <= 10^5
- -10^9 <= nums[i], target <= 10^9