Lexicographically Largest Array of Prefix MEX Values
Quick Overview
Construct the lexicographically largest sequence of MEX values by removing nonempty prefixes until an integer array is exhausted, including length-based ties.
Lexicographically Largest Array of Prefix MEX Values
Company: BNY Mellon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Other
# Lexicographically Largest Array of Prefix MEX Values
You are given an integer array `nums`. Starting with an empty result, repeatedly do the following until every input element has been removed:
1. Choose a nonempty prefix of the remaining array.
2. Append that prefix's MEX to the result.
3. Remove that prefix, preserving the order of the elements still remaining.
The MEX of an array is the smallest nonnegative integer absent from it. For example, it is `0` when the array contains no zero.
Return the lexicographically largest result obtainable.
To compare two result arrays, inspect their first differing position: the larger value wins. If one array is an exact prefix of the other, the longer array wins.
### Function
`vector<int> maximumMEX(vector<int>& nums)`
### Example 1
**Input:** `nums = [2, 0, 1, 0, 2, 1, 7]`
**Output:** `[3, 3, 0]`
**Explanation:** Remove `[2, 0, 1]`, then `[0, 2, 1]`, then `[7]`. Their MEX values are `3`, `3` and `0`. No prefix can give an initial value larger than `3` because `3` is absent from the entire input.
### Example 2
**Input:** `nums = [9, 9, 0, 2]`
**Output:** `[1, 0]`
**Explanation:** Remove `[9, 9, 0]`, whose MEX is `1`, then `[2]`, whose MEX is `0`. Removing the whole array at once would give `[1]`, which is smaller because it is a proper prefix of `[1, 0]`.
### Constraints
- `1 <= nums.length <= 100000`
- `0 <= nums[i] <= 100000`
### Clarifications
- Every chosen prefix has at least one element, and every original element must eventually be removed.
- Duplicate values are allowed and affect whether required values remain available.
- The result may have a different length from the input; its length is part of the lexicographic comparison.
### What a Strong Answer Covers
- Maximizing earlier result values before comparing later ones.
- Handling suffixes with no zero and the longer-array tie rule.
- Processing the full input without repeatedly copying the remaining array.
Overview: Construct the lexicographically largest sequence of MEX values by removing nonempty prefixes until an integer array is exhausted, including length-based ties.
Lexicographically Largest Array of Prefix MEX Values
BNY Mellon
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0
Lexicographically Largest Array of Prefix MEX Values
You are given an integer array nums. Starting with an empty result, repeatedly do the following until every input element has been removed:
Choose a nonempty prefix of the remaining array.
Append that prefix's MEX to the result.
Remove that prefix, preserving the order of the elements still remaining.
The MEX of an array is the smallest nonnegative integer absent from it. For example, it is 0 when the array contains no zero.
Return the lexicographically largest result obtainable.
To compare two result arrays, inspect their first differing position: the larger value wins. If one array is an exact prefix of the other, the longer array wins.
Function
vector<int> maximumMEX(vector<int>& nums)
Example 1
Input:nums = [2, 0, 1, 0, 2, 1, 7]
Output:[3, 3, 0]
Explanation: Remove [2, 0, 1], then [0, 2, 1], then [7]. Their MEX values are 3, 3 and 0. No prefix can give an initial value larger than 3 because 3 is absent from the entire input.
Example 2
Input:nums = [9, 9, 0, 2]
Output:[1, 0]
Explanation: Remove [9, 9, 0], whose MEX is 1, then [2], whose MEX is 0. Removing the whole array at once would give [1], which is smaller because it is a proper prefix of [1, 0].
Constraints
1 <= nums.length <= 100000
0 <= nums[i] <= 100000
Clarifications
Every chosen prefix has at least one element, and every original element must eventually be removed.
Duplicate values are allowed and affect whether required values remain available.
The result may have a different length from the input; its length is part of the lexicographic comparison.
What a Strong Answer Covers Guidance
Maximizing earlier result values before comparing later ones.
Handling suffixes with no zero and the longer-array tie rule.
Processing the full input without repeatedly copying the remaining array.