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