Rearrange apples into boxes by moving apples between boxes until every box meets the target condition.
You are given an array representing the number of apples in each box. Your task is to redistribute apples among the boxes so that the final arrangement satisfies the problem's target condition with the minimum necessary redistribution effort.
A valid solution should determine whether the redistribution can be achieved and, if the problem asks for it, compute the minimum number of apples that must be moved. Think carefully about what values must stay fixed and what values can change while preserving the overall total.
Input Format
- An integer array describing the apples in each box.
- Additional parameters may describe the target condition or limit for each box, depending on the version of the problem.
Output Format
- Return the minimum redistribution effort, or whether the redistribution is possible, according to the target condition.
- If the task is impossible, return the appropriate failure value specified by the version you are solving.
Constraints
- The total number of apples is finite and the array size is within practical interview limits.
- Use a solution that is linear or near-linear in the number of boxes whenever possible.
- Exact official constraints are not provided in the source metadata.
Example 1
Input
apples = [1, 3, 2, 4]
Output
true
Explanation
This is an illustrative example. If the target condition is satisfied after redistributing apples while preserving the total number of apples, the configuration is feasible.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.