Find two numbers from their sum and XOR | Set 2 | 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 ; Prthe numbers A and B ; Driver Code ; Given Sum and XOR of 2 numbers ; Function Call
def findNums ( X , Y ) : NEW_LINE INDENT A = 0 ; NEW_LINE B = 0 ; NEW_LINE if ( X < Y ) : NEW_LINE INDENT A = - 1 ; NEW_LINE B = - 1 ; NEW_LINE DEDENT elif ( ( ( abs ( X - Y ) ) & 1 ) != 0 ) : NEW_LINE INDENT A = - 1 ; NEW_LINE B = - 1 ; NEW_LINE DEDENT elif ( X == Y ) : NEW_LINE INDENT A = 0 ; NEW_LINE B = Y ; NEW_LINE DEDENT else : NEW_LINE INDENT A = ( X - Y ) // 2 ; NEW_LINE if ( ( A & Y ) == 0 ) : NEW_LINE INDENT B = ( A + Y ) ; NEW_LINE DEDENT else : NEW_LINE INDENT A = - 1 ; NEW_LINE B = - 1 ; NEW_LINE DEDENT DEDENT print A ; NEW_LINE print B ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 17 ; NEW_LINE Y = 13 ; NEW_LINE findNums ( X , Y ) ; NEW_LINE DEDENT