Initializing model...

Binary Search

medium

Given a sorted array of integers `nums` and a target value `target`, return the index of `target` if it is in the array. If not, return `-1`.

You must write an algorithm with O(log n) runtime complexity.

Function Signature

def binary_search(nums: list[int], target: int) -> int:

Examples

Input: nums = [-1, 0, 3, 5, 9, 12], target = 9
Output: 4
Input: nums = [-1, 0, 3, 5, 9, 12], target = 2
Output: -1