1

For a given array A containing numbers from 1 to N, I want to find the pair of numbers (x,y) which is repeated and missing. Example A = [1, 3, 3] then x = 3 and y = 2.

While I know that this problem can be solved by taking the xor approach mentioned here https://stackoverflow.com/a/5767648/5031518. I am failing to understand the last part of the solution where the values of x and y are extracted from x ^ y by splitting the array based on a set bit.

It would be helpful if someone can explain me why the xor of two list results in the value of x and y respectively.

1 Answer 1

1

At the first stage you calculate xor of values in full range 1..N and in your list

1  2  3 .. x .. N  
1  2  3 ..   .. N  y

xor of all paired items gives 0, so result is p = x xor y

Value p is non-zero, but what bits are set? Those different in x and y.

So we can find any 1-bit from p, call it mask, and perform the same procedure on values 1..N and your list, choosing only values that have this bit set

for all v in 1..N and in list:   
   if ((v & mask) == mask):
       newxor ^= v

After all, newxor contains or x or y (all other items participating here are paired), and we can find another item with

 xy = p ^ newxor

Note: we cannont distinguish what item was repeated, and what was missed, just get pair of them.

For your example

p = 1^2^3^1^3^3 = 1 = 001 binary

repeating procedure with mask 001b engages only odd numbers

 newxor = (1 xor 3) xor (1 xor 3 xor 3) = 3

and the rest number is

 3 xor 1 = 2

For example (1,2,2) we get the same p

p = 1^2^3^1^2^2 = 1 = 001 binary

and the same newxor = 3 and the same rest value xy=2

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.