Given an array of integers arr and an integer k. Find the least number of unique integers after removing exactly k elements.
Example 1:
Input: arr = [5,5,4], k = 1 Output: 1 Explanation: Remove the single 4, only 5 is left.Example 2:
Input: arr = [4,3,1,1,3,3,2], k = 3 Output: 2 Explanation: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.
Constraints:
1 <= arr.length <= 10^51 <= arr[i] <= 10^90 <= k <= arr.lengthThe key idea in #1481 Least Number of Unique Integers after K Removals is to remove elements in a way that minimizes the number of distinct values left in the array. Instead of removing elements arbitrarily, first compute the frequency of each integer using a hash map. Each frequency tells us how many deletions are required to eliminate that integer completely.
Once frequencies are known, apply a greedy strategy: remove integers with the smallest frequency first. This can be done by sorting the frequency list or by using a min-heap. Repeatedly subtract the smallest frequency from k (the allowed removals). Every time you fully remove a number's occurrences, the total count of unique integers decreases.
Stop when k is no longer sufficient to remove the next frequency. The remaining frequencies correspond to the number of unique integers left in the array. This strategy works because removing smaller groups first maximizes the reduction in unique values. The dominant cost comes from sorting or heap operations.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Hash Map + Sorting Frequencies (Greedy) | O(n log n) | O(n) |
| Hash Map + Min Heap | O(n log n) | O(n) |
NeetCodeIO
Use these hints if you're stuck. Try solving on your own first.
Use a map to count the frequencies of the numbers in the array.
An optimal strategy is to remove the numbers with the smallest count first.
This approach involves two main steps. First, count the frequency of each integer in the array. Second, use a min-heap (or priority queue) to remove the integers with the smallest frequency first. This approach ensures that we are removing the least frequent integers to reach the least number of unique integers efficiently.
Time Complexity: O(n log n) due to sorting the frequency array. Space Complexity: O(n) for storing the frequency counts.
1#include <stdio.h>
2#include <stdlib.h>
3
4int compare(const void *a, const void *b) {
This C program implements the approach by utilizing an array to keep the frequency of each integer. It then collects these frequencies into another array which is sorted to allow removal of the lowest frequencies first. A loop continues removing these and reducing the count of unique integers. A direct index is maintained to access frequencies in order.
This approach takes a greedy strategy to count frequencies of integers and sorts them. By progressively removing elements with smallest frequencies, it directly calculates the least number of unique integers remaining after exactly k removals.
Time Complexity: O(n log n) due to the qsort on frequencies. Space Complexity: O(n) for arrays used in frequency counting.
1#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
int leastNumberOfUniqueInts(vector<int>& arr, int k) {
unordered_map<int, int> count;
for (int num : arr) {
count[num]++;
}
vector<int> frequencies;
for (auto& kvp : count) {
frequencies.push_back(kvp.second);
}
sort(frequencies.begin(), frequencies.end());
int unique = frequencies.size();
for (int freq : frequencies) {
if (k >= freq) {
k -= freq;
unique--;
} else {
break;
}
}
return unique;
}
int main() {
vector<int> arr = {5, 5, 4};
int k = 1;
cout << leastNumberOfUniqueInts(arr, k) << endl;
return 0;
}Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Removing elements with the smallest frequency costs fewer deletions while eliminating an entire unique value. This maximizes the reduction in unique integers for the limited number of removals allowed by k, which is why the greedy strategy works.
Yes, problems involving frequency counting and greedy removal strategies are common in technical interviews. Variants of this problem appear in companies like Amazon and Google to test understanding of hash maps, sorting, and greedy decision making.
A hash map is used to count occurrences of each integer efficiently. After counting, a sorted list or a min-heap is useful for processing frequencies from smallest to largest. This combination enables an efficient greedy strategy.
The optimal approach counts the frequency of each integer using a hash map and then removes numbers with the smallest frequencies first. By sorting the frequency list or using a min-heap, you greedily eliminate entire groups while k removals are available. This minimizes the number of unique integers remaining.
The C++ solution uses an unordered_map to count integer frequencies. These frequencies are sorted, allowing for the removal starting with the smallest frequencies to minimize unique integers post deletion.