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.

&nbsp;
Example 1:

Input: beans = [4,1,6,5]
Output: 4
Explanation: 
- We remove 1 bean from the bag with only 1 bean.
  This results in the remaining bags: [4,0,6,5]
- Then we remove 2 beans from the bag with 6 beans.
  This results in the remaining bags: [4,0,4,5]
- Then we remove 1 bean from the bag with 5 beans.
  This results in the remaining bags: [4,0,4,4]
We removed a total of 1 + 2 + 1 = 4 beans to make the remaining non-empty bags have an equal number of beans.
There are no other solutions that remove 4 beans or fewer.


Example 2:

Input: beans = [2,10,3,2]
Output: 7
Explanation:
- We remove 2 beans from one of the bags with 2 beans.
  This results in the remaining bags: [0,10,3,2]
- Then we remove 2 beans from the other bag with 2 beans.
  This results in the remaining bags: [0,10,3,0]
- Then we remove 3 beans from the bag with 3 beans. 
  This results in the remaining bags: [0,10,0,0]
We removed a total of 2 + 2 + 3 = 7 beans to make the remaining non-empty bags have an equal number of beans.
There are no other solutions that removes 7 beans or fewer.


&nbsp;
Constraints:


	1 &lt;= beans.length &lt;= 105
	1 &lt;= beans[i] &lt;= 105


class Solution:
    def minimumRemoval(self, A: List[int]) -> int:
        return sum(A) - max((len(A) - i) * n for i, n in enumerate(sorted(A)))
class Solution {
    public long minimumRemoval(int[] beans) {
        Arrays.parallelSort(beans);
        long sum=0,min=Long.MAX_VALUE;
        int n=beans.length;
        for(int i:beans)
            sum+=i;
        for(int i=0;i<n;i++)
        {
            long temp=sum-((n-i+0L)*beans[i]);
            min=(long)Math.min(min,temp);
           
        }
        return min;
        
        
        
    }
}
class Solution {
public:
    long long minimumRemoval(vector<int>& beans) {
      long long n = beans.size();
      sort(beans.begin(), beans.end());
      long long sum = 0;
            for(int i=0; i<n; i++)
            {
                    sum += beans[i];
            }
       long long ans = sum;
       for(int i=0; i<n; i++)
       {
            long long curr = sum - (n-i)*beans[i];
            if(ans > curr)
            ans = curr;
       }
      return ans;      
    }    
};
/**
 * @param {number[]} beans
 * @return {number}
 */
 // time complexity -> O(NlogN) and Space is O(logN) due to sorting. 
 var minimumRemoval = function(beans) {
    beans.sort((a, b) => a - b);
    let frontSum = beans.reduce((sum , a) => sum + a, 0);
    let backSum = 0;
    let done = 0;
    let result = Number.MAX_SAFE_INTEGER;
    for(let j = beans.length - 1; j >= 0; j--){
        frontSum -= beans[j];
        count = frontSum + (backSum - (beans[j] * done));
        result = Math.min(result, count);
        done++;
        backSum += beans[j];
    }
    return result;
};
Removing Minimum Number of Magic Beans