Learning pathsPricingChecking sign-in…
Data structures

Binary search

Search a monotonic answer space.

Discard only a region proved impossible, state what remains possible, and separate search mechanics from predicate correctness.

Mechanism and state

Exact lookup keeps possible matching elements in a half-open interval and may return on equality. Lower/upper bounds find the first >=target or >target transition and must continue on equality according to the chosen predicate. A target range combines those transitions without target+1 arithmetic. Rotated distinct arrays identify a sorted side or compare mid to the right boundary; duplicates weaken these guarantees. Peak search instead preserves an interval containing some peak with downhill-facing outside edges: slopes need not be globally monotone. A globally row-major matrix permits virtual long indices mapped by division/remainder. Minimum-rate and shipping problems search an answer domain after separately proving a monotone feasibility test. Two-array median searches a shorter-array cut while the other cut is fixed by the combined left rank.

Why the pattern works

State whether bounds refer to elements or insertion/cut positions, and whether the interval is half-open or closed. For exact lookup, discarded values cannot match; for bounds, a certified false prefix and true suffix surround the answer boundary. For minimum/peak search, retain a nonempty interval containing the desired value or some valid witness, keeping mid when it may still be the answer. Every iteration must strictly shrink the relevant interval. Rate feasibility is a sum of independent per-pile ceiling hours, so faster rates cannot hurt; shipping feasibility uses a greedy ordered packing proof, not the same predicate. Median validity reduces to two cross inequalities because each array is sorted internally; an oversized A-left boundary rules out cuts to the right, while an oversized B-left boundary rules out cuts to the left.

Work through an example

For [1,2,2,4], target=2, an equality lookup may return index 2, while lower bound returns 1 and upper bound returns 3, giving inclusive occurrence range [1,2]. For [3,1,2], minimum search must retain mid=1 rather than discard the answer. A rising edge in [1,2,1,3,5,6,4] permits keeping a region containing peak 5 even though peak 1 is discarded. For piles [3,6,7,11], H=8, rate 4 takes [1,2,2,3] hours; rate 3 takes 10 and is infeasible. For shipping [2,3,2], D=2, capacities 5,4 imply high=5 then low=5. For median [1,2] and [3,4], cuts i=2,j=0 satisfy the cross comparisons and yield (2+3)/2=2.5.

Where the reasoning fails

Do not mix a closed loop with half-open endpoint updates, return an arbitrary duplicate for lower bound, or use target+1 at Integer.MAX_VALUE. Adjacent-unequal peaks need an existence proof rather than a monotone-slope claim. Rotated duplicate arrays and row/column-only matrix sorting require different algorithms. Keep virtual matrix products in long before multiplication. Rate predicates need per-pile ceilings and long accumulation; an early-exit prefix is not a full hours total. Shipping must preserve package order and distinguishes at-most-D from exactly-D. Median requires valid cut ranges, explicit missing boundaries, and widened middle-value addition. Runtime sortedness/shape validation would add linear preprocessing cost and must not be hidden in a logarithmic query claim.

Reconstruct the reasoning

For each problem name the answer domain, interval convention, retained invariant, exact discard proof, and progress measure. Trace a duplicate equality lookup and its lower/upper bounds; a minimum at mid; an endpoint peak at Integer.MIN_VALUE; a virtual matrix index crossing a row; a rejected rate with a partial checked prefix; and both median cut movement directions. Verify any-match/any-peak witnesses rather than overconstraining them to the baseline’s first answer. Keep construction/validation, query, auxiliary space, and output costs separate. Apply lower-bound search to DSA-133: prove minimum tails are ordered, and explain why the first tail ≥x implements strict rather than nondecreasing subsequences. The binary-search boundary is useful only after the dominance invariant is proved.

Apply this foundation

Your study notes