Open In App

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.






#include <iostream>
 
// Function to find pairs (i, j) such that i + j == N and A + i == B + j
std::pair<int, int> findPairs(int A, int B, int N) {
    for (int i = 1; i < N; i++) {
        for (int j = 1; j < N; j++) {
            if (i + j == N && A + i == B + j) {
                return std::make_pair(i, j);
            }
        }
    }
    return std::make_pair(-1, -1);
}
 
int main() {
    int A = 1;
    int B = 3;
    int N = 1;
 
    std::pair<int, int> result = findPairs(A, B, N);
 
    if (result.first != -1) {
        std::cout << "Pair found: (" << result.first << ", " << result.second << ")" << std::endl;
    } else {
        std::cout << -1 << std::endl;
    }
 
    return 0;
}




public class Main {
    // Function to find pairs (i, j) such that i + j == N and A + i == B + j
    public static int[] findPairs(int A, int B, int N) {
        for (int i = 1; i < N; i++) {
            for (int j = 1; j < N; j++) {
                if (i + j == N && A + i == B + j) {
                    return new int[]{i, j};
                }
            }
        }
        return new int[]{-1, -1};
    }
 
    public static void main(String[] args) {
        int A = 1;
        int B = 3;
        int N = 1;
 
        int[] result = findPairs(A, B, N);
 
        if (result[0] != -1) {
            System.out.println("Pair found: (" + result[0] + ", " + result[1] + ")");
        } else {
            System.out.println(-1);
        }
    }
}




# Full program in Python
 
def find_pairs(A, B, N):
    for i in range(1, N):
        for j in range(1, N):
            if i + j == N and A + i == B + j:
                return i, j
    return -1
 
A = 1
B = 3
N = 1
 
result = find_pairs(A, B, N)
print(result)




using System;
 
class Program
{
    // Function to find pairs (i, j) such that i + j == N and A + i == B + j
    static Tuple<int, int> FindPairs(int A, int B, int N)
    {
        for (int i = 1; i < N; i++)
        {
            for (int j = 1; j < N; j++)
            {
                if (i + j == N && A + i == B + j)
                {
                    return Tuple.Create(i, j);
                }
            }
        }
        return Tuple.Create(-1, -1);
    }
 
    static void Main()
    {
        int A = 1;
        int B = 3;
        int N = 1;
 
        Tuple<int, int> result = FindPairs(A, B, N);
 
        if (result.Item1 != -1)
        {
            Console.WriteLine($"Pair found: ({result.Item1}, {result.Item2})");
        }
        else
        {
            Console.WriteLine(-1);
        }
    }
}




function findPairs(A, B, N) {
    // Iterate over the range of 1 to N (exclusive) for both i and j
    for (let i = 1; i < N; i++) {
        for (let j = 1; j < N; j++) {
            // Check if the sum of i and j is equal to N and A + i is equal to B + j
            if (i + j === N && A + i === B + j) {
                // If the conditions are met, return the pair (i, j)
                return [i, j];
            }
        }
    }
    // If no such pairs are found, return an array with a single element -1
    return [-1];
}
 
const A = 1;
const B = 3;
const N = 1;
 
// Call the findPairs function and get the result
const result = findPairs(A, B, N);
 
// Print the result
 
console.log(result);

Output
-1




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++ 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 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 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# 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




<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)


Article Tags :