#K95167. Unique Set Transformation
Unique Set Transformation
Unique Set Transformation
You are given an array of n integers where each element is in the range ( [1, n] ). Your task is to perform the minimum number of operations to transform the array into a permutation of the first n positive integers. In one operation, you can either insert a missing integer into the array or remove a duplicate occurrence, so that every integer in the resulting array appears exactly once.
Formally, let ( A ) be the given array and let ( S = {1, 2, \ldots, n} ) be the target set. Define:
( \text{missing} = |S \setminus A|, \quad \text{duplicates} = |A| - |A \cap S| )
The minimum number of operations is given by:
( \text{operations} = \max(\text{missing},, \text{duplicates}) )
For example, if ( A = [4, 3, 2, 7, 8, 2, 3, 1] ), then ( S = [1,2,3,4,5,6,7,8] ) and the missing numbers are ( {5, 6} ), while there are 2 duplicate occurrences. Thus, the answer is 2.
inputFormat
The first line of input contains a single integer ( n ), representing the size of the array. The second line contains ( n ) space-separated integers, representing the elements of the array.
outputFormat
Output a single integer which represents the minimum number of operations required to transform the array into a permutation of ( [1, n] ).## sample
8
4 3 2 7 8 2 3 1
2