First and Last Position of Element in Sorted Array

Given a sorted array, find the starting and ending position of a given target. Return [-1,-1] if not found.

Input Format

nums = sorted array, target = integer

Output Format

[firstPos, lastPos]

Constraints

  • 0 <= nums.length <= 10^5; -10^9 <= nums[i] <= 10^9

Examples

Example 1:

Input:

nums = [5,7,7,8,8,10]
target = 8

Output:

[3,4]

Explanation:

8 appears at indices 3 and 4.

Loading...
First and Last Position of Element in Sorted…