backbenchcode
← all problemsnext: Lab Network Reachability
#05EasyAlgorithms · 68% acceptance

Search the Sorted Threshold

Binary SearchArray

Given a sorted array `nums` and a `target`, return the index of `target`, or -1 if it is absent.

Halving the search space each step is the same instinct as a titration — narrow in until you bracket the answer.

Examples

Input: nums = [-1,0,3,5,9,12], target = 9

Output: 4

Input: nums = [-1,0,3,5,9,12], target = 2

Output: -1

Constraints

Hints

solution.pyPython 3
1
2
3
4
5
6
7
8
9
10
11
Test console

// hit Run to execute the sample tests

// Interactive editor preview. Live code execution arrives with your early-access cohort.