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:

  • Does Background Music Matter to Speech in Language Models
  • Measuring the Checker
  • Mastermind
  • Resillience
  • Multi-Head Attention