• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
July 07, 2022 |3.5K Views
Find two numbers from their sum and XOR
  Share   Like
Description
Discussion

Given the sum and xor of two numbers X and Y s.t. sum and xor \in [0, 2^{64}-1]    , we need to find the numbers minimizing the value of X
A simple solution is to generate all possible pairs with given XOR. To generate all pairs, we can follow below rules.

If X[i] is 1, then both a[i] and b[i] should be different, we have two cases.
If X[i] is 0, then both a[i] and b[i] should be same. we have two cases.
If X[i] = 0 and A[i] = 0, then a[i] = b[i] = 0. Only one possibility for this bit.
If X[i] = 0 and A[i] = 1, then a[i] = b[i] = 1. Only one possibility for this bit.
If X[i] = 1 and A[i] = 0, then (a[i] = 1 and b[i] = 0) or (a[i] = 0 and b[i] = 1), we can pick any of the two.
If X[i] = 1 and A[i] = 1, result not possible (Note X[i] = 1 means different bits)


Find two numbers from their sum and XOR : https://www.geeksforgeeks.org/find-two-numbers-sum-xor/

Read More