Find First and Last Position of Element in Sorted Array
Return the first and last positions of target in a sorted array.
Examples
Input: [[5,7,7,8,8,10],8]
Output: [3,4]
Input: [[5,7,7,8,8,10],6]
Output: [-1,-1]
Hints
Implement a binary search to find the first occurrence of the target by checking if the middle element is the target and if the previous element is not the target.
Modify the binary search to find the last occurrence by checking if the middle element is the target and if the next element is not the target.
Handle edge cases such as an empty array or a target not present in the array by returning [-1, -1] immediately after the binary search completes without finding the target.
Related Problems
Find First and Last Position of Element in Sorted Array
Return the first and last positions of target in a sorted array.