Capacity to Ship Packages
A medium Binary Search problem included in Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Binary Search
- Sheets
- 1
- Core for
- 7 roles
- Platform
- LeetCode
The problem
A conveyor belt has packages with weights that must be shipped within a given number of days. Each day, packages are loaded onto the ship in order and the total weight cannot exceed the ship's capacity. Find the minimum capacity needed to ship all packages within the given days.
Example 1
- Input
- weights = [1,2,3,4,5,6,7,8,9,10], days = 5
- Output
- 15
- Why
- A capacity of 15 allows shipping: day 1: [1,2,3,4,5], day 2: [6,7], day 3: [8], day 4: [9], day 5: [10].
Example 2
- Input
- weights = [3,2,2,4,1,4], days = 3
- Output
- 6
- Why
- A capacity of 6 allows shipping: day 1: [3,2,1], day 2: [2,4], day 3: [4].
Constraints
- 1 <= days <= weights.length <= 5 * 10^4
- 1 <= weights[i] <= 500
How to think about it
Updated 2026-09-09The packages must stay in their original arrival sequence. If a ship of capacity C can pack everything within the allotted days, any capacity larger than C can as well, making the feasibility predicate monotonic over capacity.
Approaches, worst first
Incremental capacity probe
time O(sum(weights) * n) · space O(1)
Start capacity at the heaviest single package and test increments sequentially by simulating greedy daily loading until days used fits within the limit. Correct but wastes time testing impossible capacities.
Binary search on capacityWrite this one
time O(n log(sum(weights))) · space O(1)
Search capacity between the maximum single weight and the sum of all weights. Simulate packing sequentially: pack packages greedily into the current day and start a new day whenever adding the next exceeds the trial capacity.
Where people lose marks · 3
- Setting the lower bound of capacity below max(weights) allows the simulation to test capacities that cannot load a single package, creating infinite loops or wrong days counts.
- Reordering packages to fit more per day violates the problem requirement that shipping must preserve sequence order.
- Starting the day counter at 0 instead of 1 underestimates required days by one when the final day remains open.
The theory behind it
Binary Search — the ground this problem stands on. All Binary Search problems
What Binary Search is
Binary search is the guessing strategy used when searching a thick telephone directory or guessing a secret number between one and a hundred. Rather than inspecting names one by one from the first page, the search opens straight to the middle page. If the target precedes that middle entry, the entire back half is discarded; if it follows, the front half is eliminated. Repeating this halved split finds the item with remarkable speed.
When to reach for it
Reach for binary search when queries target sorted collections, rotated sorted arrays, or monotonic answer spaces. Strong hints include logarithmic time constraints like O(log n) or search ranges exceeding one billion where stepping one value at a time times out. It also applies when validating whether a guessed solution is possible via a monotonic boolean check, known as binary search on answer.
How the pattern works
Define the search territory with two inclusive pointers, low and high. Compute the midpoint using low plus half the difference to high, avoiding integer overflow. Formulate an exact boolean condition that divides the range into true and false halves. Decide whether the boundary condition includes the midpoint or shifts strictly past it. The loop invariant states that the sought target, if it exists, remains trapped inside the interval throughout every iteration.
What each operation costs
| Operation | Time |
|---|---|
| find target in sorted array | O(log n) |
| find boundary in monotonic range | O(log n) |
What usually goes wrong with Binary Search
- Triggering integer overflow by computing middle using low plus high divided by two instead of low plus half of high minus low in fixed-width numeric types.
- Creating an infinite loop when the interval shrinks to two elements by setting low equal to mid when mid was rounded down.
- Mismatched loop condition and bounds updates, such as pairing low less than or equal to high with non-advancing pointer assignments that never terminate.
Which roles need this problem
Binary Search is a core topic for these 7 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 17 more roles, including Full-Stack Developer, Data Engineer, Android Developer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Binary Search problems
Problem set and role mapping as of .