Likeable arrays
Practice
3.2 (16 votes)
Algorithms
Basics of greedy algorithms
Greedy algorithms
Problem
91% Success 3689 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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.

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
22 votes
Tags:
ApprovedData StructuresEasyGreedy AlgorithmsOpen
Points:20
6 votes
Tags:
AlgorithmsEasyGreedy Algorithms
Points:20
4 votes
Tags:
AlgorithmsEasyGreedy Algorithms