Dados seis enteros positivos que representan Bitwise XOR y Bitwise AND de todos los pares posibles de un triplete (a, b, c) , la tarea es encontrar el triplete.
Ejemplos:
Entrada: aXORb = 30, aANDb = 0, aXORc = 10, aANDc = 20, aXORb = 20, aANDb = 10
Salida: a = 10, b = 20, c= 30
Explicación:
Si a = 10, b = 20, c = 30
a ^ b = 30, a & b = 0
a ^ c = 10, a & c = 20
a ^ b = 20, a & b = 10
Por lo tanto, la salida requerida es (a, b, c) = ( 10, 20, 30).Entrada: aXORb = 3, aANDb = 0, aXORc = 2, aANDc = 1, aXORb = 1, aANDb = 2
Salida: a = 1, b = 2, c = 3
Enfoque: la idea es encontrar la suma de todos los pares posibles de triplete utilizando sus valores Bitwise XOR y Bitwise AND en función de las siguientes observaciones:
a + b = a ^ b + 2 * (a y b)
Siga los pasos a continuación para resolver el problema:
- Encuentre la suma de cada posible par de tripletes, es decir, (a + b, b + c, c + a) usando la fórmula anterior.
- El valor de a se puede calcular como ((a + b) + (a + c) – (b + c)) / 2 .
- El valor de b se puede calcular como ((a + b) – a) .
- El valor de c se puede calcular como ((b + c) – b) .
- Finalmente, imprima el valor del triplete (a, b, c) .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the triplet with given // Bitwise XOR and Bitwise AND values of all // possible pairs of the triplet void findNumbers(int aXORb, int aANDb, int aXORc, int aANDc, int bXORc, int bANDc) { // Stores values of // a triplet int a, b, c; // Stores a + b int aSUMb; // Stores a + c int aSUMc; // Stores b + c int bSUMc; // Calculate aSUMb aSUMb = aXORb + aANDb * 2; // Calculate aSUMc aSUMc = aXORc + aANDc * 2; // Calculate bSUMc bSUMc = bXORc + bANDc * 2; // Calculate a a = (aSUMb - bSUMc + aSUMc) / 2; // Calculate b b = aSUMb - a; // Calculate c c = aSUMc - a; // Print a cout << "a = " << a; // Print b cout << ", b = " << b; // Print c cout << ", c = " << c; } // Driver Code int main() { int aXORb = 30, aANDb = 0, aXORc = 20, aANDc = 10, bXORc = 10, bANDc = 20; findNumbers(aXORb, aANDb, aXORc, aANDc, bXORc, bANDc); }
Java
// Java program to implement // the above approach class GFG{ // Function to find the triplet with given // Bitwise XOR and Bitwise AND values of all // possible pairs of the triplet static void findNumbers(int aXORb, int aANDb, int aXORc, int aANDc, int bXORc, int bANDc) { // Stores values of // a triplet int a, b, c; // Stores a + b int aSUMb; // Stores a + c int aSUMc; // Stores b + c int bSUMc; // Calculate aSUMb aSUMb = aXORb + aANDb * 2; // Calculate aSUMc aSUMc = aXORc + aANDc * 2; // Calculate bSUMc bSUMc = bXORc + bANDc * 2; // Calculate a a = (aSUMb - bSUMc + aSUMc) / 2; // Calculate b b = aSUMb - a; // Calculate c c = aSUMc - a; // Print a System.out.print("a = " + a); // Print b System.out.print(", b = " + b); // Print c System.out.print(", c = " + c); } // Driver Code 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); } } // This code is contributed by 29AjayKumar
Python3
# Python program to implement # the above approach # Function to find the triplet with given # Bitwise XOR and Bitwise AND values of all # possible pairs of the triplet def findNumbers(aXORb, aANDb, aXORc, aANDc, bXORc, bANDc): # Stores values of # a triplet a, b, c = 0, 0, 0; # Stores a + b aSUMb = 0; # Stores a + c aSUMc = 0; # Stores b + c bSUMc = 0; # Calculate aSUMb aSUMb = aXORb + aANDb * 2; # Calculate aSUMc aSUMc = aXORc + aANDc * 2; # Calculate bSUMc bSUMc = bXORc + bANDc * 2; # Calculate a a = (aSUMb - bSUMc + aSUMc) // 2; # Calculate b b = aSUMb - a; # Calculate c c = aSUMc - a; # Pra print("a = " , a, end = ""); # Prb print(", b = " , b, end = ""); # Prc print(", c = " , c, end = ""); # Driver Code if __name__ == '__main__': aXORb = 30; aANDb = 0; aXORc = 20; aANDc = 10; bXORc = 10; bANDc = 20; findNumbers(aXORb, aANDb, aXORc, aANDc, bXORc, bANDc); # This code contributed by shikhasingrajput
C#
// C# code for above approach using System; public class GFG { // Function to find the triplet with given // Bitwise XOR and Bitwise AND values of all // possible pairs of the triplet static void findNumbers(int aXORb, int aANDb, int aXORc, int aANDc, int bXORc, int bANDc) { // Stores values of // a triplet int a, b, c; // Stores a + b int aSUMb; // Stores a + c int aSUMc; // Stores b + c int bSUMc; // Calculate aSUMb aSUMb = aXORb + aANDb * 2; // Calculate aSUMc aSUMc = aXORc + aANDc * 2; // Calculate bSUMc bSUMc = bXORc + bANDc * 2; // Calculate a a = (aSUMb - bSUMc + aSUMc) / 2; // Calculate b b = aSUMb - a; // Calculate c c = aSUMc - a; // Print a System.Console.Write("a = " + a); // Print b System.Console.Write(", b = " + b); // Print c System.Console.Write(", c = " + c); } // Driver code static public void Main () { int aXORb = 30, aANDb = 0, aXORc = 20, aANDc = 10, bXORc = 10, bANDc = 20; findNumbers(aXORb, aANDb, aXORc, aANDc, bXORc, bANDc); } } // This code is contributed by offbeat.
Javascript
<script> // JavaScript program to implement // the above approach // Function to find the triplet with given // Bitwise XOR and Bitwise AND values of all // possible pairs of the triplet function findNumbers(aXORb, aANDb, aXORc, aANDc, bXORc, bANDc) { // Stores values of // a triplet let a, b, c; // Stores a + b let aSUMb; // Stores a + c let aSUMc; // Stores b + c let bSUMc; // Calculate aSUMb aSUMb = aXORb + aANDb * 2; // Calculate aSUMc aSUMc = aXORc + aANDc * 2; // Calculate bSUMc bSUMc = bXORc + bANDc * 2; // Calculate a a = Math.floor((aSUMb - bSUMc + aSUMc) / 2); // Calculate b b = aSUMb - a; // Calculate c c = aSUMc - a; // Print a document.write("a = " + a); // Print b document.write(", b = " + b); // Print c document.write(", c = " + c); } // Driver Code let aXORb = 30, aANDb = 0, aXORc = 20, aANDc = 10, bXORc = 10, bANDc = 20; findNumbers(aXORb, aANDb, aXORc, aANDc, bXORc, bANDc); // This code is contributed by Surbhi Tyagi. </script>
a = 10, b = 20, c = 30
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)