#19

Binary Search

Easy
All Problems

19. Binary Search#704

Easy🔍 Binary SearchClassic Binary Search

Search for a target value in a sorted array. Return its index or -1.

Examples

Example 1:
Input: [[-1,0,3,5,9,12],9]
Output: 4
Example 2:
Input: [[-1,0,3,5,9,12],2]
Output: -1
Example 3:
Input: [[5],5]
Output: 0

Why learn this

The textbook binary search implementation every programmer must know.

Binary SearchArrays
Python 3Loading Python engine...
Click "Run Code" to test your solution