binary search, bias to the left
• 39 words • 1 min
• updated
Prefer:
mid = left + (right - left) // 2
Instead of:
mid = (left + right) // 2
…to avoid a potential integer overflow.
Not a real concern in Python, but still a best, defensive practice.
Related Posts
Prefer:
python mid = left + (right - left + 1) // 2 Prefer:
python mid = (left + right + 1) // 2 Instead of:
python mid = ((left + right) // 2) + 1
Dec 28, 2025
LeetCode #704: Binary Search:
From scratch, iteratively:
python class Solution: def search(self, nums: List[int], target: int) -> int: left = 0 …
Jan 03, 2026
Previously, previously, previously, previously.
Thiago Perrotta