Skip to content
Onsite

Swap Two Variables Without a Third

EasyP2Coding

Return [b, a], the two values swapped, without using a third temporary variable. The point of the exercise is the swap technique: use arithmetic on the two variables instead of a spare variable or a tuple trick.

Example 1

Input
a = 3, b = 7
Output
[7,3]

Example 2

Input
a = 0, b = 0
Output
[0,0]

Example 3

Input
a = 5, b = 5
Output
[5,5]

Constraints

  • -10^9 <= a, b <= 10^9

Hints

Run checks the 3 visible tests, Submit all 8.