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 = 4Output:3 1Explanation:If X = 3 and Y = 1, then A + X = B + Y and X + Y =4

Input:A = 1, B = 3, N = 1Output:-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(N^{2}) **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 <bits/stdc++.h>` `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` |

## Javascript

`<script>` `// Javascript program to implement` `// the above approach` ` ` `// Function to calculate the` ` ` `// splitted numbers` ` ` `function` `findPair(A , B , N) {` ` ` `var` `X, Y;` ` ` `// Calculate X` ` ` `X = N - B + A;` ` ` `// If X is odd` ` ` `if` `(X % 2 != 0) {` ` ` `// No pair is possible` ` ` `document.write(` `"-1"` `);` ` ` `}` ` ` `// Otherwise` ` ` `else` `{` ` ` `// Calculate X` ` ` `X = X / 2;` ` ` `// Calculate Y` ` ` `Y = N - X;` ` ` `document.write(X + ` `" "` `+ Y);` ` ` `}` ` ` `}` ` ` `// Driver function` ` ` ` ` `var` `A = 1;` ` ` `var` `B = 3;` ` ` `var` `N = 4;` ` ` `findPair(A, B, N);` `// This code contributed by umadevi9616` `</script>` |

**Output:**

1 3

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

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**