Binary Search

Languages

Implement a function that performs binary search on an array of numbers. The function should take in a sorted array of integers and a target integer to find. It returns the index of the target element or -1, if the target element doesn't exist in the array.

Examples

binarySearch([1, 2, 3, 6, 9, 11], 6); // 3
binarySearch([1, 2, 3, 12, 14, 16], 5); // -1

Hints

New

Loading editor

    Binary Search | Algorithms Interview Questions with Solutions