📌  相关文章
📜  从它们的总和和XOR中找出两个数字|套装2

📅  最后修改于: 2021-06-28 22:20:26             🧑  作者: Mango

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

例子:

天真的方法:有关解决问题的最简单方法,请参阅本文的上一篇文章。

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

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

存在以下几种情况:

  • 如果X 在这种情况下,解决方案不存在,因为(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。如果为true,则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)