Loading problem…
You're building a search feature that needs to find an element in a sorted array efficiently. Binary search is the optimal algorithm for sorted arrays.
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, return its index. Otherwise, return -1.
binarySearch([-1,0,3,5,9,12], 9); // 4
binarySearch([-1,0,3,5,9,12], 2); // -1This problem models real search scenarios: