I saw some solutions where programmers were checking if input was a power of 2, then there were printing n/2 as there answer and -1 otherwise except in the case of n=1.
Why it is so? Is this a property of XOR ?
Can anybody provide a little insight on this or a reference to the reason we solved this way.
yeah, ((M) XOR (M+1) is always of the form (2^k - 1) hence the number should be always of the form 2^k-1 otherwise we have to print -1 and to check if number is of form 2^k-1 we can AND number ie. 2^k-1 with number+1 ie. 2^k and if result is ‘0’ than it is of form 2^k-1 otherwise not because 2^k-1 = {01111…} and 2^k will be = {10000…} and hence AND will give ‘0’.
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
long long int n;
while(t--){
cin>>n;
cout<<(( (n&(n+1))!= 0) ? -1 :( (n!=1) ? n>>1: 2))<<endl;
}
return 0;
}
you can make try any two number M ^ (M+1) always you get number in form of 2^k-1 as @chiragjn and @glow for testing this you can just run a loop to checking this.
if number is in form of 2^k-1 than you can give ans n>>1
TAKE CARE OF N=1 ANSWER IS 2 as problem statement mentioned M should be positive
I think the others have answered it well. I’m just adding a point that during the contest, I checked the XOR of consecutive numbers upto 16 (like @chiragjn has showed).
And i was rational about whether the hypothesis that *Only number of the form 2^i-1 could be formed by XOR of consecutive two natural numbers. (i being a natural no.) * was true or not. So I checked using the a brute force till 2^10=1024.
Detailed analysis:
Given N, find smallest value of X such that X^(X+1)=N (where ^ is XOR and X>0).
If X is even, therefore X+1 is odd.
In this case the X^(X+1) = 1. It is because only the LSB is changed in the binary representation.
X is odd, X+1 is even. We can see that that X^(X+1) is the same value as the number of contiguous 1’s in the binary representation of X.
This means that N must be of form 111…111 otherwise its not possible.
In this case, we do a right shift ans=(N>>1);
4. The tricky case is of 1. We know that 0^1 = 1. So according to the above logic, X=0 but
as X>0 constraint is not satisfied. We check for other ans. As 2^3 = 1, thus for case N=1, ans=2.
5. Now we can check whether a no. N is of the form of 2^i-1 by various methods like: Binary representation contains all 1, or a better one: The truth value of (N & (N+1)) equals 0.
Hey, it’s not just simple coincidence that M^(M+1) becomes of the form 2^k-1.
Let me explain in the best way I can. I’ll do that in two parts.
Part 1:
Observe:
11 or 111 or 11111 what’s special about them…
If you convert them into decimal, they will become(I’m writing for 11111)
2^4+2^3+2^2+2^1+2^0, which will evaluate to
2^0(2^5-1)/(2-1)…(as per the Geometric Progression formula).(a(r^n-1)/(r-1)).
And this happens only when there are continuous 1’s.(this should be clear by now)
Part 2:
Okay now the second part:
What does M^(M+1): this would be much more clear if you know binary addition.
Let M be 1110000111
So to know how M+1 looks like lets add “1” to “1110000111”
1110000111
0000000001
1110001000
So you see the last continuous 1’s(until there is one zero 1110000"111" ) have been flipped into 0’s
and this happens only till the first zero (in our case it is the fourth digit from right) and this zero itself
is flipped into a 1.
Now if you notice that since things get flipped, and if we XOR them they simply get added…
HOPE you understood, if you still find anything confusing, ask here itself…