# Help me in solving MEX problem

### My issue

include
#include<bits/stdc++.h>
using namespace std;

int main() {
int t;cin>>t;
while(t–){
int n,k;cin>>n>>k;
vectorv(n);
for(int i=0;i<n;i++){
cin>>v[i];
}
sort(v.begin(),v.end());
int ans;
if(v[0]<=k){k=k-v[0];}
else {ans=k;k=-1;}
for(int i=1;i<n && k>=0;i++){
if(v[i]-v[i-1]-1<=k){k-=(v[i]-v[i-1]-1);}
else{ans=v[i-1]+1+k;k=-1;}
}
if(k>=0){ans=v[v.size()-1]+k+1;}
cout<<ans<<endl;
}
return 0;
}
Can someone help me find what am I missing? It’s partially correct. I am missing a corner case;

### My code

``````#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
int t;cin>>t;
while(t--){
int n,k;cin>>n>>k;
vector<int>v(n);
for(int i=0;i<n;i++){
cin>>v[i];
}
sort(v.begin(),v.end());
int ans;
if(v[0]<=k){k=k-v[0];}
else {ans=k;k=-1;}
for(int i=1;i<n && k>=0;i++){
if(v[i]-v[i-1]-1<=k){k-=(v[i]-v[i-1]-1);}
else{ans=v[i-1]+1+k;k=-1;}
}
if(k>=0){ans=v[v.size()-1]+k+1;}
cout<<ans<<endl;
}
return 0;
}

``````

Learning course: Jump from 2* to 3*
Problem Link: CodeChef: Practical coding for everyone

@gowtham78444
This is my code
plzz refer it i have done in a much simpler way.

``````#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
int n,k;
cin>>n>>k;
int a[n];
unordered_set<int> s;
for(int i=0;i<n;i++)
{
cin>>a[i];
s.insert(a[i]);
}
for(int i=0; ;i++)
{
if(!s.count(i))
{
if(k==0)
{
cout<<i;
cout<<endl;
break;
}
else
k--;
}
}
}
return 0;
}
``````
1 Like

Thank you . That was dope

1 Like