java.lang.Object
g2201_2300.s2208_minimum_operations_to_halve_array_sum.Solution

public class Solution extends Object
2208 - Minimum Operations to Halve Array Sum.<p>Medium</p> <p>You are given an array <code>nums</code> of positive integers. In one operation, you can choose <strong>any</strong> number from <code>nums</code> and reduce it to <strong>exactly</strong> half the number. (Note that you may choose this reduced number in future operations.)</p> <p>Return <em>the <strong>minimum</strong> number of operations to reduce the sum of</em> <code>nums</code> <em>by <strong>at least</strong> half.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [5,19,8,1]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> The initial sum of nums is equal to 5 + 19 + 8 + 1 = 33.</p> <p>The following is one of the ways to reduce the sum by at least half:</p> <p>Pick the number 19 and reduce it to 9.5.</p> <p>Pick the number 9.5 and reduce it to 4.75.</p> <p>Pick the number 8 and reduce it to 4.</p> <p>The final array is [5, 4.75, 4, 1] with a total sum of 5 + 4.75 + 4 + 1 = 14.75.</p> <p>The sum of nums has been reduced by 33 - 14.75 = 18.25, which is at least half of the initial sum, 18.25 >= 33/2 = 16.5.</p> <p>Overall, 3 operations were used so we return 3.</p> <p>It can be shown that we cannot reduce the sum by at least half in less than 3 operations.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [3,8,20]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> The initial sum of nums is equal to 3 + 8 + 20 = 31.</p> <p>The following is one of the ways to reduce the sum by at least half:</p> <p>Pick the number 20 and reduce it to 10.</p> <p>Pick the number 10 and reduce it to 5.</p> <p>Pick the number 3 and reduce it to 1.5.</p> <p>The final array is [1.5, 8, 5] with a total sum of 1.5 + 8 + 5 = 14.5.</p> <p>The sum of nums has been reduced by 31 - 14.5 = 16.5, which is at least half of the initial sum, 16.5 >= 31/2 = 16.5.</p> <p>Overall, 3 operations were used so we return 3.</p> <p>It can be shown that we cannot reduce the sum by at least half in less than 3 operations.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> <li><code>1 <= nums[i] <= 10<sup>7</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • halveArray

      public int halveArray(int[] nums)