Possible Minimum Excluded Values After One Increment
Arrays & HashingAuto-graded · 21 test casesLast updated
Find all possible values of the minimum non-negative excluded integer (MEX) achievable after at most one element increment.
Given an array tags of length $n$, where each element satisfies $0 \le \text{tags}[i] < n$, determine all possible values for the minimum excluded non-negative integer (MEX) of the array after applying at most one increment operation.
An operation consists of picking a single element $\text{tags}[i]$ such that $\text{tags}[i] < n - 1$ and incrementing it by $1$ (to $\text{tags}[i] + 1$). Values equal to $n - 1$ cannot be incremented.
Applying zero operations (keeping the original array) is always valid. Each possible single operation is evaluated independently on the original array.
The MEX (minimum excluded value) of an array is the smallest integer $x \ge 0$ that does not appear in the array.
Return a sorted list of all unique reachable MEX values.
Constraints
- $1 \le n = \text{len}(\text{tags}) \le 10^5$
- $0 \le \text{tags}[i] \le n - 1$ for all $0 \le i < n$
Examples
Example 1
Input: reachable_priority_gaps([2, 0, 1])
Output: [0, 1, 3]
Example 2
Input: reachable_priority_gaps([0, 0, 2, 2])
Output: [1, 3]
Example 3
Input: reachable_priority_gaps([4, 1, 1, 0, 3])
Output: [0, 2, 5]
Hints
Hint 1
First calculate the frequency of each value in the original array and find its initial MEX, let's call it M.
Hint 2
What happens if you increment a value strictly greater than M?
Hint 3
What happens if you increment a value V < M that appears exactly once versus one that appears multiple times?
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Instead of modifying the array and recalculating the MEX for every index (which would take $O(n^2)$ time), we can analyze how incrementing a value impacts the current MEX, $M$:
- Initial MEX ($M$): The unchanged array yields $M$, so $M$ is always in our result set.
- Incrementing a value $V > M$: Since $M$ is already absent from the array, altering values above $M$ cannot fill the gap at $M$. The MEX remains $M$.
- Incrementing a value $V < M$:
- If $V$ occurs exactly once in the original array, incrementing its only copy removes $V$ entirely. Because $V < M$, $V$ becomes the new smallest missing value, so the new MEX becomes $V$. (Note: $V$ must be $< n - 1$ to be incremented).
- If $V$ occurs more than once, at least one copy of $V$ remains, so $V$ is still present in the array.
- If $V < M - 1$, incrementing one copy of $V$ does not fill $M$ or create any new gap below $M$, so the MEX remains $M$.
- If $V = M - 1$ and $V$ occurs more than once, incrementing one copy of $M - 1$ creates a new copy of $M$. Since $M - 1$ is still present (due to multiple occurrences) and $M$ is now filled, the MEX jumps up to the next missing integer after $M$ (or $n$ if no integers are missing up to $n-1$).
Algorithm
- Compute the frequency array of all elements.
- Find the initial MEX $M$.
- Add $M$ to a result set.
- Iterate over $V$ from $0$ to $\min(M - 1, n - 2)$:
- If
counts[V] == 1, add $V$ to the result set.
- If
- If $M \ge 1$ and $M - 1 \le n - 2$ and
counts[M - 1] > 1:- Find the first missing integer after $M$, and add it to the set.
- Sort and return the set of reachable values.
Complexity
- Time Complexity: $O(n)$ to compute frequencies, locate initial MEX, scan elements below $M$, and potentially find the next missing element.
- Space Complexity: $O(n)$ to store frequency counts and unique reachable values.
from typing import List
def reachable_priority_gaps(tags: List[int]) -> List[int]:
n = len(tags)
counts = [0] * n
for value in tags:
counts[value] += 1
mex = 0
while mex < n and counts[mex] > 0:
mex += 1
reachable = {mex}
if mex == 0:
return [0]
if mex == n:
for value in range(n - 1):
if counts[value] == 1:
reachable.add(value)
return sorted(reachable)
for value in range(mex - 1):
if counts[value] == 1:
reachable.add(value)
predecessor = mex - 1
if counts[predecessor] == 1:
reachable.add(predecessor)
else:
next_missing = mex + 1
while next_missing < n and counts[next_missing] > 0:
next_missing += 1
reachable.add(next_missing)
return sorted(reachable)