Skip to content
BASA DSA

Binary Search

EasyBinary Search

Return the zero-based index of target in a strictly increasing array, or -1 if absent. Use logarithmic time.

Example 1

Input
nums = [-5,0,6,11]
target = 6
Output
2

Explanation

6 is at zero-based index 2.

Example 2

Input
nums = [1,4]
target = 3
Output
-1

Explanation

3 falls between 1 and 4 but is absent.

Example 3

Input
nums = [9]
target = 9
Output
0

Explanation

The sole element equals the target at index 0.

Constraints

  • 1 <= len(nums) <= 10000
  • Values are distinct signed 32-bit integers

Function contractsearchArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…