Java Program to Swap Two Numbers within Using Third Variable #java #coding #interview

preview_player
Показать описание
Java Program to Swap Two Numbers within Using Third Variable
Рекомендации по теме
Комментарии
Автор

Amazing!!!

Max(a, b) = sum(a, b) / 2
Min(a, b) = difference(a, b) / 2

These formulas will always be constants if we consider their absolute value, and we can use them to go from A to B and vice-versa.

1st line: You store the sum in A(new A)
2nd line: You turn B into old A by subtracting B from the stored sum(new A).
3rd line: You turn new A into old B by subtracting new B(*old A*) from the stored sum.

AurelioCunha
Автор

Brilliant but I will still walk out of the interview if asked this question 😂

aribasiebel
Автор

Let me fix this

a = (a + b) - (b=a)

khudkikhoz
Автор

Wait a sec... Would this work with any other number?

conduitstruct
visit shbcf.ru