![]() |
VOOZH | about |
Given two numbers a and b, the task is to swap them.
Examples:
Input: a = 2, b = 3
Output: 3 2Input: a = 20, b = 0
Output: 0 20Input: a = 10, b = 10
Output: 10 10
Table of Content
The idea is to use a third variable, say temp to store the original value of one of the variables during the swap.
20 10
The idea is to store sum in a variable so that we can get both values without third variable.
Let us understand with an example, a = 10, b = 20
20 10
The idea is to use the properties of XOR to swap the two variables.
Let us understand with an example, a = 10, b = 20
20 10
We can also swap using built-in functionalities like swap() function in C++, tuple unpacking in Python, destructuring assignment in JavaScript.
20 10