Find the values of X and Y in the Given Equations - GeeksforGeeks

15 Apr, 2021
Given two numbers and . Find the values of X and Y in the equations.  
A = X + YB = X xor Y
A = X + Y
B = X xor Y
The task is to make X as minimum as possible. If it is not possible to find any valid values for X and Y then print -1.Examples:  
Input : A = 12, B = 8
Output : X = 2, Y = 10

Input : A = 12, B = 9
Output : -1
 
Let’s take a look at some bit in X, which is equal to 1. If the respective bit in Y is equal to 0, then one can swap these two bits, thus reducing X and increasing Y without changing their sum and xor. We can conclude that if some bit in X is equal to 1 then the respective bit in Y is also equal to 1. Thus, Y = X + B. Taking into account that X + Y = X + X + B = A, one can obtain the following formulas for finding X and Y: 
X = (A – B) / 2
Y = X + B = (A + B) / 2
One should also notice that if A < B or A and B have different parity, then the answer doesn’t exist and output is -1. If X and (A – X) not equal to X then the answer is also -1.Below is the implementation of the above approach : 
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find the values of// X and Y using the given equations #include <bits/stdc++.h>using namespace std; // Function to find the// values of X and Yvoid findValues(int a, int b){    // base condition    if ((a - b) % 2 == 1) {        cout << "-1";        return;    }     // required answer    cout << (a - b) / 2 << " " << (a + b) / 2;} // Driver Codeint main(){    int a = 12, b = 8;     findValues(a, b);     return 0;}
// Java program to find the values of// X and Y using the given equationsimport java.io.*; class GFG{     // Function to find the// values of X and Ystatic void findValues(int a, int b){    // base condition    if ((a - b) % 2 == 1)    {            System.out.println ("-1");        return;    }     // required answer    System.out.println (((a - b) / 2)+ " " +                            ((a + b) / 2));}     // Driver Code    public static void main (String[] args)    {        int a = 12, b = 8;        findValues(a, b);    }} // This code is contributed by ajit...
# Python3 program to find the values of# X and Y using the given equations # Function to find the values of X and Ydef findValues(a, b):     # base condition    if ((a - b) % 2 == 1):        print("-1");        return;     # required answer    print((a - b) // 2, (a + b) // 2); # Driver Codea = 12; b = 8; findValues(a, b); # This code is contributed# by Akanksha Rai
// C# program to find the values of// X and Y using the given equationsusing System; class GFG{         // Function to find the// values of X and Ystatic void findValues(int a, int b){    // base condition    if ((a - b) % 2 == 1)    {            Console.Write ("-1");        return;    }     // required answer    Console.WriteLine(((a - b) / 2)+ " " +                        ((a + b) / 2));} // Driver Codestatic public void Main (){    int a = 12, b = 8;    findValues(a, b);}} // This code is contributed by @Tushil..
<?php// PHP program to find the values of// X and Y using the given equations // Function to find the values// of X and Yfunction findValues($a, $b){    // base condition    if (($a - $b) % 2 == 1)    {        echo "-1";        return;    }     // required answer    echo ($a - $b) / 2, " " ,         ($a + $b) / 2;} // Driver Code$a = 12;$b = 8;findValues($a, $b); // This code is contributed by jit_t?>
<script> // Javascript program to find the values of// X and Y using the given equations // Function to find the values// of X and Yfunction findValues(a, b){    // base condition    if ((a - b) % 2 == 1)    {        document.write( "-1");        return;    }     // required answer    document.write( (a - b) / 2+ " " +        (a + b) / 2);} // Driver Codelet a = 12;let b = 8;findValues(a, b); // This code is contributed// by bobby </script>
2 10
 
jit_t
Akanksha_Rai
gottumukkalabobby
math
Bit Magic
Competitive Programming
Mathematical
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Highest power of 2 less than or equal to given number
Swap bits in a given number
Write an Efficient Method to Check if a Number is Multiple of 3
Check for Integer Overflow
Competitive Programming - A Complete Guide
Practice for cracking any coding interview
Arrow operator -> in C/C++ with Examples
Prefix Sum Array - Implementation and Applications in Competitive Programming
Top 10 Algorithms and Data Structures for Competitive Programming