CodeChef: Practical coding for everyone whats wrong with this…!
I kept thinking that M would be smaller than n, clearly missing the base case.
Shouldn’t the ans for N=1 be M=0 as 0 xor 1 is 1…??
cleverly framed for input 1.
thumps up…
i cant see the submit button not only in this problem but all past contest problem. can anyone tell me why?
@darkshadows @atulshanbhag
what did u meant u said bit tricks can be used to check if the number is of the form 2^k - 1
please help me to find error in this problem.
Here is my sol CodeChef: Practical coding for everyone
@visharox try testcase for n = 1
I think casting float was the problem? try with double and don’t cast this time to double
I used double and it worked. Why was using float a problem in this case?
because of precision. double offers more precision in the sense say if log(2) to base 2 would be 0.999 in float casting which would be 0 if casted back to int whereas in double it would be rounded off to 1.000
run this and see the pattern
#include <iostream>
using namespace std;
int main() {
for(int i=1;i<256;i++) {
int x = i^(i+1);
cout << i << " " << i+1 << " " << x << endl;
}
return 0;
}
Given an integer N, Chef wants to find the smallest positive integer M such that the bitwise XOR of M and M+1 is N.
Positive integers are greater than 0.
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.
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