Someone please help me in MAXMEX

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll t;
cin>>t;
while(t–)
{
ll n,m,sum=0;
cin>>n>> m;
ll a[n];
for(ll i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
ll mex=0,place;
for(ll i=0;i<n;i++)
{
if((a[i+1]-a[i])!=1)
{
mex=a[i]+1;
place=i;
}
}
if(mex==m)
{
cout<<n<<endl;
}
else if(mex<m)
{
cout<<-1<<endl;
}
else if(mex>m)
{
for(ll i=0;i<n;i++)
{
if(a[i]==m)
{
sum++;
}
}
cout<<n-sum<<endl;
}

}

}

  1. Please format your code.
  2. What error did you get? WA, TLE, or RE?
1 Like

You are calculating mex incorrectly. For [1,2,4] your code is printing mex as 5, whereas correct mex is 3.

Calculate Mex Code
int mex = 1;
for (int  i=0; i<n; ++) {
		if (a[i] == mex) {
			mex++;
		}
	}
1 Like

i am getting wromg answer

thankx dear for helping me it really works

1 Like

i had rectufied myself dear
i calculate mex wrongly…so i rectify it…

Try
1
3 3
2 2 2
One of the problem is that you are not checking starting value. You may see this CodeChef: Practical coding for everyone

yeah i figure out it.