Write the flag first.
transcript
One hundred thousand ids, sorted. One question: where does an id begin? Scanning is slow. Today you learn the one habit behind seven problems. A flag makes it fast. Let me show you how.
Write the question as a flag. False first, then true. It never goes back. Say it aloud before you code. If you cannot say it, you cannot search it.
Find the first id at least eight. The middle is eight. True. A five is false. Move past it. The first true is position three. The loop never asked if eight exists. It found where eight begins. Then you check equality.
Be careful with equal values. Take running totals three, five, ten. The limit is five. A strict flag skips the five. At least keeps it.
A hundred thousand queries. Seventeen steps each. Scanning would need ten billion checks. The flag brings that down to seventeen steps.
Write the flag. Find the first true. Check the edges. Full breakdown in the lecture.