Find two numbers from their sum and OR
Given two integers X and Y, the task is to find two numbers whose Bitwise OR is X and their sum is Y. If there exist no such integers, then print “-1”.
Examples:
Input: X = 7, Y = 11
Output: 4 7
Explanation:
The Bitwise OR of 4 and 7 is 7 and the sum of two integers is 4 + 7 = 11, satisfy the given conditions.
Input: X = 11, Y = 7
Output: -1
Naive Approach: The simplest approach to solve the given problem is to generate all possible pairs of the array and if there exist any pairs that satisfy the given condition then print that pairs. Otherwise, print “-1”.
Time Complexity: O(Y)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by using the properties of Bitwise Operators. Consider any two integers A and B, then the sum of two integers can be represented as A + B = (A & B) + (A | B). Now, place the variables X and Y and change the equation as:
=> Y = (A & B) + X
=> (A & B) = Y – X
Therefore the above observations can be deduced with this equation.
Follow the steps below to solve the given problem:
- If the value of Y is less than X, then there will be no solution as Bitwise AND operations are always non-negative.
- Now for the Kth bit in the bit-wise representation of X and Y, if this bit is ‘1’ in (A&B) and “0” in (A | B) then there will be no possible solutions. This is because if the bit-wise AND of two numbers is 1 then is necessary that bit-wise OR should also be 1.
- Otherwise, it is always possible to choose two integers A and B which can be calculated as A = Y – X and B = X.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
#define MaxBit 32
using namespace std;
int possiblePair( int X, int Y)
{
int Z = Y - X;
if (Z < 0) {
cout << "-1" ;
return 0;
}
for ( int k = 0; k < MaxBit; k++) {
int bit1 = (Z >> k) & 1;
int bit2 = (Z >> k) & 1;
if (bit1 && !bit2) {
cout << "-1" ;
return 0;
}
}
cout << Z << ' ' << X;
return 0;
}
int main()
{
int X = 7, Y = 11;
possiblePair(X, Y);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int MaxBit = 32 ;
static void possiblePair( int X, int Y)
{
int Z = Y - X;
if (Z < 0 ) {
System.out.print( "-1" );
}
for ( int k = 0 ; k < MaxBit; k++) {
int bit1 = (Z >> k) & 1 ;
int bit2 = (Z >> k) & 1 ;
if (bit1 != 0 && bit2 == 0 ) {
System.out.print( "-1" );
}
}
System.out.print( Z + " " + X);
}
public static void main(String[] args)
{
int X = 7 , Y = 11 ;
possiblePair(X, Y);
}
}
|
Python3
MaxBit = 32
def possiblePair(X, Y):
Z = Y - X
if (Z < 0 ):
print ( "-1" )
return 0
for k in range (MaxBit):
bit1 = (Z >> k) & 1
bit2 = (Z >> k) & 1
if (bit1 = = 1 and bit2 = = 0 ):
print ( "-1" )
return 0
print (Z, X)
return 0
if __name__ = = '__main__' :
X = 7
Y = 11
possiblePair(X, Y)
|
C#
using System;
public class GFG{
static int MaxBit = 32;
static void possiblePair( int X, int Y)
{
int Z = Y - X;
if (Z < 0) {
Console.Write( "-1" );
}
for ( int k = 0; k < MaxBit; k++) {
int bit1 = (Z >> k) & 1;
int bit2 = (Z >> k) & 1;
if (bit1 != 0 && bit2 == 0) {
Console.Write( "-1" );
}
}
Console.Write( Z + " " + X);
}
static public void Main (){
int X = 7, Y = 11;
possiblePair(X, Y);
}
}
|
Javascript
<script>
let MaxBit = 32;
function possiblePair(X, Y) {
let Z = Y - X;
if (Z < 0) {
document.write( "-1" );
return 0;
}
for (let k = 0; k < MaxBit; k++) {
let bit1 = (Z >> k) & 1;
let bit2 = (Z >> k) & 1;
if (bit1 && !bit2) {
document.write( "-1" );
return 0;
}
}
document.write(Z + " " + X);
return 0;
}
let X = 7,
Y = 11;
possiblePair(X, Y);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
12 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...