Given two integers **A** and **B** where **(A ≠ B)**. The task is to find **K** such that **|A – K| = |B – K|**. If no such **K** exists then print **-1**.

**Examples:**

Input:A = 2, B = 16

Output:9

|2 – 9| = |16 – 9| = 7

Input:A = 5, B = 2

Output:-1

**Approach:** It is given that **A ≠ B**. So let **A < B** then there are three cases:

**K < A:**This gives**A – K = B – K**which gives**A = B**which is false.**K > B:**This gives**K – A = K – B**which is also false.**A ≤ K ≤ B:**This gives**K – A = B – K**which gives**2 * K = A + B**

If **A + B** is odd, there is thus no solution. If **A + B** is even then the answer is **(A + B) / 2**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find k such that ` `// |a - k| = |b - k| ` `int` `find_k(` `int` `a, ` `int` `b) ` `{ ` ` ` `// If (a + b) is even ` ` ` `if` `((a + b) % 2 == 0) ` ` ` `return` `((a + b) / 2); ` ` ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 2, b = 16; ` ` ` ` ` `cout << find_k(a, b); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to find k such that ` `// |a - k| = |b - k| ` `static` `int` `find_k(` `int` `a, ` `int` `b) ` `{ ` ` ` `// If (a + b) is even ` ` ` `if` `((a + b) % ` `2` `== ` `0` `) ` ` ` `return` `((a + b) / ` `2` `); ` ` ` ` ` `return` `-` `1` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `a = ` `2` `, b = ` `16` `; ` ` ` ` ` `System.out.println(find_k(a, b)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to find k such that ` `# |a - k| = |b - k| ` `def` `find_k(a, b) : ` ` ` ` ` `# If (a + b) is even ` ` ` `if` `((a ` `+` `b) ` `%` `2` `=` `=` `0` `) : ` ` ` `return` `((a ` `+` `b) ` `/` `/` `2` `); ` ` ` ` ` `return` `-` `1` `; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `2` `; b ` `=` `16` `; ` ` ` ` ` `print` `(find_k(a, b)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find k such that ` `// |a - k| = |b - k| ` `static` `int` `find_k(` `int` `a, ` `int` `b) ` `{ ` ` ` `// If (a + b) is even ` ` ` `if` `((a + b) % 2 == 0) ` ` ` `return` `((a + b) / 2); ` ` ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `a = 2, b = 16; ` ` ` ` ` `Console.Write(find_k(a, b)); ` `} ` `} ` ` ` `// This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

**Output:**

9

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Program to find whether a no is power of two
- Program to find parity
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- Find Union and Intersection of two unsorted arrays
- To find sum of two numbers without using any operator
- Find day of the week for a given date
- Given a number, find the next smallest palindrome
- Find the element that appears once
- Find the largest multiple of 2, 3 and 5
- Program to find amount of water in a given glass
- Find the maximum distance covered using n bikes
- Find if two rectangles overlap
- Find the smallest number whose digits multiply to a given number n
- Find next greater number with same set of digits
- Find n'th number in a number system with only 3 and 4
- Find the missing number in Arithmetic Progression
- Find length of period in decimal value of 1/n
- Find number of days between two given dates
- Legendre's formula (Given p and n, find the largest x such that p^x divides n!)

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.