Likeable arrays

3.2

16 votes
Algorithms, Basics of Greedy Algorithms, Greedy Algorithms
Problem

Bob and Alice are two friends, they have an array \(A\) consisting of \(N\) integers, \(A_1, A_2, A_3\ ...,\ A_N\). Alice likes the arrays in which if element \(X\) is present it must have exactly \(X\) or zero occurrences. So, Bob has decided to convert this array to an array which Alice likes. To do that, he can perform the following two operations:

  •         Add an element of any value to array \(A\).
  •         Remove an element from array A.

Find the minimum number of operations Bob has to perform so that array is liked by Alice.

Input format

  • The first line contains an integer \(T\) denoting the number of test cases.
  • The first line of each test case contains an integer \(N\) denoting the number of elements in array \(A\).
  • The second line of each test case contains \(N\) space-separated integers of array \(A\).

Output format

Print \(T\) lines. For each test case:

  • Print a single line indicating the minimum number of operations to be performed.

Constraints

\(1 \leq T \leq 20000\)

\(1 \leq N \leq 200000\)

\(1 \leq A_i \leq 10^9 \forall i \in [1,N]\)

The sum of \(N\) over all test cases does not exceed 200000.

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

According to the defination: 1 should have exactly one or zero occurence, 2 should have two or zero occurences and 3 should have three or zero occurences.

So for three, we have two options, either we remove both occurences or add one occurence, it will be optimal to add one occurence.

Editor Image

?