VOOZH about

URL: https://www.geeksforgeeks.org/dsa/mex-minimum-excluded-in-competitive-programming/

⇱ MEX (Minimum Excluded) in Competitive Programming - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

MEX (Minimum Excluded) in Competitive Programming

Last Updated : 23 Jul, 2025

MEX of a sequence or an array is the smallest non-negative integer that is not present in the sequence.

Note: The MEX of an array of size N cannot be greater than N since the MEX of an array is the smallest non-negative integer not present in the array and array having size N can only cover integers from 0 to N-1. Therefore, the MEX of an array with N elements cannot be greater than N itself.

Examples

Input: S[] = {1, 2, 0, 4, 5}
Output: 3
Explanation: The given sequence contains 0, 1, 2, 4, and 5. The smallest non-negative integer missing from the sequence is 3, as all numbers before it are present.

Input: S[] = {2, 0, 3, 10}
Output: 1
Explanation: The given sequence contains 0, 2, 3, and 10. The smallest non-negative integer missing from the sequence is 1, as 0 is present but 1 is not.

Input: S[] = {1, 2, 3, 4, 5}
Output: 0
Explanation: The given sequence contains 1, 2, 3, 4, and 5, but 0 is missing. Since 0 is the smallest non-negative integer not present in the sequence, it is the MEX.

How to Find MEX of an array (Without Updates) ?

The idea is to use a map to store frequency of each element of an array. Then, iterate through non-negative integers from 0 to N, and return the first integer with a frequency of 0 in the map. This integer represents the smallest non-negative integer not present in the array, which is the MEX.


Output
MEX of the array: 3

Time Complexity:O(N log N) due to insertions in the map and an additional O(N) iteration for finding the MEX, making it effectively O(N log N).
Auxiliary Space :O(N) for storing the frequency map.

Further Optimization: We can use a Set instead of map to optimize the space used. However time and space complexity remain the same.

How to Find MEX of an array (With Updates) ?

Given an array arr of size N, and q queries. In each query you are given two numbers (idx, val), you have to update the element at index idx with value val. The task is to determine the MEX of the array after each query.

Approach:

The idea is to use a map to store frequency of each element of an array. Then insert all the numbers in the set from 0 to N which are not present in the array. For each query, update the array and maintain the map and set accordingly. The MEX for each query is determined by selecting the smallest element from the set and added to the result.

Step-by-step approach:

  • Create a map to store the frequency of array elements and a set to store the elements(from 0 to N) which are not present in the array.
  • For each query.
    • Decrement the frequency of the element which is being replaced. If the frequency becomes 0, that element is added to set since it is not present in the array.
    • Increment the frequency of the updated element. Remove the updated element from the set if the set contains it, since the updated element will be present in the set.
  • The MEX for each query is determined by selecting the smallest element from the set and added to the result.

Below is the implementation of above approach:


Output
MEX of the array after query 1 : 0
MEX of the array after query 2 : 1
MEX of the array after query 3 : 5
MEX of the array after query 4 : 2

Time Complexity:O(N + Q log N), O(N) for initialization, O(log N) for each query due to set operations.
Auxiliary Space:O(N), for the frequency map and set.

How to solve MEX related problems in Competitive Programming ?

MEX Related tasks usually involves finding MEX of the array while updating its elements. Some of the common characteristics of MEX related tasks include:

  • Array Updates - In general we are generally given queries in which we have either add, remove or update the values of array and the task involves finding the MEX after each such query.
  • Data Structures- To optimally compute MEX after each query, set and maps are generally used because both provide fast lookup time for the elements (log n), also they keep elements sorted making it easier to find the minimum non-present element (MEX).

Let us consider few problems involving MEX:

Problem 1: Given an empty array arr[] and a integer m. You are given q queries. In ithquery you add a element yi to the array. After each query you can perform the following operation any number of times: Choose an element arrj and set arrj to arrj +m or arrj -m. The task is maximize the value of MEX of array arr after each query.

The problem can be solved in following way:

Observation: We need to firstly observe that after applying the operation: arrj +m or arrj -m on an element arrj, its modulo with m still remains the same after the operation.

Keep the track of Frequency: We keep track of frequency of remainders when divided by m, i.e., for each number [0,m-1] keep track of its frequency with each query. Let freq0, freq1 ,......, freqm-1 be the frequency of remainders 0, 1, ..... m-1.

Query Updates: During each query, we update the frequency of the remainder, by keeping the set sorted based on frequency and remainder. For example, if the query adds an element y, we calculate y % m to determine its remainder when divided by m and then update its frequency.

MEX Calculation: In each query after updating the frequency of remainder we will find the minimum of {freq0, freq1 ,......, freqm-1 }. If frqj is remainder with minimum frequency the maximum MEX will be equal to: j*frqj + j.

For example:- Suppose m=5 and frequency of remainders 0, 1, 2, 3, 4 are 3, 4, 4, 2 ,5 respectively. The minimum frequency is of remainder 3 with frequency 2. Since frequency of remainder 3 is 2, elements 3 and 8 will be present in the array by performing the operations. So the MEX will be 13 (2*10+3) in this case.

Problem 2: Given an array of n integers each integer lies in range [0,N]. The task is to make the array exactly {0, 1, 2,... , N-1} in almost 2*n operations. In one operation you can pick any element of the array and replace it with MEX of the array.

The problem can be solved in following way:

Observation: MEX of the array after each operation will lie between [0,N] and in the final array for each index i, arri = i. Let cnt be the number of indexes such that arri is not equal to i. Thus the cnt represents number of indexes for which we have to change the value of that index to its correct value.

Perform the Operations: In each operation we will firstly find the MEX of the array. Now we have 2 cases:
Case 1: When Mex of the array is between [0,N-1]: In this case we will simply put the MEX of the array in its correct index, i.e., if min_exc represents the MEX of the array then we will put min_exc in the index min_exc, this will decrease the value of cnt by 1.
Case 2: When Mex of the array is N: In this case, we will put the MEX of the array in any of the index for which arri is not equal to i. The value of cnt will remain the same.

Correctness: The above approach will convert array arr to {0,1,2, .... , N-1} in almost 2*N operations. The maximum value of cnt can be N, and the above approach decreases the value of cnt will by 1 in almost 2 operations because each time we encounter Case 2(MEX of the array is N), next time we perform the operation, we will get Case 1(MEX of the array is not N) since N was inserted in the array in last operation so MEX will not be N. Thus, we will Case 1 in which value of cnt will be decreased by 1. Hence, it takes almost 2*N operations to make the value of cnt 0.

Practice Problems on MEX:

Comment
Article Tags: