[Solution] Convert to permutation CodeChef Solution 2022
Problem
You are given an array of size . In one operation, you can:
- Choose an index and increase by .
Find the minimum number of operations required to convert the array into a permutation of size . If it is impossible to do so, print .
Note that a permutation of size contains each element from to exactly once.
Input Format
- The first line of input will contain a single integer , denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains the integer — the size of the array.
- The next line contains space-separated integers, the elements of the array .
Output Format
For each test case, output on a new line, the minimum number of operations required to convert the array into a permutation of size .
If it is impossible to do so, print .
Explanation:
Test case : We can convert the array into a permutation using operations:
- Operation : Choose and increase by . Thus, the array becomes .
- Operation : Choose and increase by . Thus, the array becomes .
- Operation : Choose and increase by . Thus, the array becomes .
It can be shown that this is the minimum number of operations required to convert into a permutation.
Test case : The given array cannot be converted into a permutation using any number of operations.
Test case : The given array is already a permutation. Thus, we require operations.
Test case : We can convert the array into a permutation using operations:
- Operation : Choose and increase by . Thus, the array becomes .
- Operation : Choose and increase by . Thus, the array becomes .
- Operation : Choose and increase by . Thus, the array becomes .
It can be shown that this is the minimum number of operations required to convert into a permutation.
No comments:
Post a Comment