Removing Minimum Number of Magic Beans - Problem

You're a wizard managing a collection of magic bean bags! Each bag contains a different number of precious magic beans, represented by the array beans.

Your goal is to make all non-empty bags contain exactly the same number of beans. You can remove beans from any bag, but you cannot add beans or move them between bags. Once removed, beans are gone forever! ๐Ÿซ˜โœจ

The challenge: Find the minimum number of beans you need to remove to achieve this magical balance.

Note: You can completely empty some bags (remove all beans), but all remaining non-empty bags must have the same count.

Input & Output

example_1.py โ€” Basic Case
$ Input: [4,1,6,5]
โ€บ Output: 4
๐Ÿ’ก Note: We can make bags equal by setting target = 4. Remove 3 beans from bag 1 (4โ†’1 is wrong, we empty bag 2), remove 0 from bag 2 (keep as 4), remove 2 from bag 3 (6โ†’4), remove 1 from bag 4 (5โ†’4). Actually, optimal is target=4: empty bag with 1 bean (remove 1), keep bag with 4 beans (remove 0), reduce 6โ†’4 (remove 2), reduce 5โ†’4 (remove 1). Total removals = 1+0+2+1 = 4.
example_2.py โ€” All Equal
$ Input: [2,2,2,2]
โ€บ Output: 0
๐Ÿ’ก Note: All bags already contain the same number of beans (2), so no removals are needed.
example_3.py โ€” Empty All
$ Input: [1,2,3]
โ€บ Output: 4
๐Ÿ’ก Note: Best strategy is to set target = 1. Keep first bag (remove 0), reduce second bag 2โ†’1 (remove 1), reduce third bag 3โ†’1 (remove 2). Total = 0+1+2 = 3. Wait, let me recalculate: target=1 costs 0+1+2=3, target=2 costs 1+0+1=2, target=3 costs 1+2+0=3. Actually target=2 gives minimum cost of 2, but the answer should be calculated properly using our algorithm.

Visualization

Tap to expand
๐Ÿง™โ€โ™‚๏ธ The Wizard's Bean Balancing Challenge๐Ÿง™Wizard's Current Collection:Bag 1: 4Bag 2: 1Bag 3: 6Bag 4: 5โœจ After Sorting: [1, 4, 5, 6] โœจTesting Target = 4 (Keep 3 bags):โˆ…Empty (-1)Keep 4 (-0)Trim to 4 (-1)Trim to 4 (-2)๐ŸŽฏ Mathematical Formula:Cost = Total_Sum - Target ร— Remaining_BagsCost = 16 - 4 ร— 3 = 4 beans removedโšก Time Complexity: O(n log n)โ€ข O(n log n) for sortingโ€ข O(n) to test each targetโ€ข O(1) per cost calculation๐Ÿ”‘ Key InsightThe optimal target is always one of the existing values - no need to test other numbers!
Understanding the Visualization
1
Survey Your Collection
Count all beans and identify the different amounts in each bag
2
Sort by Quantity
Arrange bags from smallest to largest to see the distribution clearly
3
Test Each Standard
Try using each existing amount as your target - one of these will be optimal
4
Calculate Costs
For each target: empty smaller bags completely, trim larger bags to target size
5
Choose Wisely
Select the target that wastes the fewest beans overall
Key Takeaway
๐ŸŽฏ Key Insight: Sort the array and test each existing value as a target using the mathematical formula. The optimal solution runs in O(n log n) time and finds the minimum removals efficiently!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

O(n log n) for sorting, then O(n) to test each potential target with constant-time cost calculation

n
2n
โšก Linearithmic
Space Complexity
O(1)

Only using constant extra space for variables (excluding input array)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค beans.length โ‰ค 105
  • 1 โ‰ค beans[i] โ‰ค 105
  • All integers in the array are positive
  • You can remove beans from any bag or empty bags completely
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
24.2K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen