Maintained by Vikas Dulgunde, software engineer
Packages sit on a conveyor belt with fixed weights, and they must be loaded onto the ship in exactly that order. Each day the ship picks up a contiguous run of the next packages, as long as their combined weight stays within the ship's capacity. You cannot reorder packages and you cannot split one package between two days.
You are given a target number of days. A larger ship can carry more per day and finish in fewer days; a smaller ship needs more days. Your job is to find the smallest capacity that still gets everything shipped on time.
The answer is bounded on both sides. Capacity can never be less than the heaviest single package, because that one package must fit in a day on its own. Capacity never needs to exceed the total weight, because that would let the whole belt ship in a single day. The minimum valid capacity lives somewhere in that range.
Capacity 15 supports a 5-day plan: [1,2,3,4,5]=15, [6,7]=13, [8]=8, [9]=9, [10]=10. Any capacity below 15 forces a sixth day, so 15 is the minimum.
Capacity 6 supports a 3-day plan: [3,2]=5, [2,4]=6, [1,4]=5, which loads every package in order across exactly 3 days. Capacity 5 cannot fit [2,4] together and ends up needing a fourth day, so 6 is the minimum.
Capacity 3 gives [1,2]=3, [3]=3, [1,1]=2, which is only three days, so there is slack to spare. The heaviest package is 3, so capacity can never drop below 3, and 3 already finishes within 4 days, making it the answer.
Visible test cases
If someone hands you a candidate capacity, can you quickly decide whether it is enough? Greedily fill each day until the next package would overflow, then start a new day, and count the days.
Notice the days needed is monotonic in capacity: as capacity grows, the day count only ever decreases or stays the same. That monotonic boolean (does this capacity finish in time?) is exactly what binary search needs.
Search the capacity range. The low bound is the maximum single weight (it must fit alone); the high bound is the total of all weights (one day for everything). Binary search for the smallest capacity that passes the feasibility check.
Try every capacity starting from the heaviest package and moving up, returning the first one that ships within the day limit.
The feasibility test (does capacity C finish within days days?) is monotonic, so binary search the capacity range instead of scanning it linearly.