Some Observations:

0.Obviously one of M and M+1 is odd, therefore one has 0 as LSB and other has 1 as LSB

1.Even numbers can’t be obtained by consecutive Xoring because even numbers have zero as LSB , which can’t be obtained with respect to property 0.

2.Next with some trial and experimentation

1^2 = 3

2^3 = 1

3^4 = 7

4^5 = 1

5^6 = 3

6^7 = 1

7^8 = 15

…

Numbers which can be obtained by consecutive xoring are 1,3,7,15,… i.e in binary 1,11,111,1111,…

Hence all other numbers should be answered -1

Since all bits are 1,the operands must have all bits different at each position

Numbers which are consecutive and have all bits different are (0,1),(1,2),(3,4),(7,8),… yielding 1,3,7,15,…

therefore the answer is directly n>>1 with ‘1’ being exception and is to answered ‘2’ from above trial answers

https://www.codechef.com/viewsolution/7907417