Prob-Maximize Diversity of An Array
Prob-Maximize Diversity of An Array
Problem Statement
You are given an array A. The diversity of the array A is defined as the number of pairs
i, j (i < j) such that Ai ̸= Aj .
You want to maximize the diversity of the array. For that, you are allowed to make
at most K operations on it. In each operation, you can select a particular element and
change its value to any integer in the range 1 to 109 , both inclusive.
Find out the maximum diversity of the array that you can obtain.
Input Format
The first line contains T , the number of test cases.
– The first line contains two integers N, K, where N is the length of the array
A, and K is the maximum number of operations allowed.
– The second line contains N space-separated integers representing the elements
of the array A.
Output Format
For each test case, output a single line containing an integer corresponding to the maxi-
mum possible diversity of the array.
Constraints
1 ≤ T ≤ 20
0 ≤ K ≤ 109
2 ≤ N ≤ 105
1 ≤ Ai ≤ 109
1
Example Input
3
3 10
1 2 3
4 2
1 1 2 2
6 2
2 3 3 2 4 4
Example Output
3
6
14
Explanation
Testcase 1: The array is already diverse, and no operations are required. Hence, the
answer is 3.
Testcase 2: By performing 2 operations, you can make all the elements distinct (e.g.,
change 1 to 3 and 2 to 4). Hence, the maximum diversity is 6.
Testcase 3: With 2 operations, you can maximize the distinctness of the array to
achieve a diversity of 14.