Tuesday, June 13, 2017

Hackerrank Solution : Sum vs XOR

Problem Link

Solution Link


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

CodeForces: Xenia and Ringroad(339B) Solution in Java for Beginners

  Xenia and Ringroad(339B) Problem Link: Since this is a beginner's problem, I suggest you try it out yourself first. Java programm...