Skip to main content
🧩 Algorithms & Problem Solving

Binary Search

On a sorted array you don't scan linearly β€” you jump to the middle and throw away half each step (O(log n)). Track lo and hi pointers and move them toward the target.

Your task

Write binarySearch(sorted, target) returning the index of target in the sorted array, or -1 if it's not present.

⚑ 45 XP
solution.jsRuns in your browser
Loading editor…
Tests3 to pass
finds 7 at index 3
missing β†’ -1
single element

Run your code to check these.