# Split N into two integers whose addition to A and B makes them equal

Given three positive integers A, B and N, the task is to split N into two parts such that they become equal i.e. find two positive integers X and Y, such that X + Y = N and A + X = B + Y. If no such pair exists, print -1.

Examples:

Input: A = 1, B = 3, N = 4
Output: 3 1
Explanation: If X = 3 and Y = 1, then A + X = B + Y and X + Y =4

Input: A = 1, B = 3, N = 1
Output: -1

Naive Approach:
The simplest approach to solve this problem is to generate all possible pairs with sum N and check for each pair, if A + X = B + Y.

Time complexity: O(N2
Auxiliary Space: O(1)

Efficient Approach:
It can be observed that, since X + Y = N and A + X = B + Y, then X can be expressed as (N + B – A) / 2. Simply check if (N + B – A) / 2 is even or not. If it is even, calculate X and corresponding Y. Otherwise, print -1.

Below is the implementation of the above approach:

## C++

 `// C++ Program to implement ` `// the above approach ` `#include ` `using` `namespace` `std; `   `// Function to calculate the ` `// splitted numbers ` `void` `findPair(``int` `A, ``int` `B, ``int` `N) ` `{ ` `    ``int` `X, Y; `   `    ``// Calculate X ` `    ``X = N - B + A; `   `    ``// If X is odd ` `    ``if` `(X % 2 != 0) { `   `        ``// No pair is possible ` `        ``cout << ``"-1"``; ` `    ``} `   `    ``// Otherwise ` `    ``else` `{ `   `        ``// Calculate X ` `        ``X = X / 2; `   `        ``// Calculate Y ` `        ``Y = N - X; ` `        ``cout << X << ``" "` `<< Y; ` `    ``} ` `} `   `// Driver Code ` `int` `main() ` `{ ` `    ``int` `A = 1; ` `    ``int` `B = 3; ` `    ``int` `N = 4; ` `    ``findPair(A, B, N); ` `    ``return` `0; ` `} `

## Java

 `// Java program to implement ` `// the above approach ` `import` `java.util.*;`   `class` `GFG{` `    `  `// Function to calculate the ` `// splitted numbers ` `static` `void` `findPair(``int` `A, ``int` `B, ``int` `N) ` `{ ` `    ``int` `X, Y; `   `    ``// Calculate X ` `    ``X = N - B + A; `   `    ``// If X is odd ` `    ``if` `(X % ``2` `!= ``0``) ` `    ``{ `   `        ``// No pair is possible ` `        ``System.out.print(``"-1"``); ` `    ``} `   `    ``// Otherwise ` `    ``else` `    ``{ ` `        `  `        ``// Calculate X ` `        ``X = X / ``2``; `   `        ``// Calculate Y ` `        ``Y = N - X; ` `        ``System.out.print(X + ``" "` `+ Y); ` `    ``} ` `}`   `//Driver function` `public` `static` `void` `main (String[] args)` `{` `    ``int` `A = ``1``; ` `    ``int` `B = ``3``; ` `    ``int` `N = ``4``; ` `    `  `    ``findPair(A, B, N); ` `}` `}`   `// This code is contributed by offbeat`

## Python3

 `# Python3 program to implement ` `# the above approach `   `# Function to calculate the ` `# splitted numbers ` `def` `findPair(A, B, N): `   `    ``# Calculate X ` `    ``X ``=` `N ``-` `B ``+` `A `   `    ``# If X is odd ` `    ``if` `(X ``%` `2` `!``=` `0``): `   `        ``# No pair is possible ` `        ``print``(``"-1"``) ` `    `  `    ``# Otherwise ` `    ``else` `: `   `        ``# Calculate X ` `        ``X ``=` `X ``/``/` `2`   `        ``# Calculate Y ` `        ``Y ``=` `N ``-` `X ` `        ``print` `(X , Y) `   `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: `   `    ``A ``=` `1` `    ``B ``=` `3` `    ``N ``=` `4` `    `  `    ``findPair(A, B, N) `   `# This code is contributed by chitranayal `

## C#

 `// C# program to implement  ` `// the above approach  ` `using` `System; ` `  `  `class` `GFG{ ` `      `  `// Function to calculate the  ` `// splitted numbers  ` `static` `void` `findPair(``int` `A, ``int` `B, ``int` `N)  ` `{  ` `    ``int` `X, Y;  ` `  `  `    ``// Calculate X  ` `    ``X = N - B + A;  ` `  `  `    ``// If X is odd  ` `    ``if` `(X % 2 != 0)  ` `    ``{  ` `        `  `        ``// No pair is possible  ` `        ``Console.Write(``"-1"``);  ` `    ``}  ` `  `  `    ``// Otherwise  ` `    ``else` `    ``{  ` `          `  `        ``// Calculate X  ` `        ``X = X / 2;  ` `  `  `        ``// Calculate Y  ` `        ``Y = N - X;  ` `        ``Console.Write(X + ``" "` `+ Y);  ` `    ``}  ` `} ` `  `  `// Driver code` `public` `static` `void` `Main(``string``[] args) ` `{ ` `    ``int` `A = 1;  ` `    ``int` `B = 3;  ` `    ``int` `N = 4;  ` `      `  `    ``findPair(A, B, N);  ` `} ` `} `   `// This code is contributed by rutvik_56`

Output:

```1 3

```

Time Complexity: O(1)
Auxiliary Space: O(1)

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : chitranayal, offbeat, rutvik_56