Problems/704. Binary Search

704. Binary Search

EasyArrayTwo PointersBinary Search

Given an array of integers nums sorted in ascending order, and an integer target, search for target within the array. If the target exists, return its 0-based index. Otherwise, return -1.

Your algorithm must run in O(log n) time.

Example 1
Input: nums = [-1, 0, 3, 5, 9, 12], target = 9
Output: 4
9 exists in nums and its index is 4.
Example 2
Input: nums = [-1, 0, 3, 5, 9, 12], target = 2
Output: -1
2 does not exist in nums, so return -1.
Visualizer

Visualizer will appear here