Easy · Binary search

Binary Search

You are given a list of integers, nums, sorted from smallest to largest, and a number, target. Return the position of target in the list, or -1 if it is not there.

  • Every number in the list is different.
  • Do it in O(log n): halve what is left each step, do not walk the whole list.
nums = [-1, 0, 3, 5, 9, 12], target = 9

→ 4   (9 sits at index 4)
Record your take

1 way to solve it.