Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions data_structures/arrays/minimum_excluded.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""Find the minimum excluded non-negative integer (MEX) of a list.

Reference: https://cp-algorithms.com/sequences/mex.html
"""


def minimum_excluded(numbers: list[int]) -> int:
"""Return the smallest non-negative integer absent from numbers.

Negative integers and duplicates are allowed. The input is not modified.
Non-integer elements raise TypeError.

For n elements, the answer is in [0, n]: n elements cannot contain all
n + 1 distinct integers in that range. Marking their presence therefore
takes O(n) time and O(n) extra space, regardless of the largest value.

>>> minimum_excluded([])
0
>>> minimum_excluded([0])
1
>>> minimum_excluded([1, 2, 3])
0
>>> minimum_excluded([0, 1, 3])
2
>>> minimum_excluded([3, 0, 2, 1])
4
>>> minimum_excluded([0, 0, 1, 1])
2
>>> minimum_excluded([-3, -1, 0, 2])
1
>>> minimum_excluded([-2, -1])
0
>>> minimum_excluded([0, 1, 10**100])
2
>>> values = [2, 0, 1]
>>> minimum_excluded(values)
3
>>> values
[2, 0, 1]
>>> minimum_excluded([0, 1.5])
Traceback (most recent call last):
...
TypeError: numbers must contain only integers
>>> minimum_excluded([0, "1"])
Traceback (most recent call last):
...
TypeError: numbers must contain only integers
"""
size = len(numbers)
present = [False] * (size + 1)
for number in numbers:
if not isinstance(number, int):
raise TypeError("numbers must contain only integers")
if 0 <= number <= size:
present[number] = True

missing = 0
while present[missing]:
missing += 1
return missing


if __name__ == "__main__":
import doctest

doctest.testmod()
Loading