October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Binary Search for the Minimum Feasible Answer

Binary search can locate a boundary in possible answers—not just an array—when a bounded, ordered candidate range has a monotone feasibility check.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Binary search can find an answer even when there is no sorted array. Instead, search an ordered range of possible answers and ask a yes-or-no question about each midpoint: does this candidate satisfy the requirement? This works when the answer range is bounded and the predicate changes direction only once—for example, when candidates go from infeasible to feasible.

What does it mean to binary-search the answer?

In ordinary binary search, each comparison rules out part of a sorted array. In answer-space search, the candidates are possible answers—often integer values such as a time, capacity, or minimum rate. A decision function such as check(k) determines whether a proposed answer k meets the constraint.

As an Amazon Associate I earn from qualifying purchases.

The goal is not necessarily to find any candidate that works. It is often to locate the boundary: the smallest value that works, or the largest value that still works. Princeton Competitive Programming describes the shift as searching “over the space of possible answers to the problem,” rather than over an array (Princeton Competitive Programming, Fall 2026 Week 3).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When is answer-space binary search valid?

Two conditions must hold: the candidates have an order, and the decision predicate is monotone across that order. Monotone means the truth value changes at most once.

  • False then true: small candidates fail and larger ones eventually pass. Search for the first true value.
  • True then false: small candidates pass and larger ones eventually fail. Search for the last true value.

For example, suppose check(k) asks whether a group of cooks can make at least t meals within k minutes. If the cooks can meet the target in k minutes, they can also meet it with more time. Feasibility therefore moves from false to true, and the minimum feasible time is the first true candidate. Princeton uses this kind of production question to illustrate converting an optimization problem into a decision problem and checking monotonicity (Princeton Competitive Programming).

If feasibility alternates—true, false, then true again—discarding half the candidates is not justified. A numeric answer alone does not make binary search appropriate; the predicate must have the required shape.

How to find the first feasible integer

Use an inclusive interval [lo, hi] that is known to contain the first feasible value, with hi feasible. At each step, test the lower midpoint. A feasible midpoint may itself be the first true value, so retain it; an infeasible midpoint and everything below it can be discarded.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
while lo < hi:
    mid = lo + (hi - lo) // 2
    if check(mid):
        hi = mid
    else:
        lo = mid + 1
answer = lo

The loop invariant is: the first feasible value remains in [lo, hi], and hi is feasible. When the endpoints meet, that sole remaining candidate is the boundary. The midpoint expression avoids the overflow risk of adding lo and hi directly in fixed-width integer types, assuming the difference itself is representable.

Before the loop, establish valid bounds. For example, if zero time is impossible and a known upper bound is sufficient, use those facts to set an infeasible lower endpoint and feasible upper endpoint. If no feasible upper bound exists, the search has not yet been properly bounded; find one or handle the impossibility separately. A sentinel can be useful, but only if its infeasibility or feasibility is actually established.

How to find the last feasible integer

For a true-then-false predicate, keep an interval containing the last feasible value and establish that lo is feasible. Use an upper midpoint so a two-value interval makes progress:

while lo < hi:
    mid = lo + (hi - lo + 1) // 2
    if check(mid):
        lo = mid
    else:
        hi = mid - 1
answer = lo

The invariant is that the last feasible candidate remains in [lo, hi], with lo feasible. If mid passes, it may be the last feasible value, so retain it as the lower endpoint. If it fails, the boundary must be below it. The upward rounding matters: with lo = 4 and hi = 5, a lower midpoint would be 4; if 4 passes, assigning lo = mid would leave the interval unchanged.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

These two searches look similar, but their midpoint rounding and updates follow different invariants. Choose the pattern from the direction of the predicate, not from a preference for one template. The Codeforces tutorial covers both boundary patterns and common implementation hazards (Codeforces binary-search tutorial).

How to build a useful check function

Define the check as a precise question about one candidate. In the cook example, check(k) asks whether total production by time k reaches at least t. For each cook, calculate how many meals they can produce in that time, add the amounts, and stop once the target is reached.

Then assess the check separately for correctness and cost. If one check scans n cooks, its cost is O(n). Searching an integer range of width R takes O(log R) checks, so total time is O(n log R). When the relevant range is about t, Princeton gives Θ(n log t) or better as a target for its stated constraints; this is a theoretical complexity bound, not a benchmark (Princeton Competitive Programming; course handout).

If a check also sorts data, runs dynamic programming, or traverses a graph, include that work in its cost. Binary search reduces the number of candidates tested; it does not make an expensive predicate free.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Bounds, arithmetic, and edge cases

  • Prove the boundary is inside the interval. Bounds that merely seem large enough can silently exclude the answer.
  • Prevent overflow inside the check. A running total may overflow before reaching the target. Stop as soon as the target is met, or use safe accumulation appropriate to the numeric type.
  • Guarantee progress. Every update must make the interval smaller. The upper midpoint in last-true search prevents the two-candidate stall.
  • Check boundary cases. Consider a one-value interval, a boundary at either endpoint, all candidates feasible, and no candidate feasible. Handle the latter explicitly if the chosen bounds do not guarantee a solution.
  • Keep the domain consistent. The integer templates assume discrete integer candidates. Do not apply them unchanged to real-valued answers.

Princeton specifically warns that large possible answers and integer overflow matter in its production example. The Codeforces tutorial also discusses incorrect monotonicity assumptions, poor bounds, midpoint overflow, and loops that fail to progress (Princeton Competitive Programming; Codeforces tutorial).

What changes for real-valued answers?

For a continuous domain, maintain a bracket around the transition and stop according to a justified tolerance or a specified iteration budget. If the initial interval has width W, each bisection halves it; after m iterations its width is at most W / 2m. Choose m based on the required precision and starting width rather than adopting a universal count.

State which endpoint the algorithm returns and what it represents—for example, a feasible upper approximation or an infeasible lower approximation. Floating-point rounding and the meaning of feasibility within an epsilon can affect the predicate, so the tolerance must match the problem’s requirements. The Codeforces tutorial discusses fixed-iteration bisection as one common approach (Codeforces tutorial).

A correctness checklist

  1. Write the exact yes-or-no predicate for one candidate.
  2. Prove whether it is false-to-true or true-to-false; do not rely on intuition alone.
  3. Choose first-true or last-true search to match that direction.
  4. Establish bounds that contain the transition and satisfy the invariant’s endpoint assumptions.
  5. Use safe midpoint and predicate arithmetic for the numeric range involved.
  6. Verify every update preserves the invariant and strictly shrinks the interval.
  7. Test endpoint, degenerate, and no-solution cases, and for real values justify the stopping rule.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.