You are given an array of integers arr and an integer k. Remove exactly k elements from arr so that as few distinct integers as possible remain, and return that smallest possible number of distinct integers.
Function Signature
def find_least_num_of_unique_ints(arr: list[int], k: int) -> int:
Rules
-
Each removal deletes one element, that is, one occurrence of a value. Making a value disappear completely takes as many removals as it has occurrences.
-
You may remove elements from any positions. The order of the remaining elements does not matter.
-
If every element is removed, the answer is 0.
Constraints
-
1 <= len(arr) <= 10^5
-
1 <= arr[i] <= 10^5
-
0 <= k <= len(arr)
Examples
Input: arr = [5, 5, 4], k = 1
Output: 1
Removing the single 4 leaves [5, 5], which has one distinct integer. Removing a 5 instead would leave both 4 and 5.
Input: arr = [4, 3, 1, 1, 3, 3, 2], k = 3
Output: 2
Removing 4, 2 and one of the two 1s leaves [3, 1, 3, 3], whose distinct values are 1 and 3. Getting down to a single distinct value would take at least 4 removals (keep only the three 3s), so 2 is the best possible.
Input: arr = [1, 2, 3], k = 3
Output: 0
Here k equals the array length, so every element is removed and nothing remains.