Find two numbers from their sum and XOR | Set 2
Last Updated :
23 Jul, 2025
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 <bits/stdc++.h>
using namespace std;
// Function to find the value of A and
// B whose sum is X and xor is Y
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 (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 Code
int 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 approach
import java.util.*;
class GFG{
// Function to find the value of A and
// B whose sum is X and xor is Y
static 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 Code
public 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 Y
def 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 Code
if __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 approach
using System;
class GFG{
// Function to find the value of A and
// B whose sum is X and xor is Y
static 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 Code
public 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
<script>
// JavaScript program to implement the above approach
// Function to find the value of A and
// B whose sum is X and xor is Y
function findNums(X, Y)
{
// Initialize the two numbers
let 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) {
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
document.write(A + " " + B);
}
// Driver Code
// Given Sum and XOR of 2 numbers
let X = 17, Y = 13;
// Function Call
findNums(X, Y);
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem