Problem title
Difficulty
Avg time to solve

Maze with N doors and 1 Key
Moderate
15 mins
Dice Throws
Hard
35 mins
Mean Median Mode
Easy
10 mins
Sum Tree
Moderate
29 mins
Leaves One Level Apart
Moderate
40 mins
Divide Linked List In Two
Easy
20 mins
Football Game
Easy
20 mins
Distinct Left
Moderate
20 mins
Longest Subarray Zero Sum
Moderate
18 mins
Gas Tank
Moderate
20 mins
5

Distinct Left

Difficulty: MEDIUM
Contributed By
Avg. time to solve
20 min
Success Rate
70%

Problem Statement

You have been given an array/list of integers 'ARR' of size 'N'. You are also provided an integer 'K'.

Your task is to find the number of distinct elements left in the array, after deleting 'K' maximum elements from the array.

For example:
For the given array arr = [5, 3, 2, 3, 4, 3] and K = 3.
Output = 2

We have to delete three maximum elements, i.e. 5, 4, and 3. So, the array after the deletions contains 2, 3, and 3. Now, we only have two distinct elements left in the array (2 and 3).
Input Format:
The first line of input contains an integer 'T' representing the number of test cases or queries to be processed. Then the test case follows.

The first line of each test case contains two single space-separated integers 'N' and 'K' representing the size of the array/list and the given integer respectively.

The second line of each test case contains 'N' single space-separated integers representing the array/list elements.
Output Format:
For each test case, print a single integer, i.e. the number of distinct elements left in the array/list.

Print output of each test case in a separate line.

Note:

You are not required to print the output, it has already been taken care of. Just implement the function. 
Constraints:
1 <= T <= 10
1 <= N <= 10^4
1 <= K <= N
1 <= ARR[i] <= 10^9

Time Limit: 1 sec
Sample Input 1:
2
5 3
1 3 2 2 4
4 2
10 4 8 5
Sample Output 1:
2
2
Explanation for Sample 1:
For the first test case we have 4, 3 and 2 as three maximum elements. After deleting these three elements, we are left with array [1, 2]. Hence, we are left with two distinct elements in the array.

For the second test case we have 10 and 8 as two maximum elements. After deleting these three elements, we are left with array [4, 5]. Hence, we are left with two distinct elements in the array.
Sample Input 2:
1
8 5
4 7 4 6 7 1 2 4 
Sample Output 2:
3
Reset Code
Full screen
copy-code
Console