FunctionRacerProblemsRace

Search Insert Position

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

nums is sorted in increasing order with no repeats. If target is in the list, return its index. Otherwise return the index where it would have to be inserted to keep the list sorted.

Example 1

nums [1,3,5,6]
target 5
returns 2

5 is at index 2.

Example 2

nums [1,3,5,6]
target 2
returns 1

2 would go between 1 and 3, at index 1.

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