MAXMEX - Editorial

hey if the input is 4 5 6 8 9 and M=7 then what should be output

What if the input is 4 7 8 and M=5 what should be the output

Isn’t the correct answer 10? You can choose all the numbers in the sequence and the smallest number which does not exist in this sequence is 4. Hence the answer is 10?

I found the statement confusing, quite honestly. Please try to give at least 2 test cases with proper explanation, I ended up confused about what MEX actually is, “chosen” is the word that killed my rating.

1 Like

This is the confusing part, MEX is the smallest positive integer which doesn’t occur in the set, hence, the answer will be -1. Because MEX of 4 7 8 is 1.
To clarify, look at the code:
int mex=1;
while(s.count(mex)){
mex++;
}
The answer is mex when the while loop breaks. So you take a set and start checking if 1 occurs, then you proceed to 2,3,4 and so on. until you find the one element that doesn’t occur, this shall be considered as MEX of the given Set.
The statement was confusing and a test case explaining the whole scenario would’ve gone a long way for us.

-1

You understood the problem wrong.

7 is also not present in the sequence.

1 Like

My day is ruined and my disappointment is immeasurable.

2 Likes

ok now I got it. Thanks man

Such poorly explained problem. They should have clarified that we need to choose subsequence of array rather than subarray , it was just written choose largest no. of elements (In what) . A problem should be easily understandable . But everything has a learning next time whenever there is written choose largest number , I will assume it to be subsequence .@rezwanarefin01 @raja1999

2 Likes

Right…this also happened to me.

Question was poorly defined.
Question says
Chef has a sequence of positive integers A1,A2,…,ANA1,A2,…,AN. He wants to choose some elements of this sequence (possibly none or all of them) and compute their

MEX, i.e. the smallest positive integer which does not occur among the chosen elements

If we go by this logic than for input
1
9 8
1 2 3 5 6 7 9 10 11
The output should be [5,6,7,9,10,11] i.e 6. But according to the solution provided it is different.
Poorly defined question

Yeah I got it. Thanks man

@taran_1407
There should be atleast 2-3 examples with proper explanations for each example. Also, the question description was really bad.
Checkout Leetcode or Hackerrank. They give plenty of examples with proper explanations to each one of the example.
Please take this as a constructive criticism. Thanks.

2 Likes

I agree with you totally Brother.It Took me 1 Hour to interpret what the question is asking to do.Problem Statement was a bit confusing.

I also asked in comments of 2nd problem but no one replied me.

https://www.codechef.com/viewsolution/34624153

Thanks @adarsh08x for the reply.
E.g.
1
6 6
4 5 7 8 9 10
According to your post following test case should return the total length of the array.
I have looked at the editorial code which is written in Java. But the output is returned as -1.

yes it will return -1 because 1,2,3, are not present in the in the array
just add 1,2,3 then it will return the whole length
my solution

/* package codechef; // don’t place package name! */

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i=i+1)
{
int n=sc.nextInt();
int m=sc.nextInt();
int array[]=new int[n];
for(int j=0;j<n;j=j+1)
{
array[j]=sc.nextInt();
}
Arrays.sort(array);

	    //Find Mex
	    int x=1;
	    for(int j=0;j<n;j=j+1)
	    {
	        if(x<array[j])
	        {
	            break;
	        }
	        x=array[j]+1;
	    }
	    //System.out.println(x);
	    if(x==m)
	    {
	        System.out.println(n);
	    }
	    else if(x<m)
	    {
	        System.out.println("−1");
	    }
	    else if(x>m)
	    {
	        int count=0;
	        for(int j=0;j<n;j=j+1)
	        {
	            if(array[j]==m)
	                count=count+1;
	        }
	        System.out.println(n-count);
	    }
	}
}

}

What is wrong in this code? It is giving WA.