Given two variables, x, and y, swap two variables without using a third variable.

Method 1 (Using Arithmetic Operators)
The idea is to get a sum in one of the two given numbers. The numbers can then be swapped using the sum and subtraction from the sum.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x = 10, y = 5;
x = x + y;
y = x - y;
x = x - y;
cout << "After Swapping: x =" << x << ", y=" << y;
}
|
C
#include <stdio.h>
int main()
{
int x = 10, y = 5;
x = x + y;
y = x - y;
x = x - y;
printf ( "After Swapping: x = %d, y = %d" , x, y);
return 0;
}
|
Java
import java.io.*;
class Geeks {
public static void main(String a[])
{
int x = 10 ;
int y = 5 ;
x = x + y;
y = x - y;
x = x - y;
System.out.println( "After swapping:"
+ " x = " + x + ", y = " + y);
}
}
|
Python3
x = 10
y = 5
x = x + y
y = x - y
x = x - y
print ( "After Swapping: x =" , x, " y =" , y)
|
C#
using System;
class GFG {
public static void Main()
{
int x = 10;
int y = 5;
x = x + y;
y = x - y;
x = x - y;
Console.WriteLine( "After swapping: x = " + x
+ ", y = " + y);
}
}
|
PHP
<?php
$x = 10; $y = 5;
$x = $x + $y ;
$y = $x - $y ;
$x = $x - $y ;
echo "After Swapping: x = " ,
$x , ", " , "y = " , $y ;
?>
|
Javascript
<script>
let x = 10, y = 5;
x = x + y;
y = x - y;
x = x - y;
document.write( "After Swapping: x =" + x + ", y=" + y);
</script>
|
OutputAfter Swapping: x =5, y=10
Time Complexity: O(1).
Auxiliary Space: O(1).
Multiplication and division can also be used for swapping.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x = 10, y = 5;
x = x * y;
y = x / y;
x = x / y;
cout << "After Swapping: x =" << x << ", y=" << y;
}
|
C
#include <stdio.h>
int main()
{
int x = 10, y = 5;
x = x * y;
y = x / y;
x = x / y;
printf ( "After Swapping: x = %d, y = %d" , x, y);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int x = 10 ;
int y = 5 ;
x = x * y;
y = x / y;
x = x / y;
System.out.println( "After swapping:"
+ " x = " + x + ", y = " + y);
}
}
|
Python3
x = 10
y = 5
x = x * y
y = x / / y;
x = x / / y;
print ( "After Swapping: x =" ,
x, " y =" , y);
|
C#
using System;
class GFG {
static public void Main()
{
int x = 10;
int y = 5;
x = x * y;
y = x / y;
x = x / y;
Console.WriteLine( "After swapping:"
+ " x = " + x + ", y = " + y);
}
}
|
PHP
<?php
$x = 10;
$y = 5;
$x = $x * $y ;
$y = $x / $y ;
$x = $x / $y ;
echo "After Swapping: x = " , $x ,
" " , "y = " , $y ;
?>
|
Javascript
<script>
var x = 10;
var y = 5;
x = x * y;
y = x / y;
x = x / y;
document.write( "After swapping:" + " x = " +
x + ", y = " + y);
</script>
|
OutputAfter Swapping: x =5, y=10
Time Complexity: O(1).
Auxiliary Space: O(1).
Method 2 (Using Bitwise XOR)
The bitwise XOR operator can be used to swap two variables. The XOR of two numbers x and y returns a number that has all the bits as 1 wherever bits of x and y differ. For example, XOR of 10 (In Binary 1010) and 5 (In Binary 0101) is 1111, and XOR of 7 (0111) and 5 (0101) is (0010).
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x = 10, y = 5;
x = x ^ y;
y = x ^ y;
x = x ^ y;
cout << "After Swapping: x =" << x << ", y=" << y;
return 0;
}
|
C
#include <stdio.h>
int main()
{
int x = 10, y = 5;
x = x ^ y;
y = x ^ y;
x = x ^ y;
printf ( "After Swapping: x = %d, y = %d" , x, y);
return 0;
}
|
Java
import java.io.*;
public class GFG {
public static void main(String a[])
{
int x = 10 ;
int y = 5 ;
x = x ^ y;
y = x ^ y;
x = x ^ y;
System.out.println( "After swap: x = "
+ x + ", y = " + y);
}
}
|
Python3
x = 10
y = 5
x = x ^ y;
y = x ^ y;
x = x ^ y;
print ( "After Swapping: x = " , x, " y =" , y)
|
C#
using System;
class GFG {
public static void Main()
{
int x = 10;
int y = 5;
x = x ^ y;
y = x ^ y;
x = x ^ y;
Console.WriteLine( "After swap: x = " + x + ", y = " + y);
}
}
|
PHP
<?php
$x = 10;
$y = 5;
$x = $x ^ $y ;
$y = $x ^ $y ;
$x = $x ^ $y ;
echo "After Swapping: x = " , $x ,
", " , "y = " , $y ;
?>
|
Javascript
<script>
let x = 10, y = 5;
x = x ^ y;
y = x ^ y;
x = x ^ y;
document.write( "After Swapping: x =" +
x + ", y=" + y);
</script>
|
OutputAfter Swapping: x =5, y=10
Time Complexity: O(1).
Auxiliary Space: O(1).
Problems with the above methods
1) The multiplication and division-based approach doesn’t work if one of the numbers is 0 as the product becomes 0 irrespective of the other number.
2) Both Arithmetic solutions may cause an arithmetic overflow. If x and y are too large, addition and multiplication may go out of the integer range.
3) When we use pointers to variable and make a function swap, all the above methods fail when both pointers point to the same variable. Let’s take a look at what will happen in this case if both are pointing to the same variable.
// Bitwise XOR based method
x = x ^ x; // x becomes 0
x = x ^ x; // x remains 0
x = x ^ x; // x remains 0
// Arithmetic based method
x = x + x; // x becomes 2x
x = x – x; // x becomes 0
x = x – x; // x remains 0
Let us see the following program.
C++
#include <bits/stdc++.h>
using namespace std;
void swap( int * xp, int * yp)
{
*xp = *xp ^ *yp;
*yp = *xp ^ *yp;
*xp = *xp ^ *yp;
}
int main()
{
int x = 10;
swap(&x, &x);
cout << "After swap(&x, &x): x = " << x;
return 0;
}
|
C
#include <stdio.h>
void swap( int * xp, int * yp)
{
*xp = *xp ^ *yp;
*yp = *xp ^ *yp;
*xp = *xp ^ *yp;
}
int main()
{
int x = 10;
swap(&x, &x);
printf ( "After swap(&x, &x): x = %d" , x);
return 0;
}
|
Java
class GFG {
static void swap( int [] xp, int [] yp)
{
xp[ 0 ] = xp[ 0 ] ^ yp[ 0 ];
yp[ 0 ] = xp[ 0 ] ^ yp[ 0 ];
xp[ 0 ] = xp[ 0 ] ^ yp[ 0 ];
}
public static void main(String[] args)
{
int [] x = { 10 };
swap(x, x);
System.out.println( "After swap(&x, &x): x = " + x[ 0 ]);
}
}
|
Python3
def swap(xp, yp):
xp[ 0 ] = xp[ 0 ] ^ yp[ 0 ]
yp[ 0 ] = xp[ 0 ] ^ yp[ 0 ]
xp[ 0 ] = xp[ 0 ] ^ yp[ 0 ]
x = [ 10 ]
swap(x, x)
print ( "After swap(&x, &x): x = " , x[ 0 ])
|
C#
using System;
class GFG {
static void swap( int [] xp, int [] yp)
{
xp[0] = xp[0] ^ yp[0];
yp[0] = xp[0] ^ yp[0];
xp[0] = xp[0] ^ yp[0];
}
static void Main()
{
int [] x = { 10 };
swap(x, x);
Console.WriteLine( "After swap(&x,"
+ "&x): x = " + x[0]);
}
}
|
PHP
<?php
function swap(& $xp , & $yp )
{
$xp = $xp ^ $yp ;
$yp = $xp ^ $yp ;
$xp = $xp ^ $yp ;
}
$x = 10;
swap( $x , $x );
print ( "After swap(&x, &x): x = " . $x );
?>
|
Javascript
<script>
function swap(xp,yp)
{
xp[0] = xp[0] ^ yp[0];
yp[0] = xp[0] ^ yp[0];
xp[0] = xp[0] ^ yp[0];
}
let x=[10];
swap(x, x);
document.write( "After swap(&x, &x): x = "
+ x[0]);
</script>
|
OutputAfter swap(&x, &x): x = 0
Time Complexity: O(1).
Auxiliary Space: O(1).
Swapping a variable with itself may be needed in many standard algorithms. For example, see this implementation of QuickSort where we may swap a variable with itself. The above problem can be avoided by putting a condition before swapping.
C++
#include <bits/stdc++.h>
using namespace std;
void swap( int * xp, int * yp)
{
if (xp == yp)
return ;
*xp = *xp + *yp;
*yp = *xp - *yp;
*xp = *xp - *yp;
}
int main()
{
int x = 10;
swap(&x, &x);
cout << "After swap(&x, &x): x = " << x;
return 0;
}
|
C
#include <stdio.h>
void swap( int * xp, int * yp)
{
if (xp == yp)
return ;
*xp = *xp + *yp;
*yp = *xp - *yp;
*xp = *xp - *yp;
}
int main()
{
int x = 10;
swap(&x, &x);
printf ( "After swap(&x, &x): x = %d" , x);
return 0;
}
|
Java
class GFG {
static void swap( int xp, int yp)
{
if (xp == yp)
return ;
xp = xp + yp;
yp = xp - yp;
xp = xp - yp;
}
public static void main(String[] args)
{
int x = 10 ;
swap(x, x);
System.out.println( "After swap(&x, &x): x = " + x);
}
}
|
Python3
def swap(xp, yp):
if (xp[ 0 ] = = yp[ 0 ]):
return
xp[ 0 ] = xp[ 0 ] + yp[ 0 ]
yp[ 0 ] = xp[ 0 ] - yp[ 0 ]
xp[ 0 ] = xp[ 0 ] - yp[ 0 ]
x = [ 10 ]
swap(x, x)
print ( "After swap(&x, &x): x = " , x[ 0 ])
|
C#
using System;
class GFG {
static void swap( int xp, int yp)
{
if (xp == yp)
return ;
xp = xp + yp;
yp = xp - yp;
xp = xp - yp;
}
public static void Main()
{
int x = 10;
swap(x, x);
Console.WriteLine( "After swap(&x, &x): x = " + x);
}
}
|
PHP
<?php
function swap( $xp , $yp )
{
if ( $xp == $yp )
return ;
$xp = $xp + $yp ;
$yp = $xp - $yp ;
$xp = $xp - $yp ;
}
$x = 10;
swap( $x , $x );
echo ( "After swap(&x, &x): x = " . $x );
return 0;
|
Javascript
<script>
function swap(xp, yp)
{
if (xp == yp)
return ;
xp[0] = xp[0] + yp[0];
yp[0] = xp[0] - yp[0];
xp[0]= xp[0] - yp[0];
}
x = 10;
swap(x, x);
document.write( "After swap(&x , &x) : x = " + x);
</script>
|
OutputAfter swap(&x, &x): x = 10
Time Complexity: O(1).
Auxiliary Space: O(1).
Method 3 (A mixture of bitwise operators and arithmetic operators)
The idea is the same as discussed in Method 1 but uses Bitwise addition and subtraction for swapping.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void swap( int & a, int & b)
{
a = (a & b) + (a | b);
b = a + (~b) + 1;
a = a + (~b) + 1;
}
int main()
{
int a = 5, b = 10;
swap(a, b);
cout << "After swapping: a = " << a << ", b = " << b;
return 0;
}
|
C
#include <stdio.h>
void swap( int a, int b)
{
a = (a & b) + (a | b);
b = a + (~b) + 1;
a = a + (~b) + 1;
printf ( "After swapping: a = %d , b = %d " ,a,b);
}
int main()
{
int a = 5, b = 10;
swap(a, b);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void swap( int a, int b)
{
a = (a & b) + (a | b);
b = a + (~b) + 1 ;
a = a + (~b) + 1 ;
System.out.print( "After swapping: a = " + a + ", b = " + b);
}
public static void main(String[] args)
{
int a = 5 , b = 10 ;
swap(a, b);
}
}
|
Python3
def swap(a, b):
a = (a & b) + (a | b)
b = a + (~b) + 1
a = a + (~b) + 1
print ( "After Swapping: a = " , a, ", b = " , b)
a = 5
b = 10
swap(a, b)
|
C#
using System;
class GFG {
static void swap( int a, int b)
{
a = (a & b) + (a | b);
b = a + (~b) + 1;
a = a + (~b) + 1;
Console.Write( "After swapping: a = " + a
+ ", b = " + b);
}
static void Main()
{
int a = 5, b = 10;
swap(a, b);
}
}
|
Javascript
<script>
function swap(a, b)
{
a = (a & b) + (a | b);
b = a + (~b) + 1;
a = a + (~b) + 1;
document.write( "After swapping: a = " + a + ", b = " + b);
}
let a = 5, b = 10;
swap(a, b);
</script>
|
PHP
<?php
$a = 5;
$b = 10;
echo ( "Before swap(a and b) " . $a . "and" . $b . "<br>" );
$a = ( $a & $b ) + ( $a | $b );
$b = $a + (~ $b ) + 1;
$a = $a + (~ $b ) + 1;
echo ( "After swap(a and b) " . $a . "and" . $b );
return 0;
?>
|
OutputAfter swapping: a = 10, b = 5
Time Complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.
Method 4 (One Line Expression)
We can write only one line to swap two numbers.
- x = x ^ y ^ (y = x);
- x = x + y – (y = x);
- x = (x * y) / (y = x);
- x , y = y, x (In Python)
C++
#include <iostream>
using namespace std;
int main(){
int x = 10, y = 5;
x = (x * y) / (y = x);
cout << x << " " << y;
return 0;
}
|
C
#include <stdio.h>
int main() {
int x = 10, y = 5;
x = (x * y) / (y = x);
printf ( "After Swapping: x = %d, y = %d" , x, y);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int x = 10 ;
int y = 5 ;
x = (x * y) / (y = x);
System.out.println( "After swapping:"
+ " x = " + x + ", y = " + y);
}
}
|
Python3
def swap(x, y):
x , y = y, x
print ( "After Swapping: x = " , x, ", y = " , y)
x = 10
y = 5
swap(x, y)
|
C#
using System;
public class GFG
{
static public void Main ()
{
int x = 10;
int y = 5;
x = (x * y) / (y = x);
Console.Write( "After swapping:" + " x = " + x + ", y = " + y);
}
}
|
Javascript
<script>
let x = 10, y = 5;
x = (x * y)/(x = y);
document.write( "After Swapping: x =" + x + ", y=" + y);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
To know more about swapping two variables in one line, click here.
Please comment if you find anything incorrect, or if you want to share more information about the topic discussed above