CODE THE HERO

Home › Learn › World 4: Binary Peaks

PREMIUM

Square Root Cliff

Binary search on the answer

Level 4.5 of Code the Hero, in Binary Peaks: halve the mountain with binary search.

What you practise

Binary search on the answer. You write real Python, Java or JavaScript, press Run, and the hero acts out every line, so you can see why the fast way is fast.

What it costs

TimeO(log n)
Extra spaceO(1)

How the stars are earned

  1. Correct on every test
  2. At most √n + 2 checks
  3. At most log₂(n+1) + 1 checks

Your code is also checked on hidden inputs, so it has to work for any case, not only the one on screen.

▶ Start free with World 1See Premium

Worlds 1–3 (21 levels) are free. Binary Peaks is part of Premium.

More in Binary Peaks