All MEX Values After One Optional Increment
Arrays & HashingAuto-graded · 29 test casesLast updated
Find every possible MEX after incrementing at most one array element by 1, respecting an upper-bound constraint.
You are given an array values of length n, where each element satisfies 0 <= values[i] <= n - 1. You may perform at most one operation: select an index i where values[i] < n - 1 and increase values[i] by exactly 1.
The MEX (minimum excludant) of an array is the smallest non-negative integer that does not appear in the array.
Return all possible MEX values the array can have after performing zero operations or exactly one operation. Return them in sorted order.
Rules
- You may perform zero or one operation total. Performing zero operations is always valid, so the MEX of the original array is always reachable.
- An operation chooses one index
iwherevalues[i] < n - 1and replacesvalues[i]withvalues[i] + 1. - Any element equal to
n - 1cannot be selected for an operation. - Each operation is evaluated independently on the original array; operations do not accumulate.
- The array may contain duplicate values.
Constraints
1 <= n == len(values) <= 10^50 <= values[i] <= n - 1
Examples
Example 1
Input: all_mex_after_increment([0, 1, 2])
Output: [0, 1, 3]
Example 2
Input: all_mex_after_increment([0, 0, 3, 1, 1])
Output: [2, 4]
Example 3
Input: all_mex_after_increment([1, 0, 2, 0])
Output: [1, 2, 3]
Hints
Hint 1
Think about what happens when you increment a value v: you remove one occurrence of v and add one occurrence of v+1. How does this affect the MEX?
Hint 2
The MEX can only change if you remove the last occurrence of a value below the current MEX, or if you fill in the current MEX by incrementing m-1.
Hint 3
You don't need to simulate every possible operation. Group elements by their value and reason about each distinct value's effect on the MEX.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Instead of simulating every possible increment operation, observe that incrementing an element with value v does exactly two things: it removes one occurrence of v and adds one occurrence of v + 1. The MEX can only change in specific ways based on how v relates to the current MEX m.
First, compute the original MEX m by finding the smallest non-negative integer not present in the array. This MEX is always reachable (by performing no operation), so add it to the result set.
Next, build a frequency table to count occurrences of each value. For each distinct value v where 0 <= v < n - 1 (values equal to n - 1 cannot be incremented):
-
If
cnt[v] == 1andv < m: Incrementing the only occurrence ofvmeansvdisappears from the array. Sincevis below the current MEX and was the only thing preventingvfrom being the MEX, the new MEX becomesv. -
If
v + 1 == m: Incrementingvcreates an occurrence ofm, filling in the gap at the current MEX. Now the array contains all values from0tom, so the MEX jumps to the next missing value afterm. Compute this lazily (only once) by scanning forward fromm + 1until finding a value with count zero. -
Otherwise: The increment doesn't affect the MEX. If
v >= m, we're only touching values above the MEX. Ifv < mbut has multiple copies, one copy ofvremains, sovis still present and the MEX stays atm.
Return the sorted list of all distinct MEX values collected.
Why It Works
The key insight is that the MEX depends only on which values are present, not their positions. An increment operation has a predictable effect: it can either create a new gap (by removing the last occurrence of some value), fill the current gap (by adding the MEX), or do neither. The three cases above exhaust all possibilities.
Complexity
- Time: O(n). Building the frequency table takes O(n), computing the original MEX takes O(n), and checking each distinct value takes O(n). The lazy computation of the next gap runs at most once and takes O(n). Sorting the result takes O(k log k) where k is the number of distinct MEX values, and k ≤ n.
- Space: O(n) for the frequency table and result set.
def all_mex_after_increment(values: list[int]) -> list[int]:
n = len(values)
cnt = [0] * (n + 2)
for v in values:
cnt[v] += 1
m = 0
while cnt[m]:
m += 1
res = {m}
nxt = None
for v in range(n - 1):
if cnt[v] == 0:
continue
if cnt[v] == 1 and v < m:
res.add(v)
elif v + 1 == m:
if nxt is None:
nxt = m + 1
while cnt[nxt]:
nxt += 1
res.add(nxt)
return sorted(res)