java.lang.Object
g2101_2200.s2171_removing_minimum_number_of_magic_beans.Solution

public class Solution extends Object
2171 - Removing Minimum Number of Magic Beans.<p>Medium</p> <p>You are given an array of <strong>positive</strong> integers <code>beans</code>, where each integer represents the number of magic beans found in a particular magic bag.</p> <p><strong>Remove</strong> any number of beans ( <strong>possibly none</strong> ) from each bag such that the number of beans in each remaining <strong>non-empty</strong> bag (still containing <strong>at least one</strong> bean) is <strong>equal</strong>. Once a bean has been removed from a bag, you are <strong>not</strong> allowed to return it to any of the bags.</p> <p>Return <em>the <strong>minimum</strong> number of magic beans that you have to remove</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> beans = [4, <strong>1</strong> ,6,5]</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>We remove 1 bean from the bag with only 1 bean.</p> <p>This results in the remaining bags: [4, <strong>0</strong> ,6,5]</p> </li> <li> <p>Then we remove 2 beans from the bag with 6 beans.</p> <p>This results in the remaining bags: [4,0, <strong>4</strong> ,5]</p> </li> <li> <p>Then we remove 1 bean from the bag with 5 beans.</p> <p>This results in the remaining bags: [4,0,4, <strong>4</strong> ]</p> </li> </ul> <p>We removed a total of 1 + 2 + 1 = 4 beans to make the remaining non-empty bags have an equal number of beans.</p> <p>There are no other solutions that remove 4 beans or fewer.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> beans = [<strong>2</strong> ,10, <strong>3</strong> , <strong>2</strong> ]</p> <p><strong>Output:</strong> 7</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>We remove 2 beans from one of the bags with 2 beans.</p> <p>This results in the remaining bags: [<strong>0</strong> ,10,3,2]</p> </li> <li> <p>Then we remove 2 beans from the other bag with 2 beans.</p> <p>This results in the remaining bags: [0,10,3, <strong>0</strong> ]</p> </li> <li> <p>Then we remove 3 beans from the bag with 3 beans.</p> <p>This results in the remaining bags: [0,10, <strong>0</strong> ,0]</p> </li> </ul> <p>We removed a total of 2 + 2 + 3 = 7 beans to make the remaining non-empty bags have an equal number of beans.</p> <p>There are no other solutions that removes 7 beans or fewer.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= beans.length <= 10<sup>5</sup></code></li> <li><code>1 <= beans[i] <= 10<sup>5</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minimumRemoval

      public long minimumRemoval(int[] beans)