You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain.
Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4.
Sample Input 1
4
3 0
1 0 2
3 1
1 0 2
4 3
2 5 4 9
2 0
3 4
Sample Output 1
3
4
6
0
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t–)
{
int n,k;
cin>>n>>k;
int a[1000000],b[n];
for(int i=0;i<1000000;i++)
a[i]=0;
for(int i=0;i<n;i++)
cin>>b[i];
for(int i=0;i<n;i++)
{
a[b[i]]++;
}
for(int i=0;i<1000000;i++)
{
if(k==0 && a[i]==0)
{
cout<<i<<endl;
break;
}
else if(a[i]==0)
k–;
}
}
return 0;
}