Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. You must write an algorithm with O(log n) runtime complexity.
Example 1
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 is found in nums at index 4, so the answer is 4.
Example 2
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 would fit between 0 and 3, but it is not in nums, so the answer is -1.
Example 3
Input: nums = [5], target = 5
Output: 0
Explanation: The only element, nums[0], equals the target 5, so the answer is 0.
Because the array is sorted, one comparison against the middle element eliminates half of the remaining candidates each step.
Run checks all cases above. Submit evaluates all test cases.