📌  相关文章
📜  从它们的总和和异或中找到两个数字 | 2套

📅  最后修改于: 2021-09-08 14:53:48             🧑  作者: Mango

给定两个整数XY ,任务是找到总和X和按位异或等于Y的两个整数。

例子:

朴素的方法:请参阅本文的前一篇文章,了解解决问题的最简单方法。

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

高效的方法:上述方法可以基于以下观察进行优化:

存在以下情况:

  • 如果 X < Y:在这种情况下,解不存在,因为 (A & B) 变为负数,这是不可能的。
  • 如果 X – Y 是奇数:在这种情况下,解不存在,因为 (X – Y) 不能被 2 整除。
  • 如果 X = Y:在这种情况下,A & B = 0。因此,A 的最小值应为 0,B 的值应为 Y,以满足给定的方程。
  • 否则:仅当 ((X – Y)/2) & Y 等于 0 时,才满足 A&B = (X – Y)/2。如果为真,则 A = (X – Y)/2 且 B = A + Y。否则,A = -1 且 B = -1。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the value of A and
// B whose sum is X and xor is Y
void findNums(int X, int Y)
{
 
    // Initialize the two numbers
    int A, B;
 
    // Case 1: X < Y
    if (X < Y) {
        A = -1;
        B = -1;
    }
 
    // Case 2: X-Y is odd
    else if (abs(X - Y) & 1) {
        A = -1;
        B = -1;
    }
 
    // Case 3: If both Sum and XOR
    // are equal
    else if (X == Y) {
        A = 0;
        B = Y;
    }
 
    // Case 4: If above cases fails
    else {
 
        // Update the value of A
        A = (X - Y) / 2;
 
        // Check if A & Y value is 0
        if ((A & Y) == 0) {
 
            // If true, update B
            B = (A + Y);
        }
 
        // Otherwise assign -1 to A,
        // -1 to B
        else {
            A = -1;
            B = -1;
        }
    }
 
    // Print the numbers A and B
    cout << A << " " << B;
}
 
// Driver Code
int main()
{
    // Given Sum and XOR of 2 numbers
    int X = 17, Y = 13;
 
    // Function Call
    findNums(X, Y);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
    
class GFG{
    
// Function to find the value of A and
// B whose sum is X and xor is Y
static void findNums(int X, int Y)
{
     
    // Initialize the two numbers
    int A, B;
  
    // Case 1: X < Y
    if (X < Y)
    {
        A = -1;
        B = -1;
    }
  
    // Case 2: X-Y is odd
    else if (((Math.abs(X - Y)) & 1) != 0)
    {
        A = -1;
        B = -1;
    }
  
    // Case 3: If both Sum and XOR
    // are equal
    else if (X == Y)
    {
        A = 0;
        B = Y;
    }
  
    // Case 4: If above cases fails
    else
    {
         
        // Update the value of A
        A = (X - Y) / 2;
  
        // Check if A & Y value is 0
        if ((A & Y) == 0)
        {
             
            // If true, update B
            B = (A + Y);
        }
  
        // Otherwise assign -1 to A,
        // -1 to B
        else
        {
            A = -1;
            B = -1;
        }
    }
  
    // Print the numbers A and B
    System.out.print(A + " " + B);
}
    
// Driver Code
public static void main(String[] args)
{
     
    // Given Sum and XOR of 2 numbers
    int X = 17, Y = 13;
  
    // Function Call
    findNums(X, Y);
}
}
 
// This code is contributed by susmitakundugoaldanga


Python
# Python program for the above approach
 
# Function to find the value of A and
# B whose sum is X and xor is Y
def findNums(X, Y):
   
    # Initialize the two numbers
    A = 0;
    B = 0;
 
    # Case 1: X < Y
    if (X < Y):
        A = -1;
        B = -1;
 
    # Case 2: X-Y is odd
    elif (((abs(X - Y)) & 1) != 0):
        A = -1;
        B = -1;
 
    # Case 3: If both Sum and XOR
    # are equal
    elif (X == Y):
        A = 0;
        B = Y;
 
    # Case 4: If above cases fails
    else:
 
        # Update the value of A
        A = (X - Y) // 2;
 
        # Check if A & Y value is 0
        if ((A & Y) == 0):
 
            # If True, update B
            B = (A + Y);
 
        # Otherwise assign -1 to A,
        # -1 to B
        else:
            A = -1;
            B = -1;
 
    # Prthe numbers A and B
    print A;
    print B;
 
# Driver Code
if __name__ == '__main__':
   
    # Given Sum and XOR of 2 numbers
    X = 17;
    Y = 13;
 
    # Function Call
    findNums(X, Y);
 
# This code is contributed by shikhasingrajput


C#
// C# program for the above approach
using System;
 
class GFG{
    
// Function to find the value of A and
// B whose sum is X and xor is Y
static void findNums(int X, int Y)
{
     
    // Initialize the two numbers
    int A, B;
  
    // Case 1: X < Y
    if (X < Y)
    {
        A = -1;
        B = -1;
    }
  
    // Case 2: X-Y is odd
    else if (((Math.Abs(X - Y)) & 1) != 0)
    {
        A = -1;
        B = -1;
    }
  
    // Case 3: If both Sum and XOR
    // are equal
    else if (X == Y)
    {
        A = 0;
        B = Y;
    }
  
    // Case 4: If above cases fails
    else
    {
         
        // Update the value of A
        A = (X - Y) / 2;
  
        // Check if A & Y value is 0
        if ((A & Y) == 0)
        {
             
            // If true, update B
            B = (A + Y);
        }
  
        // Otherwise assign -1 to A,
        // -1 to B
        else
        {
            A = -1;
            B = -1;
        }
    }
  
    // Print the numbers A and B
    Console.Write(A + " " + B);
}
    
// Driver Code
public static void Main(String[] args)
{
     
    // Given Sum and XOR of 2 numbers
    int X = 17, Y = 13;
     
    // Function Call
    findNums(X, Y);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
2 15

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程