Back to DSA
Find First and Last Position of Element in Sorted Array
easyIn a sorted integer array, find the first and last positions where a given target value occurs. If the target is not found, return [-1, -1]. Your algorithm must run in O(log n) time.
Examples
Example 1:
Input:
nums = [1,3,3,3,5,7], target = 3Output:
[1,3]Explanation: 3 first occurs at index 1 and last occurs at index 3.
Example 2:
Input:
nums = [2,4,6,8], target = 5Output:
[-1,-1]Explanation: 5 is absent from the array.
Hints
1234567