PRACTICE · BINARY SEARCH
Binary Search
The challenge
Given an array sorted in ascending order and a target value, return the target's index. Return -1 when the target does not appear. Aim for logarithmic time.
Example
Input nums = [-1, 0, 3, 5, 9, 12], target = 9
Output 4
The value 9 appears at index 4.
Before you begin
- Read through the example and consider edge cases.
- Write down a simple solution before optimizing.
- Describe the time and space complexity of your approach.
A space to understand the problem. Use your favorite editor to write and run your solution.