Skip to main content

Binary Search

EasyBinary Search

Given a sorted array of integers nums and a target value, return the index of target if found. If not found, return -1. You must write an algorithm with O(log n) runtime complexity.

Example:

Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums at index 4

Constraints:

  • •1 <= nums.length <= 10^4
  • •-10^4 <= nums[i], target <= 10^4
  • •All integers in nums are unique
  • •nums is sorted in ascending order