The chef has received the order to make ‘N’ dishes represented by an array ‘Arr’, where ‘Arr[i]’ denotes the type of dish.
Chef has the liberty to cancel at most ‘M’ orders. He cancels the order in such a way that the different types of dishes he has to cook are minimized. The chef is lazy and dumb so he has asked you to help him.
Find the count of the minimum variety of dishes that the chef will have to cook.
For Example :If N = 6, Arr = { 1, 2, 3, 1, 1, 2 } and M = 2
Then the chef will have to cook at least 2 different varieties of dishes, because:
He may cancel the orders for both the dishes of type 2, now he left with {1, 1, 1, 3}
He may cancel the orders for dish type 3 and one of the orders of dish type 1, now he left with {2, 1, 1, 2}
There are many other ways to cancel the orders, but none of them will result in an answer less than 2, hence we will return the value 2.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains two integers ‘N’ and ‘M’, denoting the number of orders and the maximum number of orders the chef can cancel respectively.
The next line contains N integers, ‘Arr[i]’ denoting the type of the i<sup>th</sup> dish.
Output Format :
For each test case, print a single integer denoting the minimum types of dishes that the chef will have to cook.
Output for each test case will be printed in a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
0 <= M < N <= 500
0 <= Arr[i] <= 1’000’000’000
Time limit: 1 sec
2
6 2
1 2 3 1 1 2
4 2
1 1 1 1
2
1
For test case 1 :
We will return 2, because:
The chef will cancel the orders for both the dishes of type 2, now he left with {1, 1, 1, 3}
For test case 2 :
We will return 1, because:
Regardless of whether the chef cancels the order or not, we will have to cook at least 1 dish in this case.
2
5 4
1 2 3 4 5
5 3
1 2 3 4 5
1
2
The Chef will greedily try to cancel the orders which have the dishes with the least frequency.
Create a hash-map to store how many times a particular dish is ordered. Now Iterate this hash-map and each time remove the dish with the least frequency if the current cancellations allowed are greater than or equal to the least frequency, also deduct the least frequency from the current cancellations allowed. Keep repeating this till the point when the least frequency is less than or equal to current cancellations allowed.
Finally return the size of the hash-map, as this will be the minimum variety of dishes the Chef will have to cook.
The steps are as follows :
O( N ^ 2 ), where N denotes the number of orders
In the worst case when all the ‘N’ orders are for different dishes and the chef can cancel ‘N-1’ dishes, then we will be removing one dish at a time after traversing all the remaining dishes.
Hence the time complexity is O( N^2 ).
O( N ), where N denotes the number of orders
In the worst case when all the ‘N’ orders are for different dishes we will build a hash-map of size ~N.
Hence the space complexity is O( N )