Find two numbers from their sum and XOR | Set 2

• Difficulty Level : Medium
• Last Updated : 19 Nov, 2021

Given two integers X and Y, the task is to find the two integers having sum X and Bitwise XOR equal to Y.

Examples:

Input: X = 17, Y = 13
Output: 2 15
Explanation: 2 + 15 = 17 and 2 ^ 15 = 13

Input: X = 1870807699, Y = 259801747
Output: 805502976 1065304723

Naive Approach: Refer to the previous post of this article for the simplest approach to solve the problem.

Time Complexity: O(log N)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized based on the following observations:

A + B = (A ^ B) + 2 * (A & B)
=> X = Y + 2 * (A & B)

While calculating sum, if both bits are 1(i.e., AND is 1), the resultant bit is 0, and 1 is added as carry, which means every bit in AND is left-shifted by 1, i.e. value of AND is multiplied by 2 and added.

Rearranging the terms, the expression (A&B) = (X – Y) / 2 is obtained.

This verifies the above observation.

There exist the following cases:

• If X < Y: In this case, the solution does not exist because (A & B) becomes negative which is not possible.
• If X – Y is odd: In this case, the solution does not exist because (X – Y) is not divisible by 2.
• If X = Y: In this case, A & B = 0. Therefore, the minimum value of A should be 0 and the value of B should be Y to satisfy the given equations.
• Otherwise: A&B = (X – Y)/2 is satisfied, only when ((X – Y)/2) & Y equals 0. If true, the A = (X – Y)/2 and B = A + Y. Otherwise, A = -1 and B = -1.

Below is the implementation of the above approach:

C++

 // C++ program for the above approach#include using namespace std; // Function to find the value of A and// B whose sum is X and xor is Yvoid findNums(int X, int Y){     // Initialize the two numbers    int A, B;     // Case 1: X < Y    if (X < Y) {        A = -1;        B = -1;    }     // Case 2: X-Y is odd    else if (abs(X - Y) & 1) {        A = -1;        B = -1;    }     // Case 3: If both Sum and XOR    // are equal    else if (X == Y) {        A = 0;        B = Y;    }     // Case 4: If above cases fails    else {         // Update the value of A        A = (X - Y) / 2;         // Check if A & Y value is 0        if ((A & Y) == 0) {             // If true, update B            B = (A + Y);        }         // Otherwise assign -1 to A,        // -1 to B        else {            A = -1;            B = -1;        }    }     // Print the numbers A and B    cout << A << " " << B;} // Driver Codeint main(){    // Given Sum and XOR of 2 numbers    int X = 17, Y = 13;     // Function Call    findNums(X, Y);     return 0;}

Java

 // Java program for the above approachimport java.util.*;    class GFG{    // Function to find the value of A and// B whose sum is X and xor is Ystatic void findNums(int X, int Y){         // Initialize the two numbers    int A, B;      // Case 1: X < Y    if (X < Y)    {        A = -1;        B = -1;    }      // Case 2: X-Y is odd    else if (((Math.abs(X - Y)) & 1) != 0)    {        A = -1;        B = -1;    }      // Case 3: If both Sum and XOR    // are equal    else if (X == Y)    {        A = 0;        B = Y;    }      // Case 4: If above cases fails    else    {                 // Update the value of A        A = (X - Y) / 2;          // Check if A & Y value is 0        if ((A & Y) == 0)        {                         // If true, update B            B = (A + Y);        }          // Otherwise assign -1 to A,        // -1 to B        else        {            A = -1;            B = -1;        }    }      // Print the numbers A and B    System.out.print(A + " " + B);}    // Driver Codepublic static void main(String[] args){         // Given Sum and XOR of 2 numbers    int X = 17, Y = 13;      // Function Call    findNums(X, Y);}} // This code is contributed by susmitakundugoaldanga

Python

 # Python program for the above approach # Function to find the value of A and# B whose sum is X and xor is Ydef findNums(X, Y):       # Initialize the two numbers    A = 0;    B = 0;     # Case 1: X < Y    if (X < Y):        A = -1;        B = -1;     # Case 2: X-Y is odd    elif (((abs(X - Y)) & 1) != 0):        A = -1;        B = -1;     # Case 3: If both Sum and XOR    # are equal    elif (X == Y):        A = 0;        B = Y;     # Case 4: If above cases fails    else:         # Update the value of A        A = (X - Y) // 2;         # Check if A & Y value is 0        if ((A & Y) == 0):             # If True, update B            B = (A + Y);         # Otherwise assign -1 to A,        # -1 to B        else:            A = -1;            B = -1;     # Print the numbers A and B    print A;    print B; # Driver Codeif __name__ == '__main__':       # Given Sum and XOR of 2 numbers    X = 17;    Y = 13;     # Function Call    findNums(X, Y); # This code is contributed by shikhasingrajput

C#

 // C# program for the above approachusing System; class GFG{    // Function to find the value of A and// B whose sum is X and xor is Ystatic void findNums(int X, int Y){         // Initialize the two numbers    int A, B;      // Case 1: X < Y    if (X < Y)    {        A = -1;        B = -1;    }      // Case 2: X-Y is odd    else if (((Math.Abs(X - Y)) & 1) != 0)    {        A = -1;        B = -1;    }      // Case 3: If both Sum and XOR    // are equal    else if (X == Y)    {        A = 0;        B = Y;    }      // Case 4: If above cases fails    else    {                 // Update the value of A        A = (X - Y) / 2;          // Check if A & Y value is 0        if ((A & Y) == 0)        {                         // If true, update B            B = (A + Y);        }          // Otherwise assign -1 to A,        // -1 to B        else        {            A = -1;            B = -1;        }    }      // Print the numbers A and B    Console.Write(A + " " + B);}    // Driver Codepublic static void Main(String[] args){         // Given Sum and XOR of 2 numbers    int X = 17, Y = 13;         // Function Call    findNums(X, Y);}} // This code is contributed by Rajput-Ji

Javascript


Output:
2 15

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up