← All problems

Binary Search

easy

Given a sorted array of distinct integers nums and a target, return the index of target if it exists, otherwise return -1. Your solution must run in O(log n).

This is the coding counterpart to the Binary Search visualization — same idea, now write the code yourself.

Example

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