- The N-th root of an integer
- Matrix Median
- Single Element in a Sorted Array
- 5. Search in Rotated Sorted Array
- Median of Two Sorted Arrays
- Kth element of 2 sorted arrays
- Allocate Minimum Number of Pages
- Aggressive Cows
This is the #1 most common point of confusion in Binary Search.
Whether you use l <= r or l < r depends entirely on how you update your pointers (l and r) and where you return the answer.
Comparison at a Glance
| Feature | Pattern 1: while (l <= r) | Pattern 2: while (l < r) |
|---|---|---|
| Search Space | Range [l, r] until empty (l > r) | Shrinement until 1 element remains (l == r) |
| Check inside loop | Direct match check (return nums[mid]) | Condition check to eliminate half the array |
| Pointer updates | l = mid + 1 AND r = mid - 1 | l = mid + 1 AND r = mid (or m) |
| Return statement | Returns inside the loop (or -1 outside) | Returns after the loop (return nums[l]) |
Pattern 1: while (l <= r) — The “Find & Return” Pattern
Use this when you can directly identify if mid is your answer during the current iteration.
Key Mechanics:
- You explicitly check: “Is
nums[mid]the target/answer?” - If yes
return nums[mid]immediately. - If no You know for sure
midis NOT the answer, so you can safely excludemidfrom both sides:
l = mid + 1r = mid - 1
- The loop stops when
l > r(search space becomes 0).
Example (Subarray Size Check for Single Element):
while (l <= r) {
int m = l + (r - l) / 2;
// 1. Direct check: Is 'm' the single element?
if ((m == 0 || nums[m] != nums[m - 1]) &&
(m == n - 1 || nums[m] != nums[m + 1])) {
return nums[m]; // FOUND IT! Return immediately.
}
// 2. Safely eliminate 'm' from both sides
if (leftSizeIsOdd) {
r = m - 1; // 'm' is definitely not it, exclude 'm'
} else {
l = m + 1; // 'm' is definitely not it, exclude 'm'
}
}
return -1;
Pattern 2: while (l < r) — The “Boundary Shrinking” Pattern
Use this when you cannot easily check if mid is the answer directly, or when mid might still be a potential candidate for the final answer.
Key Mechanics:
- You do NOT return inside the loop.
- You only ask: “Is the answer on the right side, or could it be at/left of
mid?” - Because
midmight still be the single element, you **cannot dor = mid - 1**. You must keepmidin your search space by doingr = mid. - The loop stops as soon as
l == r(only 1 element left in the search space). That remaining element must be the answer. - Return
nums[l]after the loop ends.
Example (mid ^ 1 XOR Trick for Single Element):
while (l < r) {
int m = l + (r - l) / 2;
if (nums[m] == nums[m ^ 1]) {
l = m + 1; // Pair is valid -> answer is strictly to the right
} else {
r = m; // Answer COULD be 'm' itself, so don't do 'm - 1'!
}
}
// Loop terminates when l == r (Search space shrunk to 1 element)
return nums[l];
How to Choose in an Interview (Simple Rule of Thumb)
-
Ask yourself: “Can I write an
ifstatement to check ifnums[mid]is the answer RIGHT NOW?” -
YES Use
while (l <= r). -
Update:
l = mid + 1andr = mid - 1. -
Return inside the loop.
-
NO (or it requires ugly boundary checks) Use
while (l < r). -
Update:
l = mid + 1andr = mid. -
Return
nums[l]after the loop.