- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a list of numbers called items and another value n. A salesman has items in a bag with random IDs. The salesman can delete as many as n items from the bag. We have to find the minimum number of different IDs in the bag after n removals.

So, if the input is like items = [2, 2, 6, 6] n = 2, then the output will be 1 as we he can sell two items with ID 2 or ID 6, then only items with single target will be there.

To solve this, we will follow these steps:

- c := frequency of each element present in items
- ans := size of c
- freq := sort the list of all frequencies in c
- i := 0
- while i < size of freq, do
- if freq[i] <= n, then
- n := n - freq[i]
- ans := ans - 1

- otherwise,
- return ans

- i := i + 1

- if freq[i] <= n, then
- return 0

Let us see the following implementation to get better understanding:

from collections import Counter class Solution: def solve(self, items, n): c = Counter(items) ans = len(c) freq = sorted(c.values()) i = 0 while i < len(freq): if freq[i] <= n: n -= freq[i] ans -= 1 else: return ans i += 1 return 0 ob = Solution() items = [2, 2, 6, 6] n = 2 print(ob.solve(items, n))

[2, 2, 6, 6], 2

1

- Related Questions & Answers
- Program to find largest product of three unique items in Python
- Python program to find the sum of all items in a dictionary
- Program to find remainder after dividing n number of 1s by m in Python
- Python Program to remove items from set
- Program to find maximum number of K-sized groups with distinct type items are possible in Python
- Program to find maximum profit after cutting rods and selling same length rods in Python
- Program to find maximize score after n operations in Python
- Python Program to remove items from the set
- Split Array of items into N Arrays in JavaScript
- Program to find maximum profit after buying and selling stocks at most two times in python
- How to Find The Largest Or Smallest Items in Python?
- Minimum number of items to be delivered using C++.
- MongoDB query to get a specific number of items
- Program to count items matching a rule using Python
- Python Program to Multiply All the Items in a Dictionary

Advertisements