Binary Search (index & answer-space)

Use for search in sorted, “min x such that feasible(x)”, peaks, rotated arrays.

What this pattern is

For sorted arrays, find positions (lower/upper bounds) in O(log n). For optimization problems, binary search the answer and check feasibility in O(n log R).

Exercise: lowerBound

Task: Return the first index i where nums[i] >= target (or nums.length if all < target).
Example: nums=[1,2,4,4,5], target=4 → 2
Edge cases: all smaller, all larger, duplicates.

Your Solution

Exercise: shipWithinDays

Task: Given weights[] and days, find the minimum ship capacity so all packages ship within days using order-preserving loads.
Example: weights=[1,2,3,1,1], days=4 → 3
Edge cases: one huge weight (lower bound), tight days, many small items.

Your Solution