Binary Search Variants: Beyond Simple Lookup

Choose binary search for monotone predicates and ternary search for unimodal objectives. Learn lower bounds, rotated-array search, and termination invariants.

published: reading time: 29 min read author: Geek Workbench updated: November 6, 2026
Quick Summary

Binary search finds a boundary when a predicate changes monotonically, such as the first value greater than or equal to a target. Lower and upper bounds handle duplicates, rotated-array and peak searches use structure in neighboring ranges, and binary search on an answer works when feasibility changes once. Ternary search solves a different problem: locating a maximum or minimum of a unimodal objective.

Binary Search Variants: Beyond Simple Lookup

Binary search repeatedly halves a search interval: test the midpoint, discard a half that cannot contain the answer, and continue. Variants use the same idea to search rotated arrays, find first or last occurrences, locate peaks, and find the first feasible answer to an optimization problem.

For an exact lookup on sorted data, start with linear and binary search basics. This guide focuses on the invariants that make the less familiar variants correct.

Introduction

Binary search finds the boundary of a monotone predicate: as the input increases, the predicate changes from false to true once, or from true to false once. The predicate may describe an array position, an integer answer, or a point in a continuous interval.

Binary search takes O(log n) predicate checks, compared with O(n) for a linear scan, so the difference grows with the input size. The same idea applies to matrix peak searches, rotated arrays, and feasibility problems such as finding the minimum gas-station spacing.

This guide covers lower and upper bounds, rotated-array search, peak finding, monotone feasibility search, and ternary search for unimodal objectives. Each method depends on a different property of the search space.

When to Use

Binary search variants apply when:

  • Search space is monotonic — Condition transitions from false to true (or vice versa) exactly once
  • Searching in rotated sorted array — Array was sorted, then rotated at a pivot point
  • Finding first/last occurrence — Need leftmost or rightmost position of target
  • Monotone feasibility — A candidate answer works (or stops working) at one boundary
  • Unimodal objective — A function rises then falls (or falls then rises); use ternary search to locate its optimum
  • Peak finding — The adjacent slope can tell which side still contains a local peak

When Not to Use

Binary search depends on a clean monotonic property, and these three scenarios break it:

  • Unsorted data without exploitable structure — A random array with no monotone predicate, rotation, or usable neighboring-slope property cannot be halved safely; use a linear scan or establish a searchable structure first.

  • Finding all occurrences rather than boundaries — Binary search finds one boundary efficiently, but collecting every matching index requires scanning outward from that point or falling back to linear search. If you need every position where a value appears, binary search only gets you to the first match; finding the last one needs a second search or extra bookkeeping.

  • Problems where O(n) solutions are already fast enough — Binary search is not free. For small arrays (n < 100), the loop overhead and extra comparisons can cost more than a simple linear scan. If the data sits in cache or the search runs only once, linear search at microsecond speeds beats binary search.

Binary Search for a Monotone Predicate

graph TD
    A["Define predicate P(x)"] --> B["Is P(mid) true?"]
    B -->|Yes| C[Answer in left half or at mid]
    B -->|No| D[Answer in right half]
    C --> E[Move right = mid]
    D --> F[Move left = mid + 1]
    E --> B
    F --> B
    B -.->|Converge| G[Return left]

The predicate P(x) must be monotone. For example, P(x) = "capacity x can finish the job" may be false below a minimum capacity and true at or above it. Binary search finds that first true value; it does not optimize an arbitrary objective function.

Production Failure Scenarios

Failure Cause Mitigation
Infinite loop Using left < right with left = mid in odd-length ranges Use left <= right with proper mid calculation or left = mid + 1
Overflow in mid calculation (left + right) can overflow in large arrays Use mid = left + (right - left) // 2
Wrong boundary for first/last Confusing lower vs upper bound logic Draw the decision tree on paper before coding
Rotated array pivot error Assuming known rotation count Search both halves when target not in sorted portion

Choose the Variant

Goal and input shape Method Loop invariant / precondition
Exact value in sorted data Binary search If present, the target remains in the closed interval [lo, hi].
First >= target or first > target Lower or upper bound Known false values are left of lo; known true values are at or right of hi in [lo, hi).
Exact value in rotated sorted data Rotated-array search If present, the target remains in [lo, hi]; distinct values identify a sorted half.
First integer answer satisfying a constraint Binary search on a predicate The first feasible answer remains in [lo, hi], and feasible(hi) is true.
Boundary in a continuous interval Continuous binary search The predicate stays false at lo and true at hi.
Local peak in an array Peak finding At least one local peak remains in [lo, hi].
Maximum/minimum of a unimodal objective Ternary search At least one optimum remains in [lo, hi]; the objective is unimodal.

Duplicate, Empty, and Termination Cases

For arr = [1, 2, 2, 2, 5], lower_bound(arr, 2) returns 1 and upper_bound(arr, 2) returns 4; the matching values occupy [1, 4). For target 3, both return 4, an empty match range where a new 3 can be inserted. On an empty array, both return 0, the boundary of [0, 0).

Lower and upper bound use a half-open interval [lo, hi). At each step, either lo becomes mid + 1 or hi becomes mid, so hi - lo strictly decreases. The loop ends at lo == hi, including the empty-input case.

Implementation

Lower Bound (First Occurrence >= Target)

Invariant: Every index before left has a value < target; every index at or after right has a value >= target. The first index with arr[i] >= target, if one exists, stays inside [left, right).

def lower_bound(arr, target):
    """
    Find index of first element >= target.
    Returns len(arr) if target larger than all elements.
    """
    left, right = 0, len(arr)

    while left < right:
        mid = left + (right - left) // 2

        if arr[mid] < target:
            left = mid + 1
        else:
            right = mid

    return left

Upper Bound (First Occurrence > Target)

Invariant: Every index before left has a value <= target; every index at or after right has a value > target. The first index with arr[i] > target, if one exists, stays inside [left, right).

def upper_bound(arr, target):
    """
    Find index of first element > target.
    """
    left, right = 0, len(arr)

    while left < right:
        mid = left + (right - left) // 2

        if arr[mid] <= target:
            left = mid + 1
        else:
            right = mid

    return left

Search in Rotated Sorted Array

Invariant: If target is present, it remains in the closed interval [left, right]. With distinct values, at least one half is sorted, so the comparison with that half’s endpoints determines which side can be discarded.

def search_rotated(nums, target):
    """
    Search in rotated sorted array (no duplicates).
    Returns index if found, -1 otherwise.
    """
    left, right = 0, len(nums) - 1

    while left <= right:
        mid = left + (right - left) // 2

        if nums[mid] == target:
            return mid

        # Determine which half is sorted
        if nums[left] <= nums[mid]:
            # Left half sorted
            if nums[left] <= target < nums[mid]:
                right = mid - 1
            else:
                left = mid + 1
        else:
            # Right half sorted
            if nums[mid] < target <= nums[right]:
                left = mid + 1
            else:
                right = mid - 1

    return -1

If nums is empty, right starts at -1 and the loop returns -1 immediately. Otherwise, every miss removes mid from the closed interval, so the loop ends when left > right.

This implementation assumes distinct values. With duplicates such as [1, 0, 1, 1, 1], equal endpoints can hide which half is sorted; a duplicate-aware version must shrink an endpoint when the comparison is ambiguous and can take O(n) time in the worst case.

Peak Finding

Invariant: The closed interval [left, right] contains at least one local peak. If the slope rises from mid to mid + 1, a peak remains on the right; otherwise one remains at mid or to its left.

def find_peak(nums):
    """Return the index of a local peak, or -1 for an empty array."""
    if not nums:
        return -1

    left, right = 0, len(nums) - 1
    while left < right:
        mid = left + (right - left) // 2
        if nums[mid] < nums[mid + 1]:
            left = mid + 1
        else:
            right = mid

    return left

Each peak-finding iteration moves one endpoint inward; it terminates when the interval contains one index, which the invariant guarantees is a peak.

Binary Search on an Integer Answer

Invariant: feasible is monotone from false to true, and the first feasible value remains in [left, right]. The caller must provide an upper bound for which feasible(right) is true.

def first_feasible(left, right, feasible):
    """Return the smallest integer x in [left, right] where feasible(x) is true."""
    while left < right:
        mid = left + (right - left) // 2
        if feasible(mid):
            right = mid
        else:
            left = mid + 1
    return left

Because mid < right whenever left < right, each iteration shortens the integer interval. At termination, the remaining candidate is the first feasible answer.

Binary Search on a Continuous Monotone Predicate

Invariant: predicate(left) is false and predicate(right) is true, so the transition remains bracketed by [left, right]. Stop when the interval is narrower than the requested tolerance.

def binary_search_boundary(predicate, left, right, epsilon=1e-7):
    """Approximate a false-to-true boundary on a continuous interval."""
    while right - left > epsilon:
        mid = left + (right - left) / 2
        if predicate(mid):
            right = mid
        else:
            left = mid
    return right

Each iteration halves the bracketed interval, so the loop stops once its width is at most epsilon.

Ternary Search for a Unimodal Objective

Ternary search does not test a monotone feasibility predicate. It compares two interior points of a unimodal objective; for a maximum, the interval on the side with the lower value can be discarded.

Invariant: At least one maximizer of f remains inside [left, right]. Each iteration reduces the interval, and the tolerance controls the final approximation.

def ternary_search_maximize(f, left, right, epsilon=1e-7):
    """
    Approximate the maximum of a unimodal function on [left, right].
    """
    while right - left > epsilon:
        mid1 = left + (right - left) / 3
        mid2 = right - (right - left) / 3

        if f(mid1) < f(mid2):
            left = mid1
        else:
            right = mid2

    return (left + right) / 2

For a minimum instead of a maximum, minimize f by applying the same interval update to -f or by reversing the comparison. Each iteration retains at most two thirds of the interval, so the loop stops once its width is at most epsilon.

Common Pitfalls / Anti-Patterns

  1. Off-by-one in termination — left < right vs left <= right changes behavior; former searches for boundary, latter for exact match
  2. Mid calculation order — Always use left + (right - left) // 2 to prevent overflow
  3. Confusing sorted half detection — In rotated arrays, check nums[left] <= nums[mid] not just equality
  4. Not handling duplicates — Rotated array search with duplicates requires O(n) worst case

Quick Recap Checklist

  • Is the condition monotonic for binary search application?
  • Used overflow-safe mid calculation?
  • Correct termination condition (≤ vs <)?
  • For rotated array: confirmed which half is sorted?
  • Tested: empty array, single element, target at boundaries

Observability Checklist

Track binary search implementations to catch edge case failures.

Core Metrics

Binary search is simple enough that a few well-chosen metrics catch most failures:

Search iteration count is the primary diagnostic. For a sorted array of size n, binary search performs at most log₂(n) + 1 iterations. If your implementation consistently runs more than 2× expected iterations, the termination condition or boundary update logic is wrong. Log iteration count alongside input size to build a baseline: for n=1M, expected count is about 20. If you see 30+ consistently, something is wrong with how the search space is shrinking.

Left/right boundary values at termination matter because they reveal what the algorithm concluded. When binary search terminates with left > right, the target doesn’t exist in the array. Log these boundary values in your trace: if left and right ever cross without terminating, you have an infinite loop. If left equals right on termination, that’s a found element; if left > right, that’s a miss. Tracking these values helps diagnose off-by-one errors in the boundary update logic.

Input size and target value should always be logged together with the search result. A binary search that returns -1 for a target that exists in the array is a correctness bug, not a performance issue. Logging input size and target lets you replay the exact search scenario. For large-scale systems, sampling a small percentage of searches and logging their full context is enough to catch correctness bugs without overwhelming your logging infrastructure.

Comparison count per search is a proxy for cache performance. Binary search on sorted arrays has poor cache locality compared to linear search, each comparison jumps to a random memory location. If your use case involves searches that are hot (repeatedly searching the same array), consider a B-tree or interpolation search for better cache behavior. Tracking comparison count alongside array size tells you whether binary search is performing as expected or degenerating due to memory layout.

Health Signals

Binary search failures are usually binary themselves, either the algorithm terminates correctly or it doesn’t terminate at all. These health signals catch the non-terminating cases before they become production incidents:

Iteration count exceeding O(log n) by 2x or more is the earliest warning. The expected iteration count for binary search is predictable: log₂(n) rounded up. For n=1M, that’s 20 iterations. If you’re seeing 40+ in production, your boundary update logic is wrong, most likely you’re using mid + 1 or mid - 1 when you should be using mid directly, causing the search space to shrink by 1 element per iteration instead of halving it. This is an off-by-one error that doubles your iteration count. Catch it early by alerting on iteration count > 2 × log₂(n).

Boundary values not converging means left and right are not moving toward each other. In a correct binary search, left advances or right retreats at every iteration. If you log the boundary values and see them staying the same across iterations, the mid calculation is producing the same value repeatedly, which happens when left and right are consecutive values and the mid calculation rounds the same way each time. This is a direct path to an infinite loop.

Index values going negative or exceeding array bounds is a memory safety issue. Binary search on an empty array (left=0, right=-1) should return -1 immediately. If your implementation tries to access arr[-1] or arr[mid] when mid is out of bounds, you have a bounds check bug. This can cause segmentation faults in low-level languages or IndexError in Python. Alert immediately on any search that accesses an out-of-bounds index, it’s a correctness bug that could corrupt memory or crash the process.

Alerting Thresholds

Binary search alerting should be simple: the algorithm either converges or it doesn’t. These thresholds catch the most common failure modes:

Iteration count > 40 for n < 1B is an immediate red flag. For n < 1 billion, binary search should never need more than 30 iterations (log₂ 1B ≈ 30). If you’re seeing 40+, your boundary update logic has an off-by-one error. The most common cause: using left = mid + 1 when left = mid is correct (or vice versa), which halves the search space incorrectly and requires extra iterations. In the worst case, this can cause the search to never converge. Set this alert at 40 and investigate the boundary update logic immediately.

Any comparison where left > right is an invariant violation that should never happen in a correctly implemented binary search. When left exceeds right, the search space has inverted, the algorithm has overshot the base case. This usually indicates a bug in the termination condition, often triggered by a specific input pattern (like searching for a value at the array boundary). Because this is a correctness bug rather than a performance issue, alert on it immediately and include the full search context (input array, target, left/right values) in the alert payload.

Search time p99 > 10ms for a single operation is a performance alert. A binary search on an in-memory array should complete in microseconds. If p99 latency for a single search exceeds 10ms, the search is either hitting disk (array too large for memory), experiencing cache thrashing (poor spatial locality on large arrays), or contending for resources with other operations. This threshold is context-dependent, if your arrays are large and memory-constrained, 10ms might be normal. Calibrate based on your baseline, but set the alert high enough to avoid false positives from GC pauses.

Distributed Tracing

Binary search traces should be lightweight but informative enough to reconstruct the full search path:

One span per search operation is sufficient. Don’t create a span per iteration — binary search iterations are too fine-grained and will pollute your trace store. Instead, create a single span that records the input size, target value, result (found/not found), and iteration count. The iteration count is the most useful diagnostic: it reveals whether the search space halved correctly at each step.

Record the initial left/right bounds and the final bounds. The initial bounds tell you the search range; the final bounds tell you how the search terminated. If the search found the target, the final left/right should bracket the found index. If it didn’t find the target, left should equal right + 1. Logging both lets you replay the search manually if the result looks wrong.

Include the comparison count per search as a span tag. While iteration count and comparison count are usually the same in a simple binary search, they diverge in variants like lower-bound and upper-bound searches where the loop may continue after finding the target. Tracking comparison count separately from iteration count helps diagnose whether the search is doing more work than expected.

Correlate slow searches with array characteristics. A search that takes unusually long on a small array suggests memory pressure or resource contention. A search that takes unusually long on a large array in a memory-mapped context suggests disk I/O. Tag each search span with the array size and whether the array is memory-mapped or in-memory. Over time, this correlation reveals whether your search performance is bounded by CPU or memory.

Security Notes

Binary search has specific security concerns.

Timing attacks on sorted data

Binary search’s branching structure is a side channel. At each iteration, the algorithm compares the target to the middle element and branches left or right. An attacker who can measure execution time with sufficient precision can infer which branch was taken—which tells them whether the target is greater than or less than the middle element. After enough iterations, they reconstruct the entire search path and recover the target value.

This is a real attack vector in high-security contexts: password comparison, cryptographic key search, database index probing. The attack requires the ability to measure execution time with nanosecond precision, which is possible over network connections in low-latency environments. The attacker doesn’t need to see the data—they just need to observe how long the search takes.

The fix is constant-time comparison. Standard comparison operators (<, >, ==) in most languages are short-circuiting: they return as soon as the outcome is determined. A constant-time comparison always examines all bits of both values, taking the same time regardless of where they first differ. In Python, you can use ctypes or a timing-safe comparison library. In Go, use crypto/subtle.ConstantTimeCompare. In Java, use MessageDigest.isEqual().

Adding noise to timing is a weaker mitigation—it raises the precision required for the attack and increases the number of samples needed. It’s useful as a defense-in-depth measure but not sufficient on its own. The stronger approach is eliminating the timing difference entirely by making all comparisons take constant time.

Overflow in mid calculation

The overflow bug is subtle but devastating when it hits. The naive mid calculation mid = (left + right) // 2 adds left and right first. In a32-bit signed integer context, if left = 2 billion and right = 3 billion, the sum overflows the32-bit range and wraps to a negative number. Dividing a negative number by 2 gives a negative mid index, which causes an out-of-bounds array access or an infinite loop depending on the language and the boundary update logic.

This bug is dormant in most production systems because inputs rarely reach the overflow threshold. It surfaces when binary search is applied to very large arrays, particularly in languages with 32-bit integers (C, C++, Java’s primitive int types). Python’s arbitrary-precision integers don’t overflow in the same way, but the fix is still worth applying universally because it makes the code portable and signals intent.

The safe formula mid = left + (right - left) // 2 never overflows because the subtraction happens first. For left=2B and right=3B: (3B - 2B) = 1B, then 1B // 2 = 500M. Both operations are safe within 32-bit range. Apply this formula universally, not just when you anticipate large inputs. The overhead is identical—a single subtraction and a division—and it removes an entire class of hard-to-reproduce bugs that only manifest in production on large arrays.

Interview Questions

1. How do you search in a rotated array with duplicates?

With duplicates, the worst case becomes O(n) because you can't determine which half is sorted when nums[left] == nums[mid] == nums[right]. The algorithm must fallback to linear search when this ambiguity occurs. If duplicates are guaranteed, consider sorting first or using a different approach.

2. What's the boundary-finding perspective of binary search?

Think of binary search as finding where a predicate P(x) transitions from false to true. For "first occurrence >= target", the predicate is arr[i] < target. Binary search finds the leftmost index where the predicate becomes false. This perspective handles first/last occurrence, lower/upper bound, and optimization problems uniformly.

3. How do you find the minimum in a rotated sorted array?

The minimum element is the pivot point where the sorted sequence restarts. Use binary search: if nums[mid] > nums[right], minimum is in right half (left = mid + 1). Otherwise, minimum is in left half including mid (right = mid). The answer is at nums[left] when left == right.

4. How would you find the peak element in a mountain array using binary search?

A mountain array (bitonic array) increases then decreases with exactly one peak. Use binary search: compare arr[mid] with arr[mid + 1]. If arr[mid] < arr[mid + 1], the peak is in the right half (left = mid + 1). Otherwise, the peak is in the left half including mid (right = mid). When left == right, that index is the peak.

5. How do you search in a sorted infinite array or array of unknown size?

Since binary search requires boundaries, you first determine the search range exponentially:

  • Start with left = 0, right = 1. Double right while arr[right] < target (or until you get an out-of-bounds indicator).
  • Once arr[right] >= target, perform standard binary search within [left, right].
  • This exponential search combined with binary search runs in O(log pos) where pos is the target position.
6. How would you find the square root of a number using binary search?

Treat it as a binary search on the answer:

  • Set left = 0, right = x (or x // 2 + 1 for efficiency).
  • While left <= right, compute mid = left + (right - left) // 2.
  • If mid * mid == x, return mid. If mid * mid < x, left = mid + 1. Otherwise right = mid - 1.
  • Return right (floor of square root). For floating-point, use abs(mid * mid - x) < epsilon.
7. Explain how to find the first bad version in a version control system using binary search.

This is the canonical first true in a monotonic predicate problem.

  • Define isBadVersion(n) — returns true if version n is bad. All versions after the first bad are also bad.
  • Use lower-bound binary search: left = 1, right = n; while left < right, if isBadVersion(mid) is false, left = mid + 1, else right = mid.
  • Return left — the first bad version. Time: O(log n), space: O(1).
8. How do you count occurrences of a target in a sorted array using binary search variants?

Use lower bound and upper bound together:

  • Find first = lower_bound(arr, target) (first index >= target).
  • Find last = upper_bound(arr, target) (first index > target).
  • Count = last - first. If arr[first] != target, count is 0.
  • Total time: O(log n), two binary searches. Can optimize to one search by finding any occurrence first, then expanding.
9. How do you find the floor and ceiling of a target in a sorted array?

Floor = largest element ≤ target. Ceiling = smallest element ≥ target.

  • Ceiling is simply lower_bound(arr, target). Return arr[lower_bound] if it exists.
  • Floor: compute upper_bound(arr, target), then floor is at upper_bound - 1 (if valid).
  • Alternatively, use modified binary search tracking the best candidate. Both run in O(log n).
10. What is binary search on the answer and when would you use it?

Binary search on the answer applies when the answer space is continuous or integer-ranged, and you can verify whether a candidate answer satisfies the problem constraints.

  • Key property: The feasibility function is monotonic — if answer x works, all larger (or smaller) answers also work.
  • Examples: Minimum capacity to ship packages within D days, smallest k such that koko can eat all bananas in H hours, square root, n-th root.
  • Pattern: Binary search over the answer range [lo, hi], check predicate feasible(mid), narrow until convergence. Time: O(log(range) × feasibility_check_time).
11. How do you search in a row-wise and column-wise sorted 2D matrix?

For a matrix where each row is sorted and each column is sorted, start from the top-right corner:

  • If matrix[row][col] == target, return true.
  • If matrix[row][col] > target, move left (col--) — this column is too large.
  • If matrix[row][col] < target, move down (row++) — this row is too small.
  • Time: O(m + n). Cannot use binary search on both dimensions simultaneously because the monotonic property does not hold in 2D.
12. Find the index where the sum of left elements equals the sum of right elements (equilibrium index).

Use binary search on the prefix sum concept:

  • Compute total sum, then iterate with left_sum = 0, right_sum = total - arr[i].
  • If left_sum == right_sum, return i. Update left_sum += arr[i] each step.
  • Binary search variant: for monotonic checking, use total - left_sum - arr[mid] vs left_sum to decide which side to search. Time: O(log n) if array is monotonic, O(n) for general case since prefix sums are not monotonic.
13. How do you find the position to insert an element in a sorted array to maintain sorted order?

Use bisect functions or implement lower/upper bound:

  • bisect_left returns index of first element >= target (lower bound). Use for insertion that keeps sorted.
  • bisect_right returns index of first element > target (upper bound). Use when you want all equal elements grouped at insertion point.
  • Implementation: binary search with left < right loop; arr[mid] < target means go right, else go left. Return left.
  • Edge cases: insert at beginning (returns 0), insert at end (returns len(arr)).
14. Find the minimum days to ship all packages given a daily capacity (LeetCode 1011).

Classic binary search on answer: the minimum capacity is between max(weights) and sum(weights).

  • Predicate: can ship all packages in D days with capacity C? Simulate: accumulate weights, split when daily_sum exceeds C.
  • Binary search over [max(arr), sum(arr)]. For mid, count required days. If days > D, increase capacity; else decrease.
  • Time: O(n log(sum - max(arr))). Space: O(1).
15. Find the minimum element in a rotated sorted array with duplicates — how does it change the algorithm?

With duplicates, worst-case becomes O(n). The standard algorithm:

  • If nums[mid] > nums[right], minimum in right half (left = mid + 1).
  • If nums[mid] < nums[right], minimum in left half (right = mid).
  • If nums[mid] == nums[right], you cannot decide — decrement right by one and continue. This handles duplicates.
  • When duplicates are prevalent, linear search may be needed in the worst case.
16. How do you find a target in a bitonic array (strictly increasing then strictly decreasing)?

Use binary search in two phases: first find the peak, then search each half.

  • Find peak: compare arr[mid] with arr[mid+1]. If increasing, left = mid + 1; else right = mid.
  • After peak is found (left == right), binary search left half (0 to peak) with increasing condition.
  • Binary search right half (peak to n-1) with decreasing condition (treat as reversed sorted or use reverse comparison).
  • Time: O(log n) for finding peak + O(log n) for search = O(log n) total.
17. How do you find the kth smallest element in the union of two sorted arrays?

Use binary search on the answer or median-of-medians approach:

  • Binary search on value range: let low = max(arr1[0], arr2[0]), high = min(arr1[-1], arr2[-1]).
  • Count elements <= mid in both arrays using lower_bound. If count >= k, high = mid; else low = mid + 1.
  • Alternative: use two-pointer approach starting from k/2 in each array. Time: O(log k) for binary search, O(k) for pointer approach.
18. Find the median of two sorted arrays (LeetCode 4) — when should you use binary search versus divide and conquer?

Use binary search on partition: partition array1 at index i and array2 at index j such that:

  • i + j = (len1 + len2 + 1) // 2 for median (including even/odd handling).
  • Elements left of partition <= elements right of partition. Binary search on i (or j) to find valid partition.
  • If arr1[i-1] <= arr2[j] and arr2[j-1] <= arr1[i], partition is correct. Median is max(left) and min(right).
  • Time: O(log(min(len1, len2))). Space: O(1). This is preferred over merge (O(n) time, O(n) space).
19. When would you use lower bound versus upper bound — what's the practical difference?

Lower bound finds first position where value >= target; upper bound finds first position where value > target.

  • Lower bound: use when you need the insertion point that keeps sorted order, or first occurrence. Example: find first bad version, first position to place an element.
  • Upper bound: use when you need the position after all equal elements, or for counting occurrences (upper - lower gives count).
  • Implementation difference: lower checks arr[mid] < target, upper checks arr[mid] <= target. The = in upper bound skips equal elements.
20. What are the most common bugs when implementing binary search and how do you avoid them?

Critical bugs that cause incorrect results or infinite loops:

  • Infinite loop: left = mid without +1 when using left < right. Fix: use left = mid + 1 or right = mid - 1.
  • Overflow: mid = (left + right) // 2 overflows for large values. Fix: mid = left + (right - left) // 2.
  • Off-by-one at boundaries: wrong termination condition. left <= right for exact match; left < right for boundary finding.
  • Wrong half selection: when comparing arr[mid] to target, ensure direction is correct for the predicate.
  • Always test with: empty array, single element, target at both ends, target not in array.

Further Reading

To deepen your understanding of binary search variants, explore these resources:

  • Competitive Programmer’s Handbook (CSES) — Excellent chapter on binary search and bisection methods
  • “Binary Search” by Errichto — YouTube video covering advanced binary search techniques
  • LeetCode Explore Card: Binary Search — Interactive learning path with curated problems
  • “The Power of Binary Search” — Codeforces Blog — Advanced applications of binary search for optimization
  • Introduction to Algorithms (CLRS) — Chapter 12 covers binary search trees and searching fundamentals
  • Algorithm Design Manual (Skiena) — Practical guidance on implementing binary search variants

Conclusion

Binary search is appropriate when sorted order or a monotone predicate lets you discard half the remaining candidates. Ternary search solves a different problem: it narrows a unimodal objective toward its maximum or minimum. For either method, state the interval invariant before choosing the update rule. Use left + (right - left) // 2 for integer midpoints, and match the loop condition to the interval convention: left < right for half-open boundaries, left <= right for exact search on a closed interval.

Category

Related Posts

Bit Manipulation: Power of Bits

Apply masks, shifts, and popcount patterns safely. Learn how integer width, signedness, language rules, and overflow affect bit operations.

#bit-manipulation #algorithms #bitwise

Common Coding Interview Patterns

Learn five coding interview patterns, derive constraints before choosing one, and practice each with a recognition problem, variation, and mixed exercise.

#coding-interview #problem-solving #patterns

Divide and Conquer: Breaking Problems into Subproblems

Learn divide and conquer through merge sort, quicksort, and binary search, with recurrences that show how subproblem costs add up.

#divide-and-conquer #algorithms #merge-sort