Just simply check if all numbers less than m exist in the array. If not then ans is -1. Else print the size of array-number of occurrences of m.

1 2 4 6

for m = 3 ans is 4 since there are 0 occurrences.

1 2 3 4 6

now ans = 5-1=4 since there is 1 3

1 3 4 6 or 1 4 6

now ans is -1 since there is no 2 in the array

You have not considered the case of duplicate elements.

can you give any test case

Take a look at my solution:

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

Since the array can have duplicates, I’ve made a new array of unique elements while counting the occurrence of each element. I have added a 0 to my array so that I can check if 1 exists without typing in specific cases.

Next, I’ve sorted the array and if all the elements before m exist, I have added the count of each number to the answer. The elements after m don’t affect the MEX so then you can just add the count of each number > m that is present in the array.