The idea comes from the most common one. I think most of participants(including me) got trapped when trying to fix k-2 ns in the array, but actually it's not the best.
Set n=(1010101010)2 as a sample, I got x=(1001010101)2 and y=(0011111111)2 after the initial process.
My solution tend to be obvious when it's found that swapping some 1s from ns to x while keep it less than n can provide us with space to manipulate the numbers been n early.
Let's try send the second 1 from highest of (1010101010)2 to x, and also the third. (Do them separately in two numbers)
Interestingly we got (1000101010)2 and (1010001010)2. We can operate them in pair, and they become (1000111111)2 and (1010011111)2 when we try to maximize them while follow the limitations.
Repeat the step and now we get an answer.
I can't prove the algorithm is completely right, so I published it and forwarded to a proof.
(I used log2 function, but it can be replace with two pointers or a stack)
Sorry that I may not reply in time, as I'm living in China and it's time to sleep :P




