Modified Binary Search
LC #704Easy
Binary Search
Modified Binary Search
AmazonGoogleMicrosoftAppleProblem
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
Input
nums = [-1, 0, 3, 5, 9, 12], target = 9Output
4Why
9 is at index 4