Sunday, December 29, 2024
Google search engine
HomeData Modelling & AIFind K such that |A – K| = |B – K|

Find K such that |A – K| = |B – K|

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:
|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;
}


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


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


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


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to find k such that
// |a - k| = |b - k|
function find_k(a, b)
{
     
    // If (a + b) is even
    if ((a + b) % 2 == 0)
        return ((a + b) / 2);
 
    return -1;
}
 
// Driver code
var a = 2, b = 16;
 
document.write( find_k(a, b));
 
// This code is contributed by akshitsaxenaa09
 
</script>


Output

9

Time Complexity: O(1)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Last Updated :
07 Mar, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

RELATED ARTICLES

Most Popular

Recent Comments