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

📅  最后修改于: 2021-09-04 11:48:53             🧑  作者: 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)