Tuesday, June 13, 2017

Hackerrank Solution : Sum vs XOR

Problem Link

Solution Link

Exaplanation:

just count total numbers of ZERO present in binary number of given n,
and answer will be the 2 to the power of (total num of zero).


Reason:I believe the justification is that the xor simulates binary addition without
the carry over to the next digit. For the zero digits of n you can either add a 1 or 0 without getting a carry which implies xor = + whereas if a digit in n is 1
then the matching digit in x is forced to be 0 on order to avoid carry. For each 0
in n in the matching digit in x can either being a 1 or 0 with a total combination count of 2^(num of zero).


No comments:

Post a Comment

Beginner's Guide: Some Useful Machine Learning Websites(Part 1)

I have been away for quite some time now. The reason is I was quite busy with my academic thesis and general academic pressure. Now I do fe...