RRJOKE - Editorial

@vijju123 First of all, thank you for so much answering my questions… Thankfully, I got my groove back after writing my question by listening to Kishore Kumar’s hits as well as solving a couple of other practice questions…

Your explanation was absolutely wonderful and it cleared all of my misconceptions and doubts regarding this fantastic question…

Even though, I am sure that my doubts are all gone… Yet, to verify, that I am finally thinking along the correct lines, I am putting forward my own test case…

Input:
4
2 3
1 2
9 0
0 0

In the above input, the only thing which matters is the number of points which, in the above case is, 4.

The coordinates of individual points really does not count, since they are present to confuse us… Hence, they do not play any role in the algorithm to find the correct answer. So, we can safely ignore them.

Output:
4

The answer is based on the fact that final answer (A)= 1 XOR 2 XOR 3 XOR 4 = 4. It is based on the fact that we have marked each point in a sequential order. Hence:

2 3 -> 1
1 2 -> 2
9 0 -> 3
0 0 -> 4

Multiple permutations can exist which will lead to shortest path, like [1, 2, 3, 4], [2, 3, 4, 1], [3, 4, 1, 2], [4, 3, 2, 1]… Each permutation will end up having the same result since XOR operation is commutative in nature.

So, coming to the final output…

We can easily calculate the final answer by XORing each mark i.e, equation A.

Steps:

  1. Initially, A = 1 (Since, 0 XOR 1 = 1)
  2. A = 1 XOR 2 = 3
  3. A = 3 XOR 3 = 0
  4. A = 0 XOR 4 = 4

Hence, the final output: 4… Please do me a favor and tell me whether my output is indeed the right one depending on my input…

1 Like

Your final ans is correct. You got permutation thing right. If you notice closely, we can always generalise answer as-

XOR of all Natural numbers till n

Ie- 1 xor 2 xor 3…(n-1) xor n

And i think you can implement this now. I am glad tgat my explanation helped :slight_smile:

@vijju123 Finally, implemented the code… And, everything came out correctly…

Thank you once again for your assistance… Without you, I guess, I would’ve still have been stuck in a mire…

CS is truly an amazing community… And, I am glad that I am a part of it…

Our community reminds me of a quote said by Professor Albus Dumbledore:

“At Hogwarts, help is always given to those who seek it.” Just replace ‘At Hogwarts’ with ‘In CS’

I am glad it helped dear :slight_smile:

Is there a way to rate a problem??
In that case, I would like to rate it 5 stars

What a question,i thought i was so close to the solution but then i thought to give a read to the editorial because i was not so sure and when i saw the question i waas far away from the real solution.Great Question ! Learnt new things from this question.