Example 1
Input
nums = [-1, 0, 3, 5, 9, 12], target = 9
Output
4
Loading CodeSprint...
/problems/binary-search
Find a target value in an ascending list of distinct integers. Return its index when found, otherwise return -1.
Implement this function and return the result. CodeSprint supplies each test case automatically.
binarySearch(nums, target) → numberInput
nums = [-1, 0, 3, 5, 9, 12], target = 9
Output
4
Input
nums = [-1, 0, 3, 5, 9, 12], target = 2
Output
-1
Constraints
0 <= nums.length <= 100,000 nums is sorted in strictly ascending order.