Max Mex Help

I am solving MEX as a practice problem but I am not able to pass the second subtask’s second condition, it keeps giving me WA… Please help me with the test case. My code:

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        int a[n];
        int c[200000]={0};
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            c[a[i]]++;
        }
        int ans = 0;
        for(int i=0;i<200000;i++)
        {
            if(c[i] == 0 && k >= 0)
            {
                ans = i;
                k--;
            }
        }

        cout<<ans<<endl;
    }
    return 0;
}

Is this the code for MEX ? Why are you printing Yes and No?

My apologies… I was solving RAINBOWA side by side and pasted that code… I’v updated the code… Please help!

Atmost answer can be 200000, but you are checking only up to (200000-1). In the last sub-task answer for few cases is 200000.

2 Likes

Yes he is right test cases can ans 200000 or greater but your code will not do that.