algorythms
Modified Binary Search
LC #704Easy

Binary Search

Modified Binary Search
AmazonGoogleMicrosoftApple

Problem

Given a sorted array and a target, return the index of the target or -1.

arraybinary-search

Constraints

  • 1 ≤ n ≤ 10⁴
  • -10⁴ ≤ nums[i], target ≤ 10⁴
  • All values in nums are unique
  • nums is sorted in ascending order
  • O(log n) runtime required

Example

Inputnums = [-1, 0, 3, 5, 9, 12], target = 9
Output4
Why

9 is at index 4

Hints — reveal one at a time