Mode of an array

4

8 votes
Data Structures, Priority queue, Trees
Problem

You are given an array of N numbers A1,A2,,AN. In one operation, you can pick any index i and change Ai to x(1x109). Given an integer K, find the minimum number of operations required to make K the mode of the array.

Note: A number is called the mode of the array if it is more frequent than any other number in the array.

Example: The mode of array [1,1,3] is 1. The array [1,1,3,3] does not have any mode.

Input format

  • The first line contains the number of test cases T(1T1000).
  • The first line of each test case contains two integers, N and K(1K109) where N denotes the number of elements in the array.
  • The second line contains N integers A1,A2,,AN(1Ai109) denoting the contents of the array.

Note: Sum of N over all test cases does not exceed 2×105.

Output format

For each test case output a line containing the minimum number of operations required to make K the mode of array A.

 Constraints

1T1000

1N2×105

1K109

1Ai109

Sum of N over all test cases is less than or equal to 2×105

Sample Input
2
3 1
1 1 3
4 1
1 1 3 3
Sample Output
0
1
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the first case, we can see that 1 is already the mode of the array.

For the second case, we can choose index 3 and do the operation a3=1. The array will be [1,1,1,3]. Now, 1 is the mode of the array.

Editor Image

?