# Leet Code 704: Binary Search

### Problem statement:

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.

### Constraints:

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

### Solution:

`.css-1r5hp7i{position:absolute;top:0;right:0;z-index:1;border-radius:0 5px 0 5px;padding:0.25rem 0.6rem;border:none;cursor:pointer;background:#44475a;color:rgb(248, 248, 242);-webkit-transition:all 200ms ease;transition:all 200ms ease;font-size:12px;}.css-1r5hp7i:disabled{cursor:not-allowed;}.css-1r5hp7i:not(:disabled):hover,.css-1r5hp7i:not(:disabled):focus{background:#8257E6;}`/** * @param {number[]} nums * @param {number} target * @return {number} */var search = function(nums, target) {        const len = nums.length;        let start = 0;    let end = len -1;        while (start <= end) {        let mid = parseInt((start+end)/2);        if(nums[mid]===target) {            return mid;        } else if(target < nums[mid]) {            end = mid -1;        } else {            start = mid+1;        }    }        return -1;    };``