FunctionRacerProblemsRace

First and Last Position

EasySorting and searchingPython, JavaScript, TypeScript, Java, C++

nums is sorted in non-decreasing order and may hold repeats. Return [first, last], the first and last index at which target appears, or [-1, -1] if it never does.

Example 1

nums [5,7,7,8,8,10]
target 8
returns [3,4]

8 is at indices 3 and 4.

Example 2

nums [5,7,7,8,8,10]
target 6
returns [-1,-1]

No 6.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Sorting and searching problems