Find two numbers from their sum and XOR
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.
- 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)
Let the summation be S and XOR be X.
Below is the implementation of above approach:
C++
#include <iostream>
using namespace std;
void compute(unsigned long int S,
unsigned long int X)
{
unsigned long int A = (S - X)/2;
int a = 0, b = 0;
for ( int i=0; i<8* sizeof (S); i++)
{
unsigned long int Xi = (X & (1 << i));
unsigned long int Ai = (A & (1 << i));
if (Xi == 0 && Ai == 0)
{
}
else if (Xi == 0 && Ai > 0)
{
a = ((1 << i) | a);
b = ((1 << i) | b);
}
else if (Xi > 0 && Ai == 0)
{
a = ((1 << i) | a);
}
else
{
cout << "Not Possible" ;
return ;
}
}
cout << "a = " << a << endl << "b = " << b;
}
int main()
{
unsigned long int S = 17, X = 13;
compute(S, X);
return 0;
}
|
Java
class GFG {
static void compute( long S, long X)
{
long A = (S - X)/ 2 ;
int a = 0 , b = 0 ;
final int LONG_FIELD_SIZE = 8 ;
for ( int i= 0 ; i< 8 *LONG_FIELD_SIZE; i++)
{
long Xi = (X & ( 1 << i));
long Ai = (A & ( 1 << i));
if (Xi == 0 && Ai == 0 )
{
}
else if (Xi == 0 && Ai > 0 )
{
a = (( 1 << i) | a);
b = (( 1 << i) | b);
}
else if (Xi > 0 && Ai == 0 )
{
a = (( 1 << i) | a);
}
else
{
System.out.println( "Not Possible" );
return ;
}
}
System.out.println( "a = " + a + "\nb = " + b);
}
public static void main(String[] args) {
long S = 17 , X = 13 ;
compute(S, X);
}
}
|
Python3
def compute(S, X):
A = (S - X) / / 2
a = 0
b = 0
for i in range ( 64 ):
Xi = (X & ( 1 << i))
Ai = (A & ( 1 << i))
if (Xi = = 0 and Ai = = 0 ):
pass
elif (Xi = = 0 and Ai > 0 ):
a = (( 1 << i) | a)
b = (( 1 << i) | b)
elif (Xi > 0 and Ai = = 0 ):
a = (( 1 << i) | a)
else :
print ( "Not Possible" )
return
print ( "a = " ,a)
print ( "b =" , b)
S = 17
X = 13
compute(S, X)
|
C#
using System;
public class GFG {
static void compute( long S, long X)
{
long A = (S - X)/2;
int a = 0, b = 0;
for ( int i=0; i<8* sizeof ( long ); i++)
{
long Xi = (X & (1 << i));
long Ai = (A & (1 << i));
if (Xi == 0 && Ai == 0)
{
}
else if (Xi == 0 && Ai > 0)
{
a = ((1 << i) | a);
b = ((1 << i) | b);
}
else if (Xi > 0 && Ai == 0)
{
a = ((1 << i) | a);
}
else
{
Console.WriteLine( "Not Possible" );
return ;
}
}
Console.WriteLine( "a = " + a + "\nb = " + b);
}
public static void Main() {
long S = 17, X = 13;
compute(S, X);
}
}
|
Javascript
function compute(S, X)
{
var A = Math.floor((S - X) / 2);
var a = 0;
var b = 0;
for ( var i = 0; i < 64; i++)
{
var Xi = (X & (1 << i));
var Ai = (A & (1 << i));
if (Xi == 0 && Ai == 0)
{
continue ;
}
else if (Xi == 0 && Ai > 0)
{
a = ((1 << i) | a);
b = ((1 << i) | b);
}
else if (Xi > 0 && Ai == 0)
{
a = ((1 << i) | a);
}
else
{
console.log( "Not Possible" );
return ;
}
}
console.log( "a = " + a);
console.log( "b = " + b);
}
var S = 17;
var X = 13;
compute(S, X);
|
Output
a = 15
b = 2
Time Complexity: O(logn)
Auxiliary Space: O(1)
Last Updated :
31 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...