Find K Distinct Element FrequenciesMedium

Find K Distinct Element Frequencies

MediumPractice library · +20 XPheapsfrequency countingtop-k

Given a list of N integers, find the K-th largest frequency of elements in the list. If there are fewer than K distinct frequencies, output 0. The elements themselves can be positive, negative, or zero. The frequencies are the counts of how many times each distinct integer appears in the list.

Input

The first line contains two integers N and K. The second line contains N integers, the elements of the list.

Output

Print a single integer: the K-th largest frequency. If there are fewer than K distinct frequencies, print 0.

Constraints

1 <= N <= 200000, 1 <= K <= N. Each element value is between -10^9 and 10^9.

Time limit 2 s (native code; slower languages get more)Memory 256 MB14 hidden tests

Example 1

Input
10 3
1 1 2 2 2 3 3 4 5 5
Output
2

Frequencies are: 1 (for 4, 5), 2 (for 3), 3 (for 2), 2 (for 1). Distinct frequencies are 1, 2, 3. Sorted descending: [3, 2, 1]. The 3rd largest frequency is 1.

Example 2

Input
5 2
10 20 10 30 10
Output
1

Frequencies are: 3 (for 10), 1 (for 20), 1 (for 30). Distinct frequencies are 3, 1. Sorted descending: [3, 1]. The 2nd largest frequency is 1.

Hints