DEV Community

Discussion on: How can you swap two variables without using a third?

m242 profile image

if (y > x){
x += (y-x)/2;
y = 2*(y-x)
x += x-y

else if (y < x){
y += (y-x)/2;
x = 2*(y-x)
y += x-y


Probably not the best way ...

theoutlander profile image
Nick Karnik Author

You might want to try it in a language of your choice. If it works, it's a very good first step. Then, you can think of optimizing it.