Removing Minimum Number of Magic Beans

You are given an array of positive integers beans, where each integer represents the number of magic beans found in a particular magic bag.

Remove any number of beans (possibly none) from each bag such that the number of beans in each remaining non-empty bag (still containing at least one bean) is equal. Once a bean has been removed from a bag, you are not allowed to return it to any of the bags.

Return the minimum number of magic beans that you have to remove.

Example 1
Inputbeans = [4,1,6,5]
Output4
Removing 1 bean from the bag with 1 bean, 2 beans from the bag with 6 beans, and 1 bean from the bag with 5 beans makes the remaining non-empty bags all contain 4 beans, for a total of 4 removed beans.
Example 2
Inputbeans = [2,10,3,2]
Output7
Removing both bags with 2 beans entirely and the bag with 3 beans entirely leaves only the bag with 10 beans non-empty, for a total of 7 removed beans.

Constraints

  • 1 <= beans.length <= 10^5
  • 1 <= beans[i] <= 10^5

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate