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.

&nbsp;
Example 1:

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


Example 2:

Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 104
	-104 &lt; nums[i], target &lt; 104
	All the integers in nums are unique.
	nums is sorted in ascending order.


class Solution:
    def search(self, nums: List[int], target: int) -> int:
        left = 0
        right = len(nums)-1
        
        while left<=right:
            mid = (left+right)//2
            if nums[mid]==target:
                return mid
            elif nums[mid]>target:
                right = mid-1
            else:
                left = mid+1
        
        return -1
class Solution {
    public int search(int[] nums, int target) {
        int l = 0;
        int r = nums.length - 1;
        return binarySearch(nums, l, r, target);
    }
    
    private int binarySearch(int[] nums, int l, int r, int target) {
        if (l <= r) {
            int mid = (r + l) / 2;
        
            if (nums[mid] == target) {
                return mid;
            } 
        
            if (nums[mid] < target) {
                return binarySearch(nums, mid + 1, r, target);
            } else {
                return binarySearch(nums, l, mid - 1, target);
            }
        } 
        return -1;
    }
}
class Solution {
public:
    int search(vector<int>& nums, int target) {
        int n = nums.size();
        int jump = 5;
        int p = 0;
        while (jump*5 < n) jump *= 5;
        while (jump > 0) {
            while (p+jump < n && nums[p+jump] <= target) p += jump;
            jump /= 5;
        }
        return (p == n || nums[p]!= target) ? -1 : p;
    }
};
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function(nums, target) {
    
    let low=0;
    let high=nums.length-1;
    
    if(target<nums[0] || target>nums[high]){
        return -1;
    }
    
    while(low<=high) {
        
        mid=Math.floor((low+high)/2);
        
        if (nums[mid]==target){
            return mid;
        }
        else{
            
            if(target>nums[mid]){
                low=mid+1;
                
            }
            else{
                high=mid-1;
            }
        }
    }
    
    return -1;
    
};
Binary Search