UtilityToolsLab

© 2026 UtilityToolsLab. Built and maintained by the UtilityToolsLab Team.

Free eBooks·About·Changelog·Privacy Policy·Terms of Service·Report a bug
HomeCompetitive ProgrammingMEX Calc

Related Tools

Code FormatterMatrix GeneratorComplexity CalcBit VisualizerBitmask PlannerPrime FactorsInterval MergerMatrix RotationPBDS GeneratorSegment TreeGraph VisualizerStress TesterModulo CalcConvex HullPath FinderOffline JudgeBig-O AnalyzerCombinatoricsDP Table BuilderExtended GCDSieve VisualizerBinary SearchSorting VisualizerSparse Table RMQUnion-Find DSU

MEX Array Calculator

Input a comma-separated array and see the MEX (Minimum Excluded) computed step-by-step with frequency table and visual walkthrough.

You Might Also Like

All Competitive Programming

Graph Visualizer

Paste CP-style edge lists and watch a force-directed graph build itself. Drag nodes, toggle directed and 0/1-indexed, then copy the adjacency list.

Stress Tester

Paste your brute force, optimal solution and test generator to get a downloadable Python or Bash stress-test script that finds counter-examples.

Modulo Calc

Compute modular inverse, fast power modulo, and nCr mod p using BigInt precision. One-click C++ snippet output for each operation.

Convex Hull

Click the canvas to place coordinate points. Renders the enclosing convex hull polygon live using Andrew's monotone chain. Outputs C++ points vector.

MEX is the smallest non-negative integer missing from a set, and it turns up constantly in game theory and array problems because it is the exact quantity a Grundy number needs. The MEX Calculator computes it and, more usefully, shows the search that got there one candidate at a time.

Each step is a row: the value being tested, whether it appeared, and its frequency. Green rows are values found and skipped, and the single red row is the answer. That layout makes the definition hard to misremember, since the stopping condition is visible rather than implied.

A frequency table sits at the foot, and the input array is drawn as tiles with the value just below the MEX highlighted. Five example chips cover the shapes that catch people out. All of it is computed in the page as you type.

Worked Example: MEX of 2, 0, 1, 5, 3

That array loads by default. The steps read 0 present, 1 present, 2 present, 3 present, then 4 absent, so the MEX is 4. Note that 5 is in the array and contributes nothing: MEX cares only about the unbroken run from 0 upward, so a value far above the gap is irrelevant. Press the Missing 0 chip for the counterpart, where 1, 2, 3, 4 has a MEX of 0 and the search ends on its first step.

Tricky Inputs: Duplicates, Negatives and Empty

  • Duplicates change nothing. The Duplicates chip loads 0, 0, 1, 1, 3, whose MEX is 2, and the frequency table shows the repeats without them affecting the answer.
  • Negative numbers are filtered out before the search, so -5, 0, 1 is treated as 0, 1 and returns 2. MEX is defined over non-negative integers, and the tool enforces that silently.
  • An empty input draws nothing at all rather than reporting 0. Mathematically the MEX of an empty set is 0, so treat the blank state as “no input” rather than as an answer.
  • The All same chip loads 5, 5, 5, 5, giving a MEX of 0 after a single step. A large value present many times still leaves 0 missing.
  • Text that yields no integers returns Please enter valid non-negative integers separated by commas or spaces. Values may be separated by commas, spaces, or both at once.
  • MEX of an array of length n is always between 0 and n inclusive, which is the bound that makes a linear-time frequency scan possible and is the reason the search cannot run away.

Input array (5 elements):

20153

MEX Computation Steps

0✅ 0 is in the array — skipfreq[0] = 1
1✅ 1 is in the array — skipfreq[1] = 1
2✅ 2 is in the array — skipfreq[2] = 1
3✅ 3 is in the array — skipfreq[3] = 1
4❌ 4 is NOT in the array — MEX found!freq[4] = 0

MEX of the array

4

The smallest non-negative integer not present in the array.

Frequency Table

val0×1
val1×1
val2×1
val3×1
val5×1