TL;DR
- Is the problem finding a specific value or a boundary?
- Specific value: Use
left <= right
. - Boundary: Use
left < right
.
- Specific value: Use
- Does
mid
need to be included as a possible answer?- Yes: Use
left = mid
orright = mid
. - No: Use
left = mid + 1
orright = mid - 1
.
- Yes: Use
Intro
If you’re given a sorted list or they request an algorithm that has TC O(logn), your to-go algorithm should generally be Binary Search. Implementing a binary search is actually easy, but the loop condition and assigning the left and right values might differ from problem to problem. In this post, I tried to understand and explain what to use in which case.
Loop Condition: left < right
vs. left <= right
left < right
- Use this when the termination condition is when the search space is reduced to a single element.
- After the loop,
left
will be the index of the result.
Example: Find the minimum element in a rotated sorted array.
- The loop continues until
left == right
, and you returnnums[left]
.
left <= right
- Use this when you want to check all elements in the search space, including the possibility of
left == right
at some point in the loop. - Typically used when the loop checks for equality or an exact match (e.g., finding a target value).
Example: Search for a specific element in a sorted array.
- The loop exits when
left > right
, meaning the target is not present.
Assignments: left = mid + 1
vs. left = mid
left = mid + 1
- Use this when you are certain that
mid
is not the answer and the next possible candidate is to the right ofmid
.
Example:
- When the condition specifies the target must be in the right half (e.g.,
nums[mid] < target
in standard binary search).
left = mid
- Use this when
mid
could still be the answer. - Often used when the problem involves finding a boundary or minimum/maximum.
Example:
- When looking for the minimum in a rotated array, the
mid
itself might be the minimum, so you don’t exclude it.
Assignments: right = mid - 1
vs. right = mid
right = mid - 1
- Use this when you are certain that
mid
is not the answer, and the next possible candidate is to the left ofmid
.
Example:
- When the condition specifies the target must be in the left half (e.g.,
nums[mid] > target
).
right = mid
- Use this when
mid
could still be the answer. - Common in problems where you need to find the minimum or maximum.
Example:
- Finding the boundary or the smallest/largest value in a rotated array.