Last Updated: 21 Nov, 2020

Swap Number Without Temporary Variable

Easy
Asked in companies
SAP LabsBNY MellonMakeMyTrip

Problem statement

Given two variables ‘X’ and ‘Y’. Your task is to swap the number without using a temporary variable or third variable.

Swap means the value of ‘X’ and ‘Y’ must be interchanged. Take an example ‘X’ is 10 and ‘Y’ is 20 so your function must return ‘X’ as a 20 and ‘Y’ as a 10.

Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.

Next ‘T’ lines contain two space-separated integers ‘X’ and ‘Y’ which represent the next ‘T’ test cases. 
Output Format
For each test case, return an array/vector that contains two integers ‘X’ and ‘Y’ with a swapped value.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 2*10^5
-10^9 <= X,Y <= 10^9

Where ‘T’ is the total number of test cases, ‘X’ and ‘Y’ denotes two given integer variables.

Time limit: 1 second

Approaches

01 Approach

  • Idea is to get the sum of given two-variable ‘X’ and ‘Y’ in an ‘X’ = ‘X’ + ‘Y’.
  • Value of swapped ‘Y’ can get by ‘X’ - ‘Y’ = (‘X’ + ‘Y’) - ‘Y’ = ‘X’
  • Value of swapped ‘X’ can get by ‘X’ - ‘Y’ = (‘X’ + ‘Y’) - ‘Y’ = (‘X’ + ‘Y’) - ‘X’ = ‘Y’
  • The current value of ‘Y’ is the initial value of ‘X’
  • Suppose ‘X’ is 10 and ‘Y’ is 20 then
    • ‘X’ = ‘X’ + ‘Y’ = 20+10’
    • Swapped value of ‘Y’ is ‘X’- ‘Y’ = 30-20 = 10’
    • And the swapped value of ‘X’ is ‘X’ - ‘Y’ = 30-10 = 20’
    • So ‘X’ is 20 and ‘Y’ is 10
  • Return swapped ‘X’ and ‘Y’.

02 Approach

  • Idea is to get the multiplication of given two-variable ‘X’ and ‘Y’ in an ‘X’ = ‘X’ * ‘Y’.
  • Value of swapped ‘Y’ can get by ‘X’ / ‘Y’ = ‘X’
  • Value of swapped ‘X’ can get by ‘X’ / ‘Y’ = ‘Y’
    • The current value of ‘Y’ is the initial value of ‘X’
  • Suppose ‘X’ is 3 and ‘Y’ is 4 then ‘X’ = ‘X’ * ‘Y’ = 4*3’ will be 12
    • Swapped value of ‘Y’ is ‘X’ / ‘Y’ = 12/4 = 3’
    • And the swapped value of ‘X’ is ‘X’ / ‘Y’ = 12/3 = 4’
    • So ‘X’ is 3 and ‘Y’ is 4
  • Return swapped ‘X’ and ‘Y’.
  • Note
    • The above approach will not work in case one of the numbers is ‘0’ because multiplication will be ‘0’

03 Approach

  • We use Bitwise XOR property
    • t^0=t
    • t^t=0
  • With help of these properties, we swap the number ‘X' and ‘Y’
    • ‘X' = ‘X' ^ ‘Y’
    • ‘Y’ = ‘X' ^ ‘Y’ this is the same as ‘Y’ = (‘X' ^ ‘Y’ ) ^ ‘Y’ = ‘X' ^ 0 = ‘X', after this step ‘Y’ contains the initial value of ‘X'.
    • ‘X' = ‘X' ^ ‘Y’ this is the same as ‘Y’ = (‘X' ^ ‘Y’) ^ ‘Y’ = (‘X' ^ ‘Y’) ^ ‘X' = ‘Y’, because the current value of ‘Y’ is ‘X'.
  • Suppose ‘X' is 10 and ‘Y’ is 5
    • ‘X' = ‘X' ^ ‘Y’ = 10^5 = 15’
    • ‘Y’ = ‘X' ^ ‘Y’ = (‘X' ^ ‘Y’) ^ ‘Y’ = ‘X' = 10’
    • ‘X' = ‘X' ^ ‘Y’ = (‘X' ^ ‘Y’) ^ ‘Y’ = (‘X' ^ ‘Y’) ^ ‘X' = ‘Y’ = 5’ there first ‘Y’ is the starting value of ‘Y’ and second ‘Y’ is swapped value of ‘Y’ with ‘X'.
    • Now the value of ‘X' is 5 and ‘Y’ is 10.
  • Return swapped ‘X' and ‘Y’.

04 Approach

  • We get the value of sum by this formula ‘X' = ('X'&'Y') + ('X' | ‘Y’) and subtraction by this formula ‘Y' = ‘X' + (~‘Y') +1’ it is same as ‘Y' = ‘X' - ‘Y'.
  • apply solution 1
  • For sum , ‘X' = (‘X' & ‘Y') + (‘X' | ‘Y')
  • For the swapped value of ‘Y' = ‘X' + (~‘Y') + 1 it will be the initial value of ‘X' and the swapped value of ‘Y'
  • For the swapped value of ‘X' = ‘X' + (~‘Y') + 1’ it will be the initial value of ‘Y' and the swapped value of ‘X'
  • Return ‘X' and ‘Y'