Class Solution
-
- All Implemented Interfaces:
public final class Solution3634 - Minimum Removals to Balance Array.
Medium
You are given an integer array
numsand an integerk.An array is considered balanced if the value of its maximum element is at most
ktimes the minimum element.You may remove any number of elements from
numswithout making it empty.Return the minimum number of elements to remove so that the remaining array is balanced.
Note: An array of size 1 is considered balanced as its maximum and minimum are equal, and the condition always holds true.
Example 1:
Input: nums = 2,1,5, k = 2
Output: 1
Explanation:
Remove
nums[2] = 5to getnums = [2, 1].Now
max = 2,min = 1andmax <= min * kas2 <= 1 * 2. Thus, the answer is 1.
Example 2:
Input: nums = 1,6,2,9, k = 3
Output: 2
Explanation:
Remove
nums[0] = 1andnums[3] = 9to getnums = [6, 2].Now
max = 6,min = 2andmax <= min * kas6 <= 2 * 3. Thus, the answer is 2.
Example 3:
Input: nums = 4,6, k = 2
Output: 0
Explanation:
Since
numsis already balanced as6 <= 4 * 2, no elements need to be removed.
Constraints:
<code>1 <= nums.length <= 10<sup>5</sup></code>
<code>1 <= numsi<= 10<sup>9</sup></code>
<code>1 <= k <= 10<sup>5</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerminRemoval(IntArray nums, Integer k)-
-
Method Detail
-
minRemoval
final Integer minRemoval(IntArray nums, Integer k)
-
-
-
-