VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-number-of-distinct-elements-after-removing-m-items/

⇱ Minimum number of distinct elements after removing m items - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum number of distinct elements after removing m items

Last Updated : 26 Apr, 2024

Given an array of items, an i-th index element denotes the item id's, and given a number m, the task is to remove m elements such that there should be minimum distinct id's left. Print the number of distinct id's.

Examples: 

Input : arr[] = { 2, 2, 1, 3, 3, 3} 
m = 3
Output : 1
Remove 1 and both 2's.So, only 3 will be
left that's why distinct id is 1.
Input : arr[] = { 2, 4, 1, 5, 3, 5, 1, 3}
m = 2
Output : 3
Remove 2 and 4 completely. So, remaining ids
are 1, 3 and 5 i.e. 3

Asked in: Morgan Stanley

  1. Count the occurrence of elements and store them in the hash. 
  2. Sort the hash. 
  3. Start removing elements from the hash whose frequency count is less than m. 
  4. Return the number of values left in the hash.

Implementation:


Output
1

Time Complexity:O(n log n)

Space Complexity: O(n), As we are using map to store elements 

Approach 2: Using Priority Queue and Map

  1. Create a frequency map:
    An unordered map is created to store the frequency of each element in the given array. The key of the map is the element itself and the value is its frequency.
  2. Create a priority queue:
    A priority queue is created to store the frequency of the elements in ascending order. The priority queue is a min heap, which means that the smallest element will be at the top of the queue.
  3. Insert the frequency of each element into the priority queue:
    Iterate through the frequency map and insert the frequency of each element into the priority queue.
  4. Remove elements:
    Now, remove elements from the priority queue until k becomes less than or equal to zero. While removing an element from the queue, subtract its frequency from k. If k becomes negative, break the loop and return the size of the priority queue.
  5. Return the size of the priority queue:
    After removing k elements, return the size of the priority queue.

Output
1

Time Complexity: O(n log n)

Space Complexity: O(n)

This article is contributed by Sahil Chhabra.  

METHOD 3:Using counter method .

APPROACH:

This program finds the minimum number of distinct elements in a list after removing m items from it.

ALGORITHM:

1.Create a Counter object from the input list to get the frequency of each element.
2.Get the total number of distinct elements in the list by getting the length of the Counter object.
3.Iterate over the items of the Counter object and remove the elements that have a frequency less than or equal to m.
4.Return the updated count of distinct elements.


Output
1

Time complexity: O(n log n), where n is the length of the input list. This is because creating a Counter object has a time complexity of O(n log n) due to the underlying hash table operations.

Space complexity: O(n), where n is the length of the input list. This is because we need to store the frequency of each element in the Counter object.

METHOD 4:Using Radix sort & counting sort method

  1. Radix Sort: We starts with radix sort to sort the input vector arr.
  2. Frequency Calculation: After sorting, the code iterates through the sorted array to calculate the frequency of each unique integer.
  3. Occurrences of each element: The code then performs frequency analysis, counting the occurrences of each frequency in the freq vector.
  4. Iterative Removal: The code iterates through the frequencies from minf to maxf (the minimum and maximum frequencies) and calculates how many unique integers need to be removed to achieve the desired grouping. It adjusts the size of the vector freq accordingly.
  5. Returning the Result: Finally, the function returns the updated size of the freq vector, representing the least number of unique integers needed to satisfy the conditions.

Output
1

Time Complexity: O(n )

Space Complexity: O(n)

Comment