You have been geven a number of product is to collect from an Amazon fulfillment center.
guys help me fast in the middle of a interview need to solve this quickly
check the picture attached
Answers
Answer:
first collect the once which are small than collect the big once fast with the number given and here the riddle is solved but fast before the day dies
Answer:
```
bool cmp(pair<int, int> &a, pair<int, int> &b) {
return a.second < b.second;
}
int removeProducts(vector<int> ids, int rem) {
map<int, int> mp;
for (auto i : ids)
mp[i]++;
vector<pair<int, int>> temp;
for (auto i : mp) {
temp.push_back(make_pair(i.first, i.second));
}
sort(temp.begin(), temp.end(), cmp);
int ind = 0;
while (rem > 0) {
if (temp[ind].second <= rem) {
temp[ind].second = 0;
rem -= temp[ind].second;
} else {
temp[ind].second -= rem;
rem = 0;
}
ind++;
}
int ans = 0;
for (int i = 0; i < temp.size(); i++)
if (temp[i].second != 0)
ans += 1;
return ans;
}
```
Explanation:
Remove the lowest frequency elements first.