Find two numbers from their sum and XOR | Set 2
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++
#include <bits/stdc++.h>
using namespace std;
void findNums( int X, int Y)
{
int A, B;
if (X < Y) {
A = -1;
B = -1;
}
else if ( abs (X - Y) & 1) {
A = -1;
B = -1;
}
else if (X == Y) {
A = 0;
B = Y;
}
else {
A = (X - Y) / 2;
if ((A & Y) == 0) {
B = (A + Y);
}
else {
A = -1;
B = -1;
}
}
cout << A << " " << B;
}
int main()
{
int X = 17, Y = 13;
findNums(X, Y);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findNums( int X, int Y)
{
int A, B;
if (X < Y)
{
A = - 1 ;
B = - 1 ;
}
else if (((Math.abs(X - Y)) & 1 ) != 0 )
{
A = - 1 ;
B = - 1 ;
}
else if (X == Y)
{
A = 0 ;
B = Y;
}
else
{
A = (X - Y) / 2 ;
if ((A & Y) == 0 )
{
B = (A + Y);
}
else
{
A = - 1 ;
B = - 1 ;
}
}
System.out.print(A + " " + B);
}
public static void main(String[] args)
{
int X = 17 , Y = 13 ;
findNums(X, Y);
}
}
|
Python
def findNums(X, Y):
A = 0 ;
B = 0 ;
if (X < Y):
A = - 1 ;
B = - 1 ;
elif ((( abs (X - Y)) & 1 ) ! = 0 ):
A = - 1 ;
B = - 1 ;
elif (X = = Y):
A = 0 ;
B = Y;
else :
A = (X - Y) / / 2 ;
if ((A & Y) = = 0 ):
B = (A + Y);
else :
A = - 1 ;
B = - 1 ;
print A;
print B;
if __name__ = = '__main__' :
X = 17 ;
Y = 13 ;
findNums(X, Y);
|
C#
using System;
class GFG{
static void findNums( int X, int Y)
{
int A, B;
if (X < Y)
{
A = -1;
B = -1;
}
else if (((Math.Abs(X - Y)) & 1) != 0)
{
A = -1;
B = -1;
}
else if (X == Y)
{
A = 0;
B = Y;
}
else
{
A = (X - Y) / 2;
if ((A & Y) == 0)
{
B = (A + Y);
}
else
{
A = -1;
B = -1;
}
}
Console.Write(A + " " + B);
}
public static void Main(String[] args)
{
int X = 17, Y = 13;
findNums(X, Y);
}
}
|
Javascript
<script>
function findNums(X, Y)
{
let A, B;
if (X < Y) {
A = -1;
B = -1;
}
else if (Math.abs(X - Y) & 1) {
A = -1;
B = -1;
}
else if (X == Y) {
A = 0;
B = Y;
}
else {
A = (X - Y) / 2;
if ((A & Y) == 0) {
B = (A + Y);
}
else {
A = -1;
B = -1;
}
}
document.write(A + " " + B);
}
let X = 17, Y = 13;
findNums(X, Y);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
19 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...