![]() |
VOOZH | about |
Given the sum and xor of two numbers X and Y s.t. sum and xor , we need to find the numbers minimizing the value of X. Examples :
Input : S = 17 X = 13 Output : a = 2 b = 15 Input : S = 1870807699 X = 259801747 Output : a = 805502976 b = 1065304723 Input : S = 1639 X = 1176 Output : No such numbers exist
Variables Used: X ==> XOR of two numbers S ==> Sum of two numbers X[i] ==> Value of i-th bit in X S[i] ==> Value of i-th bit in S
A simple solution is to generate all possible pairs with given XOR. To generate all pairs, we can follow below rules.
Let the summation be S and XOR be X.
Below is the implementation of above approach:
Output a = 15 b = 2
Time Complexity: O(logn)
Auxiliary Space: O(1)