Binary Search

Easy

Topics
ArrayBinary Search

Given a sorted (ascending) array nums of distinct integers and a target, return its index, or -1 if not present. Must run in O(log n).

Example 1

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

Example 2

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

Constraints

  • 1 <= nums.length <= 10^4
  • nums is sorted ascending with distinct values.
Run ⌘' · Submit ⌘⏎