Explorer
Node.js

Write a function `binarySearch(arr, target)` that performs binary search on a sorted array and returns the index, or -1 if not found.

Problem Statement

Write a function `binarySearch(arr, target)` that performs binary search on a sorted array and returns the index, or -1 if not found.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: Binary search works on sorted arrays by repeatedly halving the search range. šŸ’” Hint 2: Maintain left and right pointers. Check the middle element. šŸ’” Hint 3: If mid value < target, search right half. If mid value > target, search left half. āœ… Solution: ```javascript function binarySearch(arr, target) { let left = 0, right = arr.length - 1; while (left <= right) { const mid = Math.floor((left + right) / 2); if (arr[mid] === target) return mid; if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; } ```

Binary Search

Medium
Write a function `binarySearch(arr, target)` that performs binary search on a sorted array and returns the index, or -1 if not found.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
[3,-1,0]