← problems
#704

Binary Search

EasyBinary Search

Given a sorted (ascending) array of distinct integers nums and an integer target, return the index of target if it is in nums, or -1 if it is not.

You must write an algorithm with O(log n) runtime.

Example

nums = [-1, 0, 3, 5, 9, 12], target = 9  => 4
nums = [-1, 0, 3, 5, 9, 12], target = 2  => -1
Implement
function Search(nums: int[], target: int) -> int

Found a bug? Or annoyed about something in the language? Tell us.

loading editor…