Perform a binary search to find an element in a sorted array.
Time complexity: O(log n)
Space complexity: O(1)
Sorted array to perform binary search on
The value of the first element in the array that satisfies the provided testing function. Otherwise, undefined is returned.
undefined
Array
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];function directionFn(value: number) { const squared = value ** 2; if (squared === 64) { return 0; } return squared - 64;}binarySearch(array, directionFn); Copy
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];function directionFn(value: number) { const squared = value ** 2; if (squared === 64) { return 0; } return squared - 64;}binarySearch(array, directionFn);
Perform a binary search to find an element in a sorted array.
Time complexity: O(log n)
Space complexity: O(1)