Problem Description
Given an array of integers arr and an integer k, remove exactly k elements from the array such that the number of unique integers remaining is minimized.
Key Insights
- Count the frequency of each integer in arr.
- Removing numbers with lower frequency first reduces the number of unique integers effectively.
- Use a greedy approach by sorting integer frequencies in ascending order.
- Iteratively remove integers completely (if possible) until k removals are exhausted.
Space and Time Complexity
Time Complexity: O(n log n) due to sorting the frequency counts, where n is the length of the array.
Space Complexity: O(n) for storing the frequency counts.
Solution
The solution counts the frequency of each integer in the array, then sorts these frequencies in ascending order. By iterating over the sorted frequencies and subtracting from k, we remove integers with lower occurrences first until k becomes insufficient to remove an entire group. The remaining unique integers after these removals yield the least possible count.