Data Structures and Algorithms • C# examples
Lower Bound, Upper Bound, and Binary Search on Answers
Reuse binary search for insertion points and monotonic feasibility problems.
Concept
Lower bound finds the first position whose value is not less than a target; upper bound finds the first position greater than it. The same pattern can search a numeric answer space when a feasibility predicate changes monotonically from false to true. Define boundary behavior before coding.
How to approach it
Use a half-open interval [low, high), and move one boundary according to a monotonic predicate.
Complexity
O(log n) predicate checks, excluding the cost of each predicate evaluation.
C# example
int low = 0, high = values.Length;
while (low < high)
{
int mid = low + (high - low) / 2;
if (CanFinish(mid)) high = mid;
else low = mid + 1;
}
Expected result or behavior
The loop finds the first feasible value if CanFinish is monotonic and true somewhere in the search range.
Practice exercise
State and test the monotonicity assumption for a minimum-capacity scheduling problem.
Key takeaway: Binary search on answers requires a provably monotonic yes/no condition.