Open In App

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:

Below is the implementation of the above approach.




// C++ program for the above approach
 
#include <bits/stdc++.h>
#define MaxBit 32
using namespace std;
 
// Function to find the two integers from
// the given sum and Bitwise OR value
int possiblePair(int X, int Y)
{
    int Z = Y - X;
 
    // Check if Z is non negative
    if (Z < 0) {
        cout << "-1";
        return 0;
    }
 
    // Iterate through all the bits
    for (int k = 0; k < MaxBit; k++) {
 
        // Find the kth bit of A & B
        int bit1 = (Z >> k) & 1;
 
        // Find the kth bit of A | B
        int bit2 = (Z >> k) & 1;
 
        // If bit1 = 1 and bit2 = 0, then
        // there will be no possible pairs
        if (bit1 && !bit2) {
            cout << "-1";
            return 0;
        }
    }
 
    // Print the possible pairs
    cout << Z << ' ' << X;
 
    return 0;
}
 
// Driver Code
int main()
{
    int X = 7, Y = 11;
    possiblePair(X, Y);
 
    return 0;
}




// Java code for above approach
import java.util.*;
 
class GFG{
 
static int MaxBit = 32;
 
// Function to find the two integers from
// the given sum and Bitwise OR value
static void possiblePair(int X, int Y)
{
    int Z = Y - X;
 
    // Check if Z is non negative
    if (Z < 0) {
        System.out.print("-1");
    }
 
    // Iterate through all the bits
    for (int k = 0; k < MaxBit; k++) {
 
        // Find the kth bit of A & B
        int bit1 = (Z >> k) & 1;
 
        // Find the kth bit of A | B
        int bit2 = (Z >> k) & 1;
 
        // If bit1 = 1 and bit2 = 0, then
        // there will be no possible pairs
        if (bit1 != 0 && bit2 == 0) {
            System.out.print("-1");
        }
    }
 
    // Print the possible pairs
    System.out.print( Z + " " + X);
 
}
 
// Driver Code
public static void main(String[] args)
 
{
    int X = 7, Y = 11;
    possiblePair(X, Y);
}
}
 
// This code is contributed by avijitmondal1998.




# Python 3 program for the above approach
MaxBit = 32
 
# Function to find the two integers from
# the given sum and Bitwise OR value
def possiblePair(X, Y):
    Z = Y - X
 
    # Check if Z is non negative
    if (Z < 0):
        print("-1")
        return 0
 
    # Iterate through all the bits
    for k in range(MaxBit):
       
        # Find the kth bit of A & B
        bit1 = (Z >> k) & 1
 
        # Find the kth bit of A | B
        bit2 = (Z >> k) & 1
 
        # If bit1 = 1 and bit2 = 0, then
        # there will be no possible pairs
        if (bit1 == 1 and bit2 == 0):
            print("-1")
            return 0
 
    # Print the possible pairs
    print(Z, X)
 
    return 0
 
# Driver Code
if __name__ == '__main__':
    X = 7
    Y = 11
    possiblePair(X, Y)
     
    # This code is contributed by SURENDRA_GANGWAR.




//C# code for the above approach
using System;
 
public class GFG{
   
static int MaxBit = 32;
 
// Function to find the two integers from
// the given sum and Bitwise OR value
static void possiblePair(int X, int Y)
{
    int Z = Y - X;
 
    // Check if Z is non negative
    if (Z < 0) {
        Console.Write("-1");
    }
 
    // Iterate through all the bits
    for (int k = 0; k < MaxBit; k++) {
 
        // Find the kth bit of A & B
        int bit1 = (Z >> k) & 1;
 
        // Find the kth bit of A | B
        int bit2 = (Z >> k) & 1;
 
        // If bit1 = 1 and bit2 = 0, then
        // there will be no possible pairs
        if (bit1 != 0 && bit2 == 0) {
            Console.Write("-1");
        }
    }
 
    // Print the possible pairs
    Console.Write( Z + " " + X);
 
}
 
// Driver Code
 
    static public void Main (){
 
        // Code
      int X = 7, Y = 11;
    possiblePair(X, Y);
    }
}
// This code is contributed by Potta Lokesh




<script>
// Javascript program for the above approach
let MaxBit = 32;
 
// Function to find the two integers from
// the given sum and Bitwise OR value
function possiblePair(X, Y) {
  let Z = Y - X;
 
  // Check if Z is non negative
  if (Z < 0) {
    document.write("-1");
    return 0;
  }
 
  // Iterate through all the bits
  for (let k = 0; k < MaxBit; k++) {
    // Find the kth bit of A & B
    let bit1 = (Z >> k) & 1;
 
    // Find the kth bit of A | B
    let bit2 = (Z >> k) & 1;
 
    // If bit1 = 1 and bit2 = 0, then
    // there will be no possible pairs
    if (bit1 && !bit2) {
      document.write("-1");
      return 0;
    }
  }
 
  // Print the possible pairs
  document.write(Z + " " + X);
 
  return 0;
}
 
// Driver Code
 
let X = 7,
  Y = 11;
possiblePair(X, Y);
 
// This code is contributed by gfgking.
</script>

Output: 
4 7

 

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


Article Tags :