Find two numbers from their sum and OR
Last Updated :
12 Aug, 2021
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 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
// 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.
Python3
# 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#
//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
JavaScript
<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>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Find two numbers from their sum and XOR Given the sum and xor of two numbers X and Y s.t. sum and xor \in [0, 2^{64}-1] , 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
7 min read
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 = 13Output: 2 15Explanation: 2 + 15 = 17 and 2 ^ 15 = 13 Input: X = 1870807699, Y = 259801747Output: 805502976 1065304723 Naive Approach: Refer to the previous post of
8 min read
Find two numbers whose sum and GCD are given Given sum and gcd of two numbers a and b . The task is to find both the numbers a and b. If the numbers do not exist then print -1 .Examples: Input: sum = 6, gcd = 2 Output: a = 4, b = 2 4 + 2 = 6 and GCD(4, 2) = 2Input: sum = 7, gcd = 2 Output: -1 There are no such numbers whose sum is 7 and GCD is
5 min read
Sum of the multiples of two numbers below N Given three integer A, B and N. The task is to find the sum of all the elements below N which are multiples of either A or B. Examples: Input: N = 10, A = 3, B = 5 Output: 23 3, 5, 6 and 9 are the only numbers below 10 which are multiples of either 3 or 5 Input: N = 50, A = 8, B = 15 Output: 258 Nai
12 min read
Find the number of solutions to the given equation Given three integers A, B and C, the task is to find the count of values of X such that the following condition is satisfied, X = B * Sm(X)A + C where Sm(X) denotes the sum of digits of X and 1 < X < 109.Examples: Input: A = 3, B = 2, C = 8 Output: 3 For X = 10, 2 * (1)3 + 8 = 10 For X = 2008,
6 min read