Maximum distinct elements after removing K elements
Easy Accuracy: 42.1% Submissions: 1347 Points: 2

Given an array containing N elements. The task is to find the maximum number of distinct elements after removing K elements from the array.

Example 1:

Input : Arr[] = {5, 7, 5, 5, 1, 2, 2}, K = 3
Output : 4
Remove 2 occurrences of element 5 and 
1 occurrence of element 2.

Example 2:

Input : Arr[] = {1, 2, 3, 4, 5, 6, 7}, K = 5
Output : 2

Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function maxDistinctNum() that takes an array (arr), sizeOfArray (n), integer value K, and return the maximum distinct elements after removing K elements. The driver code takes care of the printing.

Expected Time Complexity: O(K*logD)  where D is the number of distinct elements in the given array.
Expected Auxiliary Space: O(N).


1 ≤ K ≤ N ≤ 106
1 ≤ A[i] ≤ 105


We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Maximum distinct elements after removing K elements

Output Window