Reachable Smallest Missing Values After a Single Increment
Arrays & HashingAuto-graded · 22 test casesLast updated
Find every possible smallest-missing value of an array after at most one capped +1 edit, using counting and MEX case analysis in linear time.
You are given a list labels of n integers, where every value lies between 0 and n - 1 inclusive. Values may repeat.
The smallest missing value of a list is the smallest non-negative integer that does not appear in it. For example, the smallest missing value of [0, 1, 3] is 2.
You may perform at most one edit on the list. An edit picks a single position whose value is strictly less than n - 1 and increases that value by exactly 1. Values can never exceed n - 1, so an element equal to n - 1 can never be edited.
Return all the values that the smallest missing value of the list could be after performing zero or one edit, as a sorted list.
Rules
- Doing nothing is a legal choice, so the smallest missing value of the unmodified list is always part of the answer.
- Every possible edit is considered independently, starting from the original list. Edits never combine.
- The result contains each value once, in strictly increasing order.
- If no element can be edited, the result contains only the original smallest missing value.
Constraints
1 <= n <= 2 * 10^50 <= labels[i] <= n - 1- The intended solution runs in
O(n)time. Trying every edit and recomputing the smallest missing value each time takesO(n^2)and is too slow.
Examples
Example 1
Input: reachable_missing_values([0, 2, 1, 2, 4, 0])
Output: [1, 3, 5]
Example 2
Input: reachable_missing_values([0, 1, 2, 3])
Output: [0, 1, 2, 4]
Hints
Hint 1
Let m be the smallest missing value of the original list. Which edited values could possibly change anything at or below m? Edits on elements larger than m cannot matter.
Hint 2
If a value v < m appears exactly once and you increase it, v disappears and everything below v is still present, so the new smallest missing value is exactly v. Remember that elements equal to n - 1 cannot be edited.
Hint 3
The only other way to change the answer is to increase a spare copy of m - 1 (one that has a duplicate), which fills the hole at m. The new answer is then the next value after m that is absent from the original list.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Count how many times each value occurs, then compute m, the smallest missing value of the original list. So every value in 0..m-1 is present and m is absent. Now consider an edit that turns one copy of v into v + 1.
v > m. Nothing at or belowmchanges, so the answer staysm. (v == mcannot happen becausemis absent.)v < mandvoccurs exactly once. After the editvis gone, while all values belowvremain, so the new answer is exactlyv. This is valid only ifv < n - 1, becausen - 1cannot be edited. The cap only matters whenm == n.v < m,voccurs at least twice, andv + 1 < m.vis still present andv + 1was already present, so the answer staysm.v == m - 1and it occurs at least twice.vsurvives and the new copy ofmfills the hole. The new answer is the next value aftermthat is absent from the original counts (ornif all remaining values are present).
Therefore the answer set is:
m(no edit, or any edit that does not change it),- every
v < mwithcount[v] == 1andv < n - 1, - the next gap after
m, ifm >= 1andcount[m - 1] >= 2.
Mark these in a boolean array of size n + 1 and read it out in increasing order, which gives a sorted, duplicate-free result.
Why it is correct
The four cases cover every possible edited value, and each one determines the new smallest missing value exactly. The cap rule removes only the element n - 1, which matters just when the list is a permutation of 0..n-1. The edge cases fall out naturally: if m == 0 the answer is [0], and if n == 1 the single tag 0 equals n - 1, so only [1] is possible.
Complexity
Counting, finding m, the loop over v < m, and the single scan for the next gap are each O(n). Total time is O(n) and space is O(n).
Reference solution
from typing import List
def reachable_missing_values(labels: List[int]) -> List[int]:
n = len(labels)
counts = [0] * n
for x in labels:
counts[x] += 1
# Smallest missing value of the untouched list.
m = 0
while m < n and counts[m] > 0:
m += 1
reachable = [False] * (n + 1)
reachable[m] = True # no edit
# Editing the only copy of v (< m) makes v the new smallest missing value.
# An element equal to n - 1 cannot be edited.
for v in range(min(m, n - 1)):
if counts[v] == 1:
reachable[v] = True
# Editing a spare copy of m - 1 fills the hole at m.
if 0 < m < n and counts[m - 1] >= 2:
nxt = m + 1
while nxt < n and counts[nxt] > 0:
nxt += 1
reachable[nxt] = True
return [v for v, ok in enumerate(reachable) if ok]