Write a function to perform a binary search on a sorted array.
HardProblem Statement
Write a function to perform a binary search on a sorted array.
Sample Input
array = [ 1, 3, 5, 7, 9, 11 ], target = 7
Sample Output
3
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
// Perform binary search on a sorted array
function binarySearch(arr, target) {
// Initialize left and right pointers
// Use a loop to find the target by adjusting pointers
// Return the index if found, otherwise -1
}
console.log(binarySearch([ 1, 3, 5, 7, 9, 11 ], 7));
Input
array = [ 1, 3, 5, 7, 9, 11 ], target = 7
Expected Output
3