MAXMEX - Editorial

while(!s.empty() && s.top()==--m){
        
    s.pop();
    count++;
}

here if i am not wrong, then you have assumed that element less than m occur only once
but according to this misleading problem ,
element less than m can occur any no of times in the given sequence

try this instead
while(!s.empty() && s.top()<m){

    s.pop();
    count++;
}

still the code need correction and i.e.
pls ensure that all element less than m occurs at least 1 time in the given sequence else m will not be the MEX. i used arrays to do this
for(int i=1;i<=m-1;i++)
{
if(binary_search(arr,arr+n,i))
{
flag++;
}
}
if(flag!=m-1)
{
cout<<"-1"<<endl;
}

   # cook your dish here

a=int(input())
for i in range(a):
x=list(map(int, input().split()))
f=list(map(int, input().split()))
f.sort()
MEX=0
for i in list(range(1, max(f)+1)):
if i not in f:
MEX+=i
break
if MEX==x[1]:
print(len(f))
elif MEX<x[1]:
print(-1)
else:
for i in range(len(f)):
if f[i]==x[1]:
f.pop(i)
print(len(f))

    Why am I getting a runtime error here? Please tell me the changes.

See this,
if A[]={1, 2, 4, 3, 6, 5}
M=6
then what will a correct code return ?
-1 : logically because there is no MEX in the given sequence, therefore X not exists
so we cannot compare X,N

problem does not account for this case ,
what should our code return in this particular e.g. according to editorial explanation

According to the statement: " the smallest positive integer which does not occur among the chosen element. "
can someone please tell what is the MEX for the array: [1,2,3,4,5,6]
-> If the MEX is ‘7’, then what is the significance of the statement “among the chosen elements”. It should have been something like “among the +ve integers”.
-> If the MEX is “NONE”, by the exact meaning of the statement “among the chosen elements”, then the whole editorial is contradictory.

Editorial : MEX of a sequence is the smallest positive integer not present in the sequence.
Problem:MEX, i.e. the smallest positive integer which does not occur among the chosen elements.

These two statements are same for e.g. if you you choose set of no [45,46,47] only then also the mex will be 1 (one may or may not be present in the given sequence).
So, in the end you need all such integers such that all values less than m should be present and m should not be present in the given sequence (selected subset). :slightly_smiling_face:

So, we have to start every sequence by assuming it starts with 1.
like in [4,7,8,9,10] MEX = 1 and if m=6 or 5 then answer will be -1.

I can see only one problem in the solution… Since repeated elements are allowed, it’s not guaranteed that the sum of elements smaller than m present in the array is m * (m - 1)/2. We have to check it using either a set or hashtable.

Can someone help me figure out the test cases??
3
4 3
1 2 4 5
6 3
1 2 4 6 7 8
6 3
1 4 5 6 7 8
According to me output should should be : 4 6 1
Am I right or wrong?

In the test case provided by you - MEX(i.e X) exists and is equal to 7 and the code will return or print 5.
You can check my solution here
I hope it helps you :slightly_smiling_face:

1 Like

Last one should be -1 since 2 is absent.

1 Like

Shittiest problem statement ever, should’ve better explained maybe

3 Likes

Atleast 2 sample test cases must have been there. Even the confusing problem statement can be ignored if we look at multiple sample test cases. With only 1 sample TCs, many got WA!

1 Like

in that case mex will be 1

  1. It is not necessary that input starts from 1, if m>1 and 1 is not present then the answer will be -1 always as 1 is the smallest positive integer.(every positive integer up to m-1 should be present otherwise answer will be -1)
  2. Order of the input don’t matter i guess, since only the presence of element matters.

for this array A mex will be 7 and largest posible sequence with mex=6 is {1,2,3,4,5}

No where it was told that duplicate element can be present, later on it was mentioned in comments.
Also please take good examples in explanations.
Codechef needs learn alot from codeforces.

1 Like

can anyone tell me code to find mex here in c language

can you please check why i am getting wrong ans?

code

Why are you doing binary search? See this CodeChef: Practical coding for everyone

YES but your code prints 3.