Back to Data Structures & Algorithms
2026-01-205 min read

MEX (Minimum Excluded element in an array)

Learn MEX (Minimum Excluded element in an array) step by step with clear examples and exercises.

Here's the revised C programming lesson on "MEX (Minimum Excluded element in an array)" with expanded sections and additional content:

Why This Matters

The Minimum Excluded Element (MEX) problem is a crucial concept in competitive programming, graph theory, set theory problems, and other complex issues. Understanding MEX can provide a competitive edge in programming competitions and aid in tackling complex issues with ease. In real-world applications, it helps optimize algorithms and data structures for efficient problem-solving.

Prerequisites

Before proceeding, it's essential to have a solid grasp of:

  1. Basic C programming concepts (variables, loops, functions)
  2. Data structures like arrays and pointers
  3. Familiarity with sets is beneficial but not required
  4. Understanding the concept of frequency distribution in data structures

Core Concept

The Minimum Excluded Element (MEX) is defined as the smallest positive integer that does not appear in a given list. To find the MEX, we create a frequency map to count the occurrences of each number in the array and then iterate through numbers from 1, excluding any with non-zero counts in our frequency map.

Here's an expanded version of the C code:

#include <stdio.h>
#include <stdlib.h>

int *find_mex(int arr[], int size) {
int freq[1000] = {0}; // Assuming maximum number is 999

for (int i = 0; i < size; ++i) {
if (arr[i] > 0 && arr[i] <= 1000) {
freq[arr[i]]++;
}
}

int i = 1;
while (freq[i] > 0 || i == arr[i - 1]) {
++i;
}

return &i;
}

In this code, we first initialize an empty frequency array (freq) of size 1000. We then iterate through the array arr, incrementing the count of numbers that appear more than once and adding new numbers to our frequency map. Afterward, we start a loop from 1 and continue until we find a number not present in our frequency map or with a count of zero.

Worked Example

Let's try finding the MEX for the following array: {4, 3, 2, 5, 6, 7, 8, 9, 1}.

#include <stdio.h>
#include <stdlib.h>

int main() {
int arr[] = {4, 3, 2, 5, 6, 7, 8, 9, 1};
int size = sizeof(arr) / sizeof(arr[0]);

int *mex_ptr = find_mex(arr, size);
printf("Minimum Excluded Element: %d\n", *mex_ptr); // Output: Minimum Excluded Element: 0

return 0;
}

In this example, the MEX is 0 because it's the smallest positive integer not present in the array.

Common Mistakes

  • Forgetting to check for negative numbers: Ensure your input array only contains positive integers or handle them appropriately before finding the MEX.
  • Not initializing the frequency map: If you forget to initialize the frequency map, you might end up with undefined variables and errors in your code.
  • Incorrect handling of repeated numbers: Be sure to correctly increment the count for repeated numbers in your frequency map.
  • Ignoring zeroes: In some cases, you may need to consider the order of zeros when finding the MEX for specific problems.
  • Not considering edge cases: Make sure to handle edge cases like empty arrays or arrays containing only one number appropriately.

Practice Questions

  1. Find the MEX for the array {2, 4, 3, 6, 7, 5, 8, 9}.
  2. Write a C function that finds the MEX of multiple arrays at once (e.g., find_mex([1, 2, 3], [4, 5, 6], [7, 8, 9])).
  3. What if there are repeated zeros in the array? How should we handle them when finding the MEX?
  4. Can we find the MEX using built-in C functions or libraries? If so, what are some potential methods and their trade-offs?
  5. Write a function to find the MEX of an array with negative numbers, excluding those from the calculation.
  6. What is the time complexity of the provided solution for finding the MEX in terms of Big O notation?
  7. How would you optimize the provided solution to improve its performance?
  8. Implement a recursive function to find the MEX of an array using divide and conquer approach.
  9. Write a C program that finds the MEX of an input file containing multiple arrays, each on a separate line.
  10. Compare the efficiency of different algorithms for finding the MEX in terms of time complexity and space complexity.

FAQ

What if there are repeated zeros in the array?

In such cases, you can consider any zero as the MEX. However, it's essential to keep track of the order of zeros when finding the MEX for specific problems.

Can we find the MEX using built-in C functions or libraries?

Yes, there are multiple ways to find the MEX using built-in C functions like calloc() and malloc(), but they might not be as efficient for large arrays due to memory allocation overhead. A custom implementation like the one provided in this guide is often preferred for competitive programming scenarios.

What are some alternative methods for finding the MEX using built-in C functions?

One possible method is to use a bitmask representation for the frequency map:

#include <stdio.h>
#include <stdlib.h>

unsigned int find_mex(int arr[], int size) {
unsigned int freq = 0; // Bitmask representation of frequencies

for (int i = 0; i < size; ++i) {
if (arr[i] > 0 && arr[i] <= 1000) {
freq |= (1 << arr[i]);
}
}

unsigned int i = 1;
while ((freq & (1 << i)) || i == arr[i - 1]) {
++i;
}

return i;
}

This method creates a bitmask from the input array and iterates through numbers from 1, excluding any that are already set in the bitmask or whose predecessor is not present. However, this method may not be as efficient for large arrays due to the time complexity of bitwise operations.

MEX (Minimum Excluded element in an array) | Data Structures & Algorithms | XQA Learn