1. MEX after deleting one element
Let be the MEX of the entire array . Every value from to occurs, and does not occur.
Deleting gives two cases:
Consequently, the positions with are exactly the positions containing a value below that occurs once. At every position with , we must have
where denotes bitwise XOR.
Let be the number of zeros in , and let be the frequency of a positive value in . Once is fixed, all positions with nonzero are determined.
If one of their resulting values below occurs exactly once, the candidate is impossible: that position would have to produce . Values occurring at least twice cause no problem. The remaining missing values below can be assigned, one each, to the zero positions of .
Thus, a candidate is feasible if and only if:
Since is the MEX of an array of length , we have .
2. Small inputs: enumerate the MEX
Try every . Set at nonzero positions and fill zero positions with the missing values below . If the number of missing values is not , this candidate is impossible.
We can then delete each position in turn and recompute its MEX directly. Checking one candidate takes time, giving an algorithm for subtask 3.
Using the necessary and sufficient conditions above is faster. For each candidate, count the frequencies of values below , check that none occurs once, and check that exactly values are missing. Each candidate takes time, yielding an algorithm for subtask 4.
3. Reduce XOR comparisons to the highest set bit
Let be the position of the highest set bit of a positive value . The numbers and agree at every higher bit and differ at bit . Therefore,
Group the positive values in by their highest set bit. For each bit , compute:
If the group contains a value occurring once, bit cannot be set in . Selecting it would produce a unique value below at a nonzero position.
Exclude these forbidden bits, and let indicate whether bit is selected. The number of distinct values below already provided by nonzero positions is . Hence
For each allowed bit, define a weight
We need to select a subset of the allowed bits whose weights sum to .
There are exactly positive integers with highest set bit . Thus, and every weight is nonnegative.
4. Subset-sum DP and reconstruction
Let indicate whether a subset of the processed bits has total weight . Initially, only is true.
For each allowed bit with positive weight , process and update
Descending order prevents selecting the same bit more than once. Whenever a state is reached for the first time, record its previous sum and the selected bit so that the choices can be reconstructed.
A zero-weight bit can be omitted without changing the sum, so skip it. Weights larger than can also be skipped.
If is false, print -1. Otherwise, reconstruct from the selected bits and construct :
5. Correctness proof
For any valid answer with MEX , the first observation implies that no forbidden bit is selected and that the selected weights sum to . Therefore, if an answer exists, the DP can reach . Omitting zero-weight bits preserves the sum, so skipping them is safe.
Conversely, consider the value reconstructed by the DP. Every distinct positive value in a selected group occurs at least twice. Let be the total number of these distinct values. Then
Thus, the reconstructed value is within the possible MEX range.
At a nonzero position, cannot equal , since that would imply . These positions provide exactly distinct values below , each occurring at least twice. Assign the remaining values to the zero positions, one each.
The completed array contains every value below and does not contain , so its MEX is . A zero position contains a unique value below , making the MEX after deletion equal to that value. Deleting a nonzero position leaves the MEX equal to . Both cases satisfy the required XOR equation.
Finally, and imply . The missing values are smaller than , so every output value is within the allowed range.
6. Special-case subtasks
In subtask 1, every . Set . Since zero is absent, the MEX remains zero after any deletion. This gives an solution.
In subtask 2, every is either zero or one. If all values are zero, any permutation of works. Otherwise, a solution exists if and only if is even.
To prove this, consider the value assigned to every position with . If is even, this value is , so every value below must come from a zero position and . If is odd, the value is . A single occurrence is impossible, while at least two occurrences imply . Both cases require even .
For even , choose . Assign to the zero positions and to the one positions. This construction takes time.
7. Complexity
Sorting the positive input values gives their frequencies and the per-bit information in time. There are 30 possible highest-bit positions, so the DP takes time. Reconstructing the answer takes time.
The total time complexity is , and the space complexity is . The total bound of on across all test cases makes this sufficient for the full constraints.
Solution written by GPT