DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

MEX with XOR

Back to problem

Editorial

1. MEX after deleting one element

Let mmm be the MEX of the entire array AAA. Every value from 000 to m−1m-1m−1 occurs, and mmm does not occur.

Deleting AiA_iAi​ gives two cases:

  • If Ai<mA_i<mAi​<m and AiA_iAi​ occurs exactly once, then Mi=AiM_i=A_i.

Consequently, the positions with Bi=0B_i=0Bi​=0 are exactly the positions containing a value below mmm that occurs once. At every position with Bi≠0B_i\ne0Bi​, we must have

Ai=Bi⊕m,A_i=B_i\oplus m,Ai​=Bi​⊕m,

where ⊕\oplus⊕ denotes bitwise XOR.

Let ZZZ be the number of zeros in BBB, and let f(v)f(v)f(v) be the frequency of a positive value vvv in BBB. Once mm is fixed, all positions with nonzero are determined.

If one of their resulting values below mmm occurs exactly once, the candidate is impossible: that position would have to produce Bi=0B_i=0Bi​=0. Values occurring at least twice cause no problem. The remaining missing values below mmm can be assigned, one each, to the zero positions of BBB.

Thus, a candidate mmm is feasible if and only if:

  • For every positive vvv with v⊕m<mv\oplus m<mv⊕m<m, we have f(v)≠1f(v)\ne1f(v)=.

Since mmm is the MEX of an array of length NNN, we have 0≤m≤N0\le m\le N0≤m≤N.

2. Small inputs: enumerate the MEX

Try every m=0,1,⋯ ,Nm=0,1,\cdots,Nm=0,1,⋯,N. Set Ai=Bi⊕mA_i=B_i\oplus mAi​=B 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 O(N2)O(N^2)O(N2) time, giving an O(N3)O(N^3)O(N3) algorithm for subtask 3.

Using the necessary and sufficient conditions above is faster. For each candidate, count the frequencies of values below mmm, check that none occurs once, and check that exactly ZZZ values are missing. Each candidate takes O(N)O(N)O(N) time, yielding an O(N2)O(N^2)O(N2) algorithm for subtask 4.

3. Reduce XOR comparisons to the highest set bit

Let hhh be the position of the highest set bit of a positive value vvv. The numbers v⊕mv\oplus mv⊕m and mmm agree at every higher bit and differ at bit hhh. Therefore,

v⊕m<m⟺bit h of m is 1.v\oplus m<m \quad\Longleftrightarrow\quad \text{bit }h\text{ of }m\text{ is }1.v⊕m<m⟺bit h of m is 1.

Group the positive values in BBB by their highest set bit. For each bit hhh, compute:

  • ChC_hCh​, the number of distinct positive values whose highest set bit is hhh.
  • Whether the group contains a value that occurs exactly once.

If the group contains a value occurring once, bit hhh cannot be set in mmm. Selecting it would produce a unique value below mmm at a nonzero position.

Exclude these forbidden bits, and let shs_hsh​ indicate whether bit hhh is selected. The number of distinct values below mmm already provided by nonzero positions is ∑hChsh\sum_h C_hs_h∑. Hence

Z=m−∑hChsh=∑h(2h−Ch)sh.Z=m-\sum_h C_hs_h=\sum_h(2^h-C_h)s_h.Z=m−h∑​Ch​

For each allowed bit, define a weight

Wh=2h−Ch.W_h=2^h-C_h.Wh​=2h−Ch​.

We need to select a subset of the allowed bits whose weights sum to ZZZ.

There are exactly 2h2^h2h positive integers with highest set bit hhh. Thus, Ch≤2hC_h\le2^hCh​≤2 and every weight is nonnegative.

4. Subset-sum DP and reconstruction

Let dp[s]dp[s]dp[s] indicate whether a subset of the processed bits has total weight sss. Initially, only dp[0]dp[0]dp[0] is true.

For each allowed bit with positive weight WhW_hWh​, process s=Z,Z−1,⋯ ,Whs=Z,Z-1,\cdots,W_hs=Z,Z−1,⋯,W and update

dp[s]←dp[s]∨dp[s−Wh].dp[s]\leftarrow dp[s]\lor dp[s-W_h].dp[s]←dp[s]∨dp[s−Wh​].

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 ZZZ can also be skipped.

If dp[Z]dp[Z]dp[Z] is false, print -1. Otherwise, reconstruct mmm from the selected bits and construct AAA:

  • At every nonzero position, set Ai=Bi⊕mA_i=B_i\oplus mAi​=Bi​⊕m.
  • Find the values from 000 to that have not appeared.

5. Correctness proof

For any valid answer with MEX mmm, the first observation implies that no forbidden bit is selected and that the selected weights sum to ZZZ. Therefore, if an answer exists, the DP can reach ZZZ. Omitting zero-weight bits preserves the sum, so skipping them is safe.

Conversely, consider the value mmm reconstructed by the DP. Every distinct positive value in a selected group occurs at least twice. Let DDD be the total number of these distinct values. Then

m=Z+D≤Z+N−Z2≤N.m=Z+D\le Z+\frac{N-Z}{2}\le N.m=Z+D≤Z+2N−Z​

Thus, the reconstructed value is within the possible MEX range.

At a nonzero position, AiA_iAi​ cannot equal mmm, since that would imply Bi=0B_i=0Bi​=0. 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 mmm and does not contain mmm, so its MEX is mmm. A zero position contains a unique value below mmm, making the MEX after deletion equal to that value. Deleting a nonzero position leaves the MEX equal to mmm. Both cases satisfy the required XOR equation.

Finally, m≤N<230m\le N<2^{30}m≤N<230 and Bi<230B_i<2^{30}Bi​< imply . The missing values are smaller than , so every output value is within the allowed range.

6. Special-case subtasks

In subtask 1, every Bi>0B_i>0Bi​>0. Set A=BA=BA=B. Since zero is absent, the MEX remains zero after any deletion. This gives an O(N)O(N)O(N) solution.

In subtask 2, every BiB_iBi​ is either zero or one. If all values are zero, any permutation of 0,1,⋯ ,N−10,1,\cdots,N-10,1,⋯,N−1 works. Otherwise, a solution exists if and only if ZZ is even.

To prove this, consider the value m⊕1m\oplus1m⊕1 assigned to every position with Bi=1B_i=1Bi​=1. If mmm is even, this value is m+1m+1, 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 ZZZ, choose m=Zm=Zm=Z. Assign 0,1,⋯ ,Z−10,1,\cdots,Z-10,1,⋯,Z−1 to the zero positions and Z+1Z+1 to the one positions. This construction takes time.

7. Complexity

Sorting the positive input values gives their frequencies and the per-bit information in O(Nlog⁡N)O(N\log N)O(NlogN) time. There are 30 possible highest-bit positions, so the DP takes O(30Z)O(30Z)O(30Z) time. Reconstructing the answer takes O(N)O(N)O(N) time.

The total time complexity is O(Nlog⁡N+30Z)O(N\log N+30Z)O(NlogN+30Z), and the space complexity is O(N)O(N)O(N). The total bound of 200 000200\,000200000 on NN across all test cases makes this sufficient for the full constraints.

Solution written by GPT

Mi​
=
Ai​
  • Otherwise, Mi=mM_i=mMi​=m.
  • =
    0
    m
    BiB_iBi​
    1
  • The number of distinct values below mmm produced by nonzero positions is exactly m−Zm-Zm−Z.
  • i​
    ⊕
    m
    mmm
    ZZZ
    h
    ​
    Ch​
    sh​
    sh​
    =
    h∑​(2h−
    Ch​)sh​.
    h
    h​
    m−1m-1
    m−1
  • Assign these missing values, one each, to positions with Bi=0B_i=0Bi​=0.
  • ≤
    N.
    DD
    D
    mmm
    m−D=Zm-D=Zm−D=Z
    2
    30
    Bi⊕m<230B_i\oplus m<2^{30}Bi​⊕m<230
    mmm
    Z
    m+
    1
    mmm
    Z=mZ=mZ=m
    mmm
    m−1m-1m−1
    Z=m−1Z=m-1Z=m−1
    ZZZ
    Z+1
    O(N)O(N)O(N)
    N