3 Approaches to Calculating the Mex of an Array

Правка en8, от isaacbell, 2023-08-11 09:42:53

Important Note: This is my first blog entry, and some mistakes were pointed out in the comments. I appreciate the feedback and have made corrections accordingly.

Mex refers to the minimum non-negative integer that is not present in an array of non-negative integers. Calculating Mex is a common task in various algorithmic problems. In this blog post, we'll explore three different approaches to calculating Mex in an array.

1. Quick Mex for Small Array Values

----------------------------------- The first approach is to use a set when the values in the array are small, specifically when all values are less than 1 million. This approach takes advantage of the set's constant time lookups.

int mex(vector<int>& numberArray) {
  set<int> sett;
  for(int i = 0; i < numberArray.size(); i++) 
    sett.insert(numberArray[i]);
  for(int i = 0; i < 1000001; i++)
    if(!sett.count(i)) return i;
  return -1;
}

2. Sorting Approach

The second approach involves sorting the array and then looping through the sorted array. This approach takes O(N log N) time.

sort(numberArray.begin(), numberArray.end());
int mex = 0;
for(int e : numberArray) {
 if (e == mex) {
  mex++;
 }
}

3. Efficient O(n) Approach

The third approach is an efficient O(n) method.

int mex(vector<int> &a) {
    vector<bool> f(a.size() + 1, 0);
    for (int i : a) if (i <= (int) a.size()) f[i] = 1;
    int mex = 0;
    while (f[mex]) ++mex;
    return mex;
}

This works because the Mex of n integers cannot exceed n.

Conclusion

There are multiple approaches to calculating Mex, and each approach has its own advantages and limitations. The efficient O(n) approach is particularly noteworthy, and it was supplied by @VGTCross. Special thanks to him.

If you have questions or comments, please leave them below.

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en15 Английский isaacbell 2023-08-11 10:53:50 215
en14 Английский isaacbell 2023-08-11 10:48:48 2523
en13 Английский isaacbell 2023-08-11 10:42:02 2165
en12 Английский isaacbell 2023-08-11 10:03:38 16 Tiny change: 'ingly.\n\nMex re' -> 'ingly.\n\nIntro\n=====\n\nMex re'
en11 Английский isaacbell 2023-08-11 10:03:12 22 Tiny change: 'rtant Note: This is my' -> 'rtant Note\n==============\n\nThis is my'
en10 Английский isaacbell 2023-08-11 10:02:38 1518
en9 Английский isaacbell 2023-08-11 09:43:26 37 Tiny change: '------\n\n-----------------------------------\nThe firs' -> '------\n\nThe firs'
en8 Английский isaacbell 2023-08-11 09:42:53 33
en7 Английский isaacbell 2023-08-11 09:41:47 2084
en6 Английский isaacbell 2023-08-11 09:22:20 6
en5 Английский isaacbell 2023-08-11 09:21:48 29
en4 Английский isaacbell 2023-08-11 09:21:07 177
en3 Английский isaacbell 2023-08-11 03:55:35 16 Tiny change: '------\n\n--------------\nThere ar' -> '------\n\nThere ar'
en2 Английский isaacbell 2023-08-11 03:55:09 56
en1 Английский isaacbell 2023-08-11 03:54:26 3119 Initial revision (published)