Hello again. Last time, you reframed binary search as finding a boundary in a Boolean predicate over an ordered domain: typically a sequence of false values followed by true values. You also saw that exact equality is often the wrong predicate, because duplicates can create a false–true–false pattern.
This lesson sharpens the key judgment that must happen before implementation: given a proposed predicate, can its truth values support binary search? You will learn to identify the required orientation, prove monotonicity from the problem’s rules, find concise counterexamples when it fails, and repair common non-monotone formulations.
The formal test: what binary search is allowed to assume
Let be an ordered search domain, and let be a Boolean predicate over candidates .
For a first-true search, the required condition is:
Equivalently,
Treating false as and true as , is a nondecreasing Boolean function. Its complete sequence must have the shape
For a last-true search, the direct predicate has the reverse orientation:
Both are usable. The difference is the boundary you intend to find.
| Search objective | Required predicate shape | Interpretation |
|---|---|---|
| First true | false values, then true values | Find the minimum qualifying candidate |
| Last true | true values, then false values | Find the maximum qualifying candidate |
The all-false and all-true cases are valid monotone cases. There may be no transition inside the domain, but the predicate still never reverses direction.
Read the formal explanation in Binary Search from Topcoder. It states the central theorem behind boundary search and gives a useful shortcut for proving monotonicity on an integer domain.
In the opening formal discussion, read the main theorem. Focus on why a true answer allows the algorithm to discard candidates to its right, while a false answer allows it to discard candidates to its left. Then continue in the following discussion on designing and proving predicates. Read the integer-domain shortcut. Note the important practical claim: on consecutive integers, proving the implication from x to x+1 is enough.
The phrase “monotone predicate” is therefore not a vague statement that the input is somehow sorted. It is a precise promise:
Once the predicate becomes true, it cannot later become false.
That promise is what makes a midpoint informative. If is true, every candidate to the right is also true, so none can be the first true candidate. If is false, every candidate to the left is also false, so none can be the answer.
Without this propagation property, discarding half the domain is unjustified.
Monotonicity is relative to the domain and its order
A predicate is not monotone “in isolation.” It is monotone with respect to an ordered domain.
For example, suppose:
and the domain is the index set , ordered by increasing index.
The predicate
evaluates to:
It is monotone because the vector is sorted: if an element at index is at least , every later element is at least as large.
But sorted input does not make every predicate over the input monotone. Consider:
For
we get:
The indices are ordered and the values are sorted, but parity does not propagate to the right. A true result at index tells you nothing about index .
This is a frequent competitive-programming mistake:
“The array is sorted, so I can binary search this condition.”
The repair is to ask a more specific question:
“If this condition is true at one index, what fact forces it to remain true at every later index?”
If there is no answer, the predicate is not suitable for first-true binary search.

The same principle applies when the domain is not an array. Candidates may be capacities, days, scores, distances, or answer values. What matters is that candidates have an order in which a constraint consistently becomes easier or consistently becomes harder.
Binary Search - A Different Perspective | Python Algorithms
Watch Binary Search: A Different Perspective by mCoding for a compact visualization of the shift from sorted values to a monotone Boolean property.
Watch the core abstraction to see why binary search relies on the grouping of Boolean outcomes rather than on sortedness as an end in itself. Then watch the predicate choice, which contrasts the predicates for left and right insertion boundaries.
A reliable monotonicity diagnosis
When someone proposes a predicate, do not begin by simulating binary-search updates. First run this short analysis.
1. State the candidates and their order
Write the domain explicitly.
Examples:
- indices , ordered left to right;
- capacities from through , ordered from smaller to larger;
- days from onward, ordered chronologically;
- a sorted list of possible scores, ordered increasingly.
The order is part of the claim. A condition may be monotone in one order and non-monotone in another.
2. Choose the intended orientation
For a minimization problem, use a predicate such as:
If larger makes the constraint easier, seek the first true.
For a maximization problem, use:
If larger makes the constraint harder, seek the last true.
You can always complement a valid true-then-false predicate:
This gives a false-then-true predicate. In practice, however, it is often clearer to keep the predicate expressed in the problem’s natural language and select the matching boundary template later.
3. Look for a one-directional change in constraints
The strongest monotonicity proofs come from constraint relaxation.
Suppose means:
All packages can be shipped within days using capacity .
As increases, the capacity constraint becomes weaker. Any schedule that works at capacity also works unchanged at capacity . Therefore:
and, by repetition,
This is a complete proof of first-true monotonicity. Notice that it does not depend on the eventual greedy implementation of the feasibility test. Monotonicity follows directly from the meaning of “capacity at most .”
4. Try to break it with a reversal
For a first-true predicate, you need only find two candidates such that:
That single pair disproves monotonicity.
For a last-true predicate, look for:
A small counterexample is usually more useful than an abstract objection: it identifies exactly what information binary search would wrongly discard.
5. Check the predicate’s exact wording
Words such as exactly, equal, odd, divisible, and contains should make you cautious. They often describe scattered valid candidates rather than a threshold.
Words such as at least, at most, no later than, within, and can accommodate often signal a monotone feasibility condition—but only after you prove how changing the candidate relaxes or tightens the requirement.
Worked classifications: valid, invalid, and repaired predicates
Equality in a sorted vector: invalid
Task: find whether a sorted vector contains target.
A tempting predicate is:
For
the values are:
This predicate changes twice. It is neither first-true nor last-true monotone.
The issue is semantic: equality describes a region of matching values, not a threshold separating the whole domain into two sides.
Repair: use a threshold predicate:
This is false-then-true. Find its first true position, then validate whether that position contains target.
Alternatively, to find the position after all duplicates:
This is also false-then-true.
“The event occurs on day ”: invalid
Suppose an event happens on exactly one day, say day . Consider:
Its truth values resemble:
A single true value in the middle is not a boundary. Discovering that the event does not occur on day tells you nothing about whether it occurred on day .
Repair: change the question to a cumulative one:
If the event is irreversible, then once is true it remains true on all later days. Now the first true day is the event day.
This repair appears constantly in scheduling and “earliest time” problems: replace an exact-time property with a “by this time” feasibility property.
Feasible workload cap: valid
Suppose a set of sequential jobs must be assigned to at most workers. Let:
This is first-true monotone.
Proof: If a particular partition satisfies a workload cap of , the same partition also satisfies any larger cap . Increasing the cap cannot invalidate an already valid partition.
The important detail is that the proof concerns the existence of a valid partition. You need not know which partition will be used at each value of ; one valid partition at is enough to establish feasibility for every larger bound.
Absolute value in a sorted vector: generally invalid
Suppose is sorted and you propose:
For
the predicate values are:
The absolute value decreases toward zero and then increases again. Sortedness of does not imply sortedness of .
A direct counterexample already proves this predicate cannot support an ordinary boundary search over all indices.
Possible repairs depend on the actual task:
- If the desired property is “first value at least ,” use .
- If the task concerns absolute values, split the problem around zero, or use a different ordered candidate domain.
- If the array is known to contain only nonnegative values, then , and the predicate becomes monotone.
The general lesson is that an operation applied to sorted values must itself preserve order before it can justify a threshold predicate.
Rotated sorted array with an ordinary threshold: invalid
For a rotated sorted vector,
consider:
The outcomes are:
There are two transitions, so the usual lower-bound predicate over the whole index range fails.
This does not mean rotated arrays are impossible to search. It means this particular predicate-domain pair is unsuitable. Later, you will use structural predicates based on which half is ordered or where the rotation boundary lies.
Proof patterns worth memorizing
For interview explanations and contest write-ups, a monotonicity proof is often just one or two sentences. These patterns cover much of answer-space binary search.
| Candidate increases | Typical predicate | Proof idea | Orientation |
|---|---|---|---|
| Capacity | Can all items fit? | More capacity relaxes a limit. | First true |
| Deadline | Can all work finish by then? | More time allows every earlier schedule. | First true |
| Processing speed | Can all work finish by deadline? | More speed cannot require more time. | First true |
| Minimum separation | Can at least items be placed? | More required distance makes placement harder. | Last true |
| Target score | Is this score achievable? | A higher target is at least as demanding. | Last true |
| Allowed error | Is approximation acceptable? | Larger tolerance relaxes acceptance. | First true |
Be precise about direction. “The predicate is monotone” is incomplete unless you indicate which way it moves.
For example, with a minimum-distance placement problem, let:
If is true, then every smaller separation requirement is also true. Thus the pattern is true-then-false, and you seek the last true distance.
A common error is to use the correct feasibility function but reason in the wrong direction. State the implication explicitly:
That one line exposes the correct orientation.
Sampling helps debugging, but it is not a proof
For a small example, writing a truth table is an excellent sanity check. It quickly reveals patterns like:
or
which are immediate disqualifiers.
But sampled values cannot prove monotonicity over a large domain. A predicate may look monotone at the endpoints and midpoint while reversing in between. For example:
could appear harmless if you sampled only the first, middle, and last positions poorly.
Use two levels of validation:
- Semantic proof: explain why a true candidate stays true as candidates increase, or why a true candidate stays true as candidates decrease.
- Small-instance testing: enumerate candidate values for tiny random cases and inspect the resulting Boolean sequence.
The proof establishes correctness. Small tests catch an incorrect implementation of a predicate whose intended logic was monotone.
Also separate two concerns:
- Monotonicity: does binary search have the right to discard half the candidate space?
- Predicate correctness and cost: does your
feasible(x)function return the right answer quickly enough?
A perfectly monotone predicate that costs too much may still be impractical. A fast predicate that is not monotone cannot safely be binary-searched at all.
Key takeaways
Binary search requires a Boolean predicate whose values make at most one directional transition over a clearly ordered domain.
- For first true, prove that true persists to the right.
- For last true, prove that true persists to the left.
- A sorted input does not automatically make an arbitrary condition over its indices monotone.
- Equality and “exactly” conditions are often non-monotone; threshold or cumulative formulations frequently repair them.
- The most convincing monotonicity proofs come from showing that increasing a candidate consistently relaxes, or consistently tightens, the problem’s constraints.
- A single reversal is enough to reject a proposed predicate.
Next, you will turn a valid first-true formulation into a precise search contract, choosing between inclusive and half-open intervals while preserving correct behavior for all-true, all-false, and empty domains.
Can't find a good explanation? Sign up and we'll make it for you
Sign up