XORNUBER - Editorial

Oh…Well Thnkx fr this…

Thanks Bro, I was sad after solving the first problem, as everybody was able to do it, but I was not.
Thanks. you made me learn a new thing. :slight_smile:

it was pure instinct that made me code that. saw the pattern and coded in a jiffy! you should develop such instinct as well. happy coding :slight_smile:

This tricky case was extremely tricky. Couldn’t have found out without this Editorial. Awesome.

like for an hour, i struggled for the tricky case. enjoyed the question.

For me, simply printing (n/2) worked when n is of the form 2^k - 1 (Except for case 1 of course). To check if n is of the form 2^k - 1, I just checked if (n & (n+1)) == 0.

for N=1 ans should be 0 not 2 because XOR of 0 ans 1 is 1 i.e, 0^1=1;
can any one help

Chef wants to find the smallest positive integer M


What’s wrong with this solution?

for n = 1 your code is outputing 0, also your conditions in the else statement is wrong for n = 3 (it should be 1 instead of 7)
Handle the case for n = 1(consider it first) and n = 2 ^ k - 1 separately.

U can check 2 if a number is of type 2 ^ k - 1 in different ways :

  1. iterating over possible highest set bit (as in your code)
  2. considering n = 2 ^ k - 1
    then n + 1 = 2 ^ k
    you have to just check whether n + 1is a power of 2
    there is a nice shortcut to do for checking some number (m > 0) a power of 2 by checking if (m & (m - 1) == 0)

finally to get the ans you can just print n / 2 since we are xoring two consecutive numbers(x , x+1):
so 2 * x + 1 = n => this leads to x = n / 2