Medium

Quiz

#34 Find First and Last Position of Element in Sorted Array

APPROACH

In non-decreasing sorted integer array nums, find the leftmost and rightmost indices of target. Return [-1, -1] if target is not found. The solution must run in O(log n) time.

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]

Example 2:

Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]

Example 3:

Input: nums = [], target = 0
Output: [-1,-1]
1 of 4
1:00

What is the optimal approach for this problem?