Mode of an array
Practice
3.7 (9 votes)
Data structures
Priority queue
Trees
Problem
91% Success 4145 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given an array of \(N\) numbers \(A_1, A_2,\ldots, A_N\). In one operation, you can pick any index \(i\) and change \(A_i \) to \(x (1 \leq x \leq 10^9)\). 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 (1 \leq T \leq 1000)\).
  • The first line of each test case contains two integers, \(N\) and \(K (1 \leq K \leq 10^9)\) where N denotes the number of elements in the array.
  • The second line contains \(N\) integers \(A_1,A_2, \ldots, A_N (1 \leq A_i \leq 10^9)\) denoting the contents of the array.

Note: Sum of \(N\) over all test cases does not exceed \(2 \times 10^5\).

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

\(1 \leq T \leq 1000\)

\(1 \leq N \leq 2 \times 10^5\)

\(1 \leq K \leq 10^9\)

\(1 \leq A_i \leq 10^9 \)

Sum of \(N\) over all test cases is less than or equal to \(2 \times 10^5\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
20 votes
Tags:
Ad-HocData StructuresEasyHeapsTrees
Points:20
25 votes
Tags:
ApprovedData StructuresEasyHeapsImplementationOpenPriority queueSortingTrees
Points:20
95 votes
Tags:
ApprovedEasyHeapsOpenPriority Queue