Takahashi has ( N ) balls, each labeled with an integer ( A_i ). He wants to rewrite the integers on some of these balls such that there are at most ( K ) different integers among the ( N ) balls.
Your task is to determine the minimum number of balls that need to be rewritten to achieve this goal.
Input Format
The first line contains two integers ( N ) and ( K ) (1 ≤ ( N ), ( K ) ≤ 100,000).
The second line contains ( N ) integers ( A_1, A_2, ..., A_N ) (1 ≤ ( A_i ) ≤ 10^9), representing the integers on the balls.
Output Format
Output a single integer: the minimum number of balls that need to be rewritten.
Examples
Example 1:
Input:
text
5 2
1 1 2 2 3
Output:
text
1
Explanation:
By rewriting one of the balls labeled '3' to either '1' or '2', we can achieve at most 2 different integers.
Example 2:
Input:
text
6 3
1 2 3 4 5 6
Output:
text
3
Explanation:
To have exactly 3 different integers, we can change any 3 of the balls with integers 4, 5, and 6 to either 1, 2, or 3.
Constraints
The input values for ( N ) and ( K ) should be within the prescribed limits to ensure efficiency in your solution.
The integers on the balls ( A_i ) should be considered while calculating the minimum rewrites needed.
Solution Submission
Please place your solution in the /app/ directory and ensure that it is well-documented for readability and maintainability.
Important: place your implementation at /app/solution.py so the verifier can run it.