Skip to main content
Back to all problems
Beginner guide, Algorithms & data structures

Binary Search

Throw away half the search space at every step for O(log n), even when the array is only partly sorted.

Binary search finds something in a sorted range by checking the middle and throwing away the half that can't contain the answer. Then it repeats on the half that's left. Each step cuts the problem in half, so even a million items take only about 20 steps.

The guessing game

"I'm thinking of a number between 1 and 100." The smart strategy is to guess 50. If I say "higher", you know it's 51 to 100, so you guess 75. Then 63, 57, and so on. Each guess halves what's left. You'll always find it in 7 guesses or fewer.

That's binary search. The array needs one property: when you look at the middle, you must be able to tell which half the answer is in. A sorted array gives you that for free.

Why it's so fast: O(log n)

Each step halves the range. How many halvings until one item is left?

Items (n)Steps (about log₂ n)
83
1,00010
1,000,00020
1,000,000,00030

That's O(log n): the work barely grows even when the input explodes. Compare O(n), which would take a billion steps for the last row.

The template

js
let left = 0
let right = nums.length - 1
while (left <= right) {
  const mid = Math.floor((left + right) / 2) // the middle index
  if (nums[mid] === target) return mid // found it
  if (nums[mid] < target)
    left = mid + 1 // answer is in the right half
  else right = mid - 1 // answer is in the left half
}
return -1 // not found
Loading diagram...

Walking through it

nums = [1, 3, 5, 7, 9, 11, 13], looking for 9:

leftrightmidnums[mid]Decision
06377 < 9 → left = 4
4651111 > 9 → right = 4
4449found at index 4 ✅

The real requirement: "which half?"

Binary search doesn't strictly need a perfectly sorted array. It needs a way to look at the middle and decide which half to keep. That's why it works on a rotated sorted array (the two problems here): you can't use plain "smaller or bigger" anymore, but with one extra comparison you can still tell which half to keep.

ProblemHow you decide which half to keep
Find Minimum in Rotated Sorted ArrayIs nums[mid] bigger than the last element? Then the drop is right.
Search in Rotated Sorted ArrayFind the half that's sorted normally, and check if the target fits.

Two loop styles

There are two common versions, and mixing them up causes infinite loops:

  • while (left <= right) with left = mid + 1 and right = mid - 1: for "find this exact value". The range can become empty, which means "not found".
  • while (left < right) with left = mid + 1 and right = mid: for "find the first position where something becomes true" (like the minimum). It stops when one candidate is left, which is the answer.

Pick one and keep the updates consistent with it.

How to recognize this category

Think "binary search" when:

  • the input is sorted (or sorted with a twist, like rotated)
  • the problem demands O(log n)
  • you're looking for the first or last position where some condition flips from false to true
  • you can guess an answer and quickly check "too small" or "too big"

Common mistakes

  • Off-by-one errors. right = mid vs right = mid - 1 matters. Ask: "could mid itself be the answer?" If yes, keep it.
  • Infinite loops. With while (left < right) and left = mid (without + 1), a two-element range never shrinks.
  • Forgetting the array must be sorted. Binary search on an unsorted array gives wrong answers, silently.

Practice what you learned

The problems go from easier to harder. Try each one on your own and open a hint when you get stuck.