FunctionRacerProblemsRace

Binary Search

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

nums is sorted in increasing order with no repeats. Return the index of target, or -1 if it is not in the list.

The point of the exercise is to halve the search range each step.

Example 1

nums [-1,0,3,5,9,12]
target 9
returns 4

9 is at index 4.

Example 2

nums [-1,0,3,5,9,12]
target 2
returns -1

2 is not in the list.

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