Note on Binary Search Methods

Last modified: July 21, 2026

L<=R vs L < R - When to use which one?

Yeah that’s a common trap β€” L <= R is the more “natural” feeling loop because it feels exhaustive, but it’s actually the harder variant to get right for this class of problems.

Quick mental model for when to use which:

L <= R β€” you’re searching for an exact target. Loop ends when the space is empty, you return inside the loop when you find it. Classic binary search on a sorted array.

L < R β€” you’re searching for a position/condition. Loop ends when L == R, which is your answer. No need to return inside β€” the convergence is the answer.

This problem is the second type β€” you’re not looking for a specific value, you’re converging on a position that satisfies a property. So L < R fits naturally and the boundary math just works out clean.

Rule of thumb: if you’re writing return mid inside the loop, L <= R is fine. If you find yourself wanting to return L after the loop, switch to L < R.