# Find the values of X and Y in the Given Equations

Given two numbers and . Find the values of **X** and **Y **in the equations.

- 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 = 8Output :X = 2, Y = 10Input :A = 12, B = 9Output :-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++

`// 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 Y ` `void` `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 Code ` `int` `main() ` `{ ` ` ` `int` `a = 12, b = 8; ` ` ` ` ` `findValues(a, b); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the values of ` `// X and Y using the given equations ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the ` `// values of X and Y ` `static` `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... ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the values of ` `# X and Y using the given equations ` ` ` `# Function to find the values of X and Y ` `def` `findValues(a, b): ` ` ` ` ` `# base condition ` ` ` `if` `((a ` `-` `b) ` `%` `2` `=` `=` `1` `): ` ` ` `print` `(` `"-1"` `); ` ` ` `return` `; ` ` ` ` ` `# required answer ` ` ` `print` `((a ` `-` `b) ` `/` `/` `2` `, (a ` `+` `b) ` `/` `/` `2` `); ` ` ` `# Driver Code ` `a ` `=` `12` `; b ` `=` `8` `; ` ` ` `findValues(a, b); ` ` ` `# This code is contributed ` `# by Akanksha Rai ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the values of ` `// X and Y using the given equations ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the ` `// values of X and Y ` `static` `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 Code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `a = 12, b = 8; ` ` ` `findValues(a, b); ` `} ` `} ` ` ` `// This code is contributed by @Tushil.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the values of ` `// X and Y using the given equations ` ` ` `// Function to find the values ` `// of X and Y ` `function` `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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2 10

## Recommended Posts:

- Find n-variables from n sum equations with one missing
- Find the repeating and the missing number using two equations
- Find 'N' number of solutions with the given inequality equations
- Find n positive integers that satisfy the given equations
- Program to find root of an equations using secant method
- Find minimum possible values of A, B and C when two of the (A + B), (A + C) and (B + C) are given
- Find smallest values of x and y such that ax - by = 0
- Find the minimum value of m that satisfies ax + by = m and all values after m also satisfy
- Find the sum of the ascii values of characters which are present at prime positions
- Linear Diophantine Equations
- Number of solutions to Modular Equations
- Gaussian Elimination to Solve Linear Equations
- Using Chinese Remainder Theorem to Combine Modular equations
- System of Linear Equations in three variables using Cramer's Rule
- Number of values of b such that a = b + (a^b)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.