DEV Community

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

Collapse
 
m242 profile image
m242

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 ...

Collapse
 
theoutlander profile image
Nick Karnik

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.