KatInterview

Possible Minimum Excluded Values After One Increment

Arrays & HashingAuto-graded · 21 test cases
MediumAuto-gradedArrays & Hashingarrayshash-tablefrequency-array

Last 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?