Goldman Sachs interview question

write a function to swap integers not using third int?

Interview Answers

Anonymous

Mar 21, 2011

I believe this can be solved with simple subtraction: A = A + B B = A - B A = A - B For example, if A = 10 and B = 24: A = 10 + 24 = 34 B = 34 - 24 = 10 A = 34 - 10 = 24

7

Anonymous

Mar 19, 2011

IIRC, this can be done using three XOR operations: A = A xor B B = A xor B A = A xor B

1