AlgoMaster Logo

Apple Redistribution into Boxes

Last Updated: March 29, 2026

easy

Understanding the Problem

We have some packs of apples and some boxes. Each pack has a certain number of apples, and each box can hold a certain number of apples. We want to use as few boxes as possible to fit all the apples.

The key detail is that apples from the same pack can be split across different boxes. This means we don't need to worry about fitting individual packs into individual boxes. We just need the total capacity of our chosen boxes to be at least the total number of apples.

So the problem reduces to: find the minimum number of boxes whose combined capacity is at least the total apple count. To minimize the number of boxes, we should pick the largest boxes first. That's the greedy insight.

Key Constraints:

  • 1 <= n, m <= 50 → Both arrays are tiny, at most 50 elements. Even O(n^3) or O(n^2) are more than fast enough. Sorting is trivially efficient here.
  • 1 <= apple[i], capacity[j] <= 50 → Values are small positive integers. No overflow concerns, no negative numbers to handle.
  • The input guarantees a valid solution exists, so we never need to return -1 or handle the impossible case.

Approach 1: Brute Force (Check All Subsets)

Intuition

The most direct way to find the minimum number of boxes: try every possible subset of boxes, check if the subset's total capacity is enough, and return the smallest valid subset size.

This is what you'd do if you were thinking purely in terms of "which combination of boxes works?" For each subset of size k (starting from k = 1), check if any subset of that size has enough capacity. The first k where we find a valid subset is the answer.

Algorithm

  1. Compute the total number of apples across all packs.
  2. Try all subsets of the capacity array, starting from the smallest subsets.
  3. For each subset, sum the capacities.
  4. If the sum is at least the total apples, return the subset size.

Code

The brute force tries every combination of boxes. But if we want to minimize the count, we should always pick the biggest boxes first, since each large box gets us closer to the target faster. What if we just sorted the boxes by size and greedily picked from the top?

Approach 2: Greedy with Sorting (Optimal)

Intuition

Since apples can be split freely across boxes, the only thing that matters is the total capacity of the boxes we choose. We want to reach the total apple count using as few boxes as possible.

To minimize the number of boxes, we should always pick the box with the largest remaining capacity. This is a classic greedy choice: at each step, take the option that makes the most progress toward our goal. There's no reason to pick a smaller box when a larger one is available, because the larger box gets us closer to the target with fewer selections.

So the approach is simple: sort the capacities in descending order, then keep adding boxes until the cumulative capacity meets or exceeds the total apples.

Algorithm

  1. Compute the total number of apples by summing the apple array.
  2. Sort the capacity array in descending order.
  3. Initialize a running sum of capacity to 0.
  4. Iterate through the sorted capacities, adding each to the running sum.
  5. As soon as the running sum reaches or exceeds the total apples, return the number of boxes used.

Example Walkthrough

1Total apples = 1+3+2 = 6. Sort capacity descending: [5, 4, 3, 2, 1]
0
5
1
4
2
3
3
2
4
1
1/4

Code