Skip to content

Start typing to search patterns, problems and pages.

Binary Search

Finding a word in a paper dictionary, you open near the middle and see which side the word falls on. The other half closes for good. Binary search works the same way: one comparison rules out half of what is left.

⏱ about 20 minutesNeeds: arrays, sorted orderTime O(log n)Space O(1)

2 · Watch

Binary Search in 60 seconds

Seven numbers, sorted but rotated, and you need the smallest. Reading all seven works, but it touches every number.

3 · Now you drive

Keys:←→ stepSpace playR resetHomeEnd jump

Find Minimum in Rotated Sorted Array

or try
1–10 different numbers from -99 to 99, sorted then rotated, like 4 5 6 1 2 3.
The minimum is somewhere in all 7 numbers. lo at 0, hi at 6.Step 1 of 8. Starting out. Search space index 0 to 6, all 7 numbers. The minimum is somewhere inside.
Step 1 of 8
solution.ts
function findMin(nums: number[]): number {
let lo = 0, hi = nums.length - 1;
while (lo < hi) {
const mid = Math.floor((lo + hi) / 2);
if (nums[mid] > nums[hi]) {
lo = mid + 1;
} else {
hi = mid;
}
}
return nums[lo];
}
▶ marks the line running now

4 · Your turn

0 of 9 problems solved

Start from this

let lo = 0, hi = nums.length - 1;
// Stop when lo and hi meet on the answer.
while (lo < hi) {
  // Rounds down, so mid is never hi.
  const mid = Math.floor((lo + hi) / 2);
  // mid is ruled out, so skip past it.
  if (answerIsRightOf(mid)) lo = mid + 1;
  // mid could be the answer, so keep it.
  else hi = mid;
}
return lo;

Easy ways to go wrong

  • Setting lo = mid, which loops forever when lo and hi are neighbours.
  • Mixing lo < hi with hi = mid - 1 without checking the last element.
  • Assuming it needs a sorted array. It needs a yes/no that flips once.
  • Searching on the answer with the wrong range. Koko’s speeds start at 1, and a split’s cap starts at the biggest number.

Find a position

Compare with the middle and keep the half that can still hold the answer.

  1. Easy01

    Binary Search

    LeetCode 704 · NeetCode 150

    The plain version: compare with the middle and drop half.

  2. Medium02

    Search a 2D Matrix

    LeetCode 74 · NeetCode 150

    Read the grid row by row as one long sorted list.

  3. Medium03

    Find First and Last Position of Element in Sorted Array

    LeetCode 34

    Search twice: once for the first copy, once for the first bigger value.

  4. Medium04

    Find Peak Element

    LeetCode 162

    No sorting needed: walk uphill and half the array drops away.

Rotated arrays

One half around the middle is always in order. Work out which one first.

  1. Medium05

    Find Minimum in Rotated Sorted Array

    LeetCode 153 · Blind 75 · NeetCode 150

    The one you just watched. Write it from memory.

  2. Medium06

    Search in Rotated Sorted Array

    LeetCode 33 · Blind 75 · NeetCode 150

    Same halving, but first ask which half is sorted.

Search on the answer

Guess an answer, check whether it works, and halve the range of answers.

  1. Medium07

    Koko Eating Bananas

    LeetCode 875 · NeetCode 150

    Halve the possible answers, not the input.

    Watch it run
  2. Hard08

    Split Array Largest Sum

    LeetCode 410

    Guess the largest sum, then check greedily how many pieces it needs.

  3. Hard09

    Median of Two Sorted Arrays

    LeetCode 4 · NeetCode 150

    Search for where to cut the shorter array, so both left parts make half.

UP NEXT · LESSON 7 OF 20Linked ListRewire pointers without losing the chain.