89. Find First and Last Position of Element in Sorted Array

MediumArrayArray

Given sorted integer array nums and target, find its starting and ending position. Return [-1,-1] if not found. O(log n) required.

Input: Sorted integer array nums and integer target.

Output: [first_index, last_index] or [-1,-1].

Examples

Example 1
Input: [5,7,7,8,8,10],8
Output: [3,4]
Explanation: 8 at indices 3 and 4.
Example 2
Input: [5,7,7,8,8,10],6
Output: [-1,-1]
Explanation: 6 not found.
Example 3
Input: [],3
Output: [-1,-1]
Explanation: Empty array.

Constraints

Asked by

MetaAppleBloombergCapgeminiAmazonMicrosoft
Solve this problem in the editor →