704. Binary Search

Practice Binary Search for free on NudgeCode: Easy, Binary Search pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

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

Example

nums = [-1,0,3,5,9,12], target = 9  ->  4
nums = [-1,0,3,5,9,12], target = 2  ->  -1

Constraints