How can we efficeiently find MEX(minimum excluded) of an array?

Before contest

Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)

38:03:18

Register now »

Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)

38:03:18

Register now »

*has extra registration

# | User | Rating |
---|---|---|

1 | tourist | 3682 |

2 | ecnerwala | 3603 |

3 | Benq | 3549 |

4 | Radewoosh | 3494 |

5 | Petr | 3452 |

6 | ksun48 | 3413 |

7 | maroonrk | 3406 |

8 | Miracle03 | 3314 |

9 | scott_wu | 3313 |

10 | Um_nik | 3299 |

# | User | Contrib. |
---|---|---|

1 | 1-gon | 214 |

2 | Errichto | 188 |

3 | awoo | 187 |

3 | rng_58 | 187 |

5 | SecondThread | 184 |

6 | Um_nik | 177 |

7 | Ashishgup | 176 |

8 | maroonrk | 173 |

9 | antontrygubO_o | 171 |

10 | -is-this-fft- | 169 |

How can we efficeiently find MEX(minimum excluded) of an array?

↑

↓

Codeforces (c) Copyright 2010-2021 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Apr/22/2021 03:31:44 (j3).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

Auto comment: topic has been updated by u1804011 (previous revision, new revision, compare).For c++, just use set. But it will only work for arr[i] <= 1000000.

If value is not matter, we can compress the array so that each element will have the value in

`[1..n]`

UwUI don't think there is any better way than

`O(n)`

.Example: n = 9, 0 3 5 7 2 4 1 10 19

delete (>= n) 0 3 5 7 2 4 1

sorting 0 1 2 3 4 5 7

analysis of 0-index

0 in 0 position, 1 in 1 position ... 5 in 5 position, 7 in 6 position

then the answer is 6

//google translate

Consider your values are stored in

`vector<int> a`

.$$$O(N*lg(N))$$$

Although, if range for $$$A[i]$$$ are small enough, you can then sort in $$$O(N)$$$.

I saw all the other comments almost cover the approaches, although I'd like to share one of the approaches I use most frequently. The approach takes O(NlogN) precomputation, but each MEX query takes O(1) time and updates the MEX of an array in O(logN) for every point update in the array.

In C++:-Precomputation:- Create a set and a frequency map(or array).

- Fill the set with all numbers from 0 to n+1.

- Now, traverse in the array, if the element is within [0, n+1] remove it from the set, and keep updating the frequency map(or array). It takes at worst O(NlogN) time.

- Now, for any state, the set.begin() will give the MEX of the current array.

For updates:- If the element to be replaced, is within [0, n+1] then update its frequency in the frequency map(or array) and if after updating, the frequency of that element becomes zero, insert it into our set. It takes O(logN) time.

- Now if the element which is placed in that position is within [0, n+1] then update its frequency in the frequency map(or array) and remove it from our set(if its present). It takes O(logN) time.

- And yet again, after any update, set.begin() will give us the current MEX in O(1).

A sample code where I used this technique to find MEX in queries : 86004255

:)I am also using this one.

Great description ;)

Cool and Thanks!

:)Very good explanation

Thanks!

:)thanks for ur contribution

it's my pleasure!

:)Awesome explanation...

Hii

I'm assuming that the elements are in the range [0, n] inclusive where n is the length of the array.

I just use a bool array to keep track of the elements I've encountered and I output the smallest one I haven't.

Actually the range of the elements won't matter, only the elements in the range [0, n+1] will affect the MEX.

:)