📌  相关文章
📜  将N拆分为两个整数,它们与A和B的加法使它们相等

📅  最后修改于: 2021-04-23 21:45:25             🧑  作者: Mango

给定三个正整数ABN ,任务是将N分为两部分,使它们相等,即找到两个正整数XY ,使得X + Y = NA + X = B +Y 。如果不存在这样的对,则打印-1

例子:

天真的方法:
解决此问题的最简单方法是生成所有可能的对,总和为N,并检查每个对,如果A + X = B + Y。

时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:
可以看到,由于X + Y = NA + X = B + Y ,则X可以表示为(N + B – A)/ 2 。只需检查(N + B – A)/ 2是否为偶数即可。如果是偶数,则计算X和相应的Y。否则,打印-1

下面是上述方法的实现:

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


Javascript


输出:
1 3

时间复杂度: O(1)
辅助空间: O(1)