binary search in 3 days
I couldn’t get a single word in on day 1. Here’s what actually changed.
Contents
Three days ago I sat down to study binary search and completely fell apart. I knew the logic. I’ve seen rotated arrays before. I’ve solved some of these problems in the past. And yet when I opened the problem, nothing came out. Wrong code, wrong thinking, frustration, closed the laptop.
binary search in 3 days
I wrote in my notes that day: “I am not getting a word.”
Today I solved Koko Eating Bananas, Minimum Days to Make Bouquets, Aggressive Cows, and Book Allocation. Debugged my own wrong code on two of them. Understood why it was wrong. Moved on.
So what changed? Honestly — not that much. And that’s the point.
The real problem wasn’t binary search
I was sick. I was behind schedule.
I had this voice in my head saying “if you don’t finish this today the whole plan falls apart.” And when you’re trying to think through a rotated array problem with that noise running, you’re not actually thinking — you’re panicking.
Binary search is one of those topics that punishes panic specifically. The logic is clean. The implementation has these tiny decisions — low + (high-low)/2 instead of (low+high)/2, whether to do <= or < in the while loop, when to update ans vs when to move the pointer — and if you’re rushing, you mix styles without realizing it.
Everything looks right. Nothing works.
Day 2, I just rested. Day 3, I came back with one goal: remove the fear. Not finish the playlist. Not hit a problem count. Just — stop being afraid of it.
Start with something you can actually solve
Before touching the hard problems I went back to floor square root. Easy problem. I wrote it out fully, with comments, slowly. It worked first try.
That sounds small. It wasn’t. It told my brain “you know how to do this.” Sometimes that’s the only thing standing between you and the rest of the session.
If you’re stuck on binary search: Don’t start with rotated arrays. Start with something that lets you write the template cleanly — lower bound, search insert position, floor sqrt. Get one clean AC. Then move.
The two patterns that actually matter
Here’s what nobody tells you clearly enough: almost every binary search problem in a contest is one of two things.
Pattern 1
Binary search on a sorted (or rotated) array
Classic. You’re searching for a value or a position. The array is sorted, possibly rotated. You figure out which half is sorted, eliminate the other. Lower bound, upper bound, first/last occurrence, rotated array — all variations of this.
Pattern 2
Binary search on the answer
This one is less obvious but once you see it, it’s everywhere. Instead of searching for a value in an array, you’re searching for the optimal answer in a range. The key question: “can I achieve this result?” If yes/no is monotonic — meaning once it becomes possible it stays possible — binary search works. Koko, Bouquets, Aggressive Cows, Book Allocation. All this pattern.
That’s it. Two patterns. Everything else is a variation.
Walking through the pattern 2 problems
Koko Eating Bananas
Binary search on eating speed. Range: 1 to max(piles). For each mid speed, check: can Koko finish all piles in h hours? If yes, try slower. If no, go faster. One useful trick: ceiling division without floats — (p + k - 1) / k.
Minimum Days to Make Bouquets
Binary search on days. Range: min(bloomDay) to max(bloomDay). For a given number of days, count how many bouquets of k consecutive bloomed flowers you can make. If it’s enough, try fewer days. The canMake function: iterate the array, count consecutive bloomed flowers, reset on any unbloom.
Aggressive Cows — the mistake I made
Binary search on minimum distance between cows. I initially set the range as min adjacent gap to max adjacent gap. Wrong. Cows don’t have to go in adjacent stalls. The correct range is 1 to stalls.back() - stalls.front(). Sort the stalls first. Then for each mid distance, greedily check if you can place all k cows with at least that much gap between them.
Book Allocation — the off-by-one I missed
Binary search on max pages per student. The canAllocate function starts with students = 1, not 0 — because you always have at least one student reading. And the return condition is students <= k, not == k — because using fewer students than available is still valid. These two things look small. In a contest at 11pm they’ll cost you 20 minutes if they’re not locked in.
What I’d tell someone starting binary search today
Don’t try to memorize implementations. Understand what the search space is, understand what you’re eliminating and why, and then write it. If you mix up the template mid-problem, slow down and dry run it on a small example. Three minutes of dry running saves thirty minutes of debugging.
The rotated array problems feel hard because there are two decisions happening at once — finding the sorted half, and deciding if your target is in it. Do them slowly the first time. Draw it out. Once the intuition lands it doesn’t leave.
And if you’re having a day where nothing clicks — stop. Rest. Come back tomorrow. Binary search specifically requires a calm head. It’s not a grind-through topic. It’s a think-clearly topic.
The thing that actually fixed it:I stopped trying to finish and started trying to understand. One problem fully understood is worth five problems half-done and forgotten by morning.
binary search in 3 days binary search in 3 days binary search in 3 days binary search in 3 days binary search in 3 days binary search in 3 days binary search in 3 days binary search in 3 days