Find the triplet from given Bitwise XOR and Bitwise AND values of all its pairs
Given six positive integers representing the Bitwise XOR and Bitwise AND of all possible pairs of a triplet (a, b, c), the task is to find the triplet.
Examples:
Input: aXORb = 30, aANDb = 0, aXORc = 10, aANDc = 20, aXORb = 20, aANDb = 10
Output: a = 10, b = 20, c= 30
Explanation:
If a = 10, b = 20, c= 30
a ^ b = 30, a & b = 0
a ^ c = 10, a & c = 20
a ^ b = 20, a & b = 10
Therefore, the required output is (a, b, c) = (10, 20, 30).
Input: aXORb = 3, aANDb = 0, aXORc = 2, aANDc = 1, aXORb = 1, aANDb = 2
Output: a = 1, b = 2, c = 3
Approach: The idea is to find the sum of every possible pairs of triplet using their Bitwise XOR and Bitwise AND values based on the following observations:
a + b = a ^ b + 2 * (a & b)
Follow the steps below to solve the problem:
- Find the sum of every possible pair of triplets i.e, (a + b, b + c, c + a) using the above formula.
- The value of a can be calculated as ((a + b) + (a + c) – (b + c)) / 2.
- The value of b can be calculated as ((a + b) – a).
- The value of c can be calculated as ((b + c) – b).
- Finally, print the value of the triplet (a, b, c).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findNumbers( int aXORb, int aANDb, int aXORc, int aANDc,
int bXORc, int bANDc)
{
int a, b, c;
int aSUMb;
int aSUMc;
int bSUMc;
aSUMb = aXORb + aANDb * 2;
aSUMc = aXORc + aANDc * 2;
bSUMc = bXORc + bANDc * 2;
a = (aSUMb - bSUMc + aSUMc) / 2;
b = aSUMb - a;
c = aSUMc - a;
cout << "a = " << a;
cout << ", b = " << b;
cout << ", c = " << c;
}
int main()
{
int aXORb = 30, aANDb = 0, aXORc = 20, aANDc = 10,
bXORc = 10, bANDc = 20;
findNumbers(aXORb, aANDb, aXORc, aANDc, bXORc, bANDc);
}
|
Java
class GFG{
static void findNumbers( int aXORb, int aANDb,
int aXORc, int aANDc,
int bXORc, int bANDc)
{
int a, b, c;
int aSUMb;
int aSUMc;
int bSUMc;
aSUMb = aXORb + aANDb * 2 ;
aSUMc = aXORc + aANDc * 2 ;
bSUMc = bXORc + bANDc * 2 ;
a = (aSUMb - bSUMc + aSUMc) / 2 ;
b = aSUMb - a;
c = aSUMc - a;
System.out.print( "a = " + a);
System.out.print( ", b = " + b);
System.out.print( ", c = " + c);
}
public static void main(String[] args)
{
int aXORb = 30 , aANDb = 0 ,
aXORc = 20 , aANDc = 10 ,
bXORc = 10 , bANDc = 20 ;
findNumbers(aXORb, aANDb, aXORc,
aANDc, bXORc, bANDc);
}
}
|
Python3
def findNumbers(aXORb, aANDb, aXORc, aANDc, bXORc, bANDc):
a, b, c = 0 , 0 , 0 ;
aSUMb = 0 ;
aSUMc = 0 ;
bSUMc = 0 ;
aSUMb = aXORb + aANDb * 2 ;
aSUMc = aXORc + aANDc * 2 ;
bSUMc = bXORc + bANDc * 2 ;
a = (aSUMb - bSUMc + aSUMc) / / 2 ;
b = aSUMb - a;
c = aSUMc - a;
print ( "a = " , a, end = "");
print ( ", b = " , b, end = "");
print ( ", c = " , c, end = "");
if __name__ = = '__main__' :
aXORb = 30 ; aANDb = 0 ; aXORc = 20 ; aANDc = 10 ; bXORc = 10 ; bANDc = 20 ;
findNumbers(aXORb, aANDb, aXORc, aANDc, bXORc, bANDc);
|
C#
using System;
public class GFG
{
static void findNumbers( int aXORb, int aANDb,
int aXORc, int aANDc,
int bXORc, int bANDc)
{
int a, b, c;
int aSUMb;
int aSUMc;
int bSUMc;
aSUMb = aXORb + aANDb * 2;
aSUMc = aXORc + aANDc * 2;
bSUMc = bXORc + bANDc * 2;
a = (aSUMb - bSUMc + aSUMc) / 2;
b = aSUMb - a;
c = aSUMc - a;
System.Console.Write( "a = " + a);
System.Console.Write( ", b = " + b);
System.Console.Write( ", c = " + c);
}
static public void Main ()
{
int aXORb = 30, aANDb = 0,
aXORc = 20, aANDc = 10,
bXORc = 10, bANDc = 20;
findNumbers(aXORb, aANDb, aXORc,
aANDc, bXORc, bANDc);
}
}
|
Javascript
<script>
function findNumbers(aXORb, aANDb, aXORc, aANDc, bXORc, bANDc)
{
let a, b, c;
let aSUMb;
let aSUMc;
let bSUMc;
aSUMb = aXORb + aANDb * 2;
aSUMc = aXORc + aANDc * 2;
bSUMc = bXORc + bANDc * 2;
a = Math.floor((aSUMb - bSUMc + aSUMc) / 2);
b = aSUMb - a;
c = aSUMc - a;
document.write( "a = " + a);
document.write( ", b = " + b);
document.write( ", c = " + c);
}
let aXORb = 30, aANDb = 0, aXORc = 20, aANDc = 10,
bXORc = 10, bANDc = 20;
findNumbers(aXORb, aANDb, aXORc, aANDc, bXORc, bANDc);
</script>
|
Output:
a = 10, b = 20, c = 30
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
10 Mar, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...