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
Explanation:
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



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).

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

We are replacing the old Disqus forum with the new Discussions section given below.