How to swap two variables without extra space

Let’s say, we have two variables, A and B, and our task is swapping these two variables without extra space.

First, we can this:

A = A + B

and,

B = A – B = A + B – B = A (import A from Step 1)

finally,

A = A – B = A + B – A = B (import A, B from Step 1 and 2 respectively)

That’s it, Dude!




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Resillience
  • Multi-Head Attention
  • Preference Alignment 101
  • Challenges in Code Generation
  • PREDICTING AND OPTIMIZING LLVM COMPILER PASS ORDER