#K35852. Minimum Operations for Equal Subsequence
Minimum Operations for Equal Subsequence
Minimum Operations for Equal Subsequence
You are given an array A of N integers and an integer K. In one operation, you can change any element of the array to any other integer. Your goal is to achieve a state where there exists at least one subsequence of length K in which all elements are equal.
Let \( f \) be the maximum frequency of any integer currently present in the array. Then, the minimum number of operations needed is given by:
\( \max(0, K - f) \)
For example, if A = [1, 2, 2, 2, 3] and K = 3, since the most frequent element (2) appears 3 times, no operation is needed.
inputFormat
The first line contains an integer T representing the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers N and K, where N is the number of elements in the array and K is the required subsequence length. The second line contains N space-separated integers denoting the elements of the array.
outputFormat
For each test case, output a single integer — the minimum number of operations required to obtain a subsequence of length K with all equal elements. Each output should be on a new line.
## sample3
5 3
1 2 2 2 3
7 5
1 2 3 4 5 6 7
4 2
4 4 4 4
0
4
0
</p>