0% found this document useful (0 votes)
15 views2 pages

Prob-Maximize Diversity of An Array

Uploaded by

aashnangoyal12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Prob-Maximize Diversity of An Array

Uploaded by

aashnangoyal12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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.

ˆ Each test case consists of:

– 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.

You might also like