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++
// C++ program for the above approach#include <bits/stdc++.h>#define MaxBit 32using namespace std;// Function to find the two integers from// the given sum and Bitwise OR valueint 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 Codeint main(){ int X = 7, Y = 11; possiblePair(X, Y); return 0;} |
Java
// Java code for above approachimport java.util.*;class GFG{static int MaxBit = 32;// Function to find the two integers from// the given sum and Bitwise OR valuestatic 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 Codepublic static void main(String[] args){ int X = 7, Y = 11; possiblePair(X, Y);}}// This code is contributed by avijitmondal1998. |
Python3
# Python 3 program for the above approachMaxBit = 32# Function to find the two integers from# the given sum and Bitwise OR valuedef 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 Codeif __name__ == '__main__': X = 7 Y = 11 possiblePair(X, Y) # This code is contributed by SURENDRA_GANGWAR. |
C#
//C# code for the above approachusing System;public class GFG{ static int MaxBit = 32;// Function to find the two integers from// the given sum and Bitwise OR valuestatic 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 |
Javascript
<script>// Javascript program for the above approachlet MaxBit = 32;// Function to find the two integers from// the given sum and Bitwise OR valuefunction 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 Codelet X = 7, Y = 11;possiblePair(X, Y);// This code is contributed by gfgking.</script> |
4 7
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
