CodePark/ problems / Find a Number in a Sorted Array
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Binary search

Find a Number in a Sorted Array

easyabout 10 minutes in an interview

You get an array of numbers nums, sorted in ascending order, and a number target. Return the index of target in nums. If target is not in nums, return -1.

  • All values in nums are distinct.
  • nums can be empty, and it can hold negative numbers.
  • nums can hold up to 1,000,000 numbers, and the function is called many times. So your solution must run in O(log n) time.
Input: nums = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91], target = 23 Output: 5
Input: nums = [-4, 0, 7, 11], target = 3 Output: -13 would sit between 0 and 7, but it is not in the array.