#K65452. Minimum Changes to Achieve Distinct Elements
Minimum Changes to Achieve Distinct Elements
Minimum Changes to Achieve Distinct Elements
You are given a list of n integers, where each integer is in the range \(1 \leq x \leq k\). In one change, you can modify any element to any integer within the range \([1,k]\). Your task is to determine the minimum number of changes required to make all the elements in the list distinct.
In other words, if the given list is \(a_1, a_2, \ldots, a_n\), determine the minimum number of modifications needed such that after the changes, every integer in the list is unique. Note that a valid modification can change a duplicate element to a value that is not already present in the list.
A key observation is that the answer is given by \[ n - |\{a_1, a_2, \ldots, a_n\}|, \] which means the total number of elements minus the number of distinct elements.
inputFormat
The input is read from stdin
and consists of two lines:
- The first line contains two integers, n and k, where n is the number of elements in the list and k is the maximum possible value an element can take.
- The second line contains n space-separated integers representing the elements of the list.
outputFormat
Print a single integer to stdout
representing the minimum number of changes required to make all elements in the list distinct.
6 10
1 2 2 3 4 4
2