Need an Explanation

I have been trying to understand the solution for the question :

Though i understood as how the code works and what exactly is happening but cant get the logic on which the code is written.

We take the XOR of numbers with same MSB and keep on changing the array, etc !!!
Could anyone please elaborate the logic behind and why are we performing these operations in our code to get the desired result?