Swapping four variables without temporary variable
Suppose we have four variables a, b, c, d and we want to perform swapping of these variables in the following manner
a = b, b = c, c = d, d = a
without using any other fifth or temporary variable
Solution :
Step 1. Swap a and b without using any other variable
a = a + b
b = a – b
a = a – b
Step 2. Swap b and c without using any other variable
b = b + c
c = b – c
b = b – c
Step 3. Swap c and d without using any other variable
c = c + d
d = c – d
c = c – d
Examples:
Input : a = 1, b = 2, c = 3, d = 4
Output :After swapping
a = 2, b = 3, c = 4, d = 1
Input :a = 6, b = 9, c = 10, d = 50
Output : After swapping : a = 9, b = 10, c = 50, d = 6
Prerequisite: Swapping two variables without temporary variable, Swapping three variables without temporary variable
CPP
#include <bits/stdc++.h>
using namespace std;
void swap( int a, int b, int c, int d)
{
a = a + b;
b = a - b;
a = a - b;
b = b + c;
c = b - c;
b = b - c;
c = c + d;
d = c - d;
c = c - d;
cout << "values after swapping are : " << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
cout << "d = " << d << endl;
}
int main()
{
int a = 1;
int b = 2;
int c = 3;
int d = 4;
cout << "Values before swapping are :" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
cout << "d = " << d << endl << endl;
swap(a, b, c, d);
return 0;
}
|
Java
class GFG {
static void swap( int a, int b, int c, int d)
{
a = a + b;
b = a - b;
a = a - b;
b = b + c;
c = b - c;
b = b - c;
c = c + d;
d = c - d;
c = c - d;
System.out.println( "values after "
+ "swapping are : " );
System.out.println( "a = " + a);
System.out.println( "b = " + b);
System.out.println( "c = " + c);
System.out.println( "d = " + d);
}
public static void main(String[] args)
{
int a = 1 ;
int b = 2 ;
int c = 3 ;
int d = 4 ;
System.out.println( "values before "
+ "swapping are : " );
System.out.println( "a = " + a);
System.out.println( "b = " + b);
System.out.println( "c = " + c);
System.out.println( "d = " + d);
System.out.println( "" );
swap(a, b, c, d);
}
}
|
Python3
def swap(a, b, c, d):
a = a + b
b = a - b
a = a - b
b = b + c
c = b - c
b = b - c
c = c + d
d = c - d
c = c - d
print ( "values after swapping are : " )
print ( "a = " , a)
print ( "b = " , b)
print ( "c = " , c)
print ( "d = " , d)
a = 1
b = 2
c = 3
d = 4
print ( "values before swapping are : " )
print ( "a = " , a)
print ( "b = " , b)
print ( "c = " , c)
print ( "d = " , d)
print ("")
swap(a, b, c, d)
|
C#
using System;
class GFG {
static void swap( int a, int b, int c, int d)
{
a = a + b;
b = a - b;
a = a - b;
b = b + c;
c = b - c;
b = b - c;
c = c + d;
d = c - d;
c = c - d;
Console.WriteLine( "values after "
+ "swapping are : " );
Console.WriteLine( "a = " + a);
Console.WriteLine( "b = " + b);
Console.WriteLine( "c = " + c);
Console.WriteLine( "d = " + d);
}
public static void Main()
{
int a = 1;
int b = 2;
int c = 3;
int d = 4;
Console.WriteLine( "values before "
+ "swapping are : " );
Console.WriteLine( "a = " + a);
Console.WriteLine( "b = " + b);
Console.WriteLine( "c = " + c);
Console.WriteLine( "d = " + d);
Console.WriteLine( "" );
swap(a, b, c, d);
}
}
|
PHP
<?php
function swap( $a , $b , $c , $d )
{
$a = $a + $b ;
$b = $a - $b ;
$a = $a - $b ;
$b = $b + $c ;
$c = $b - $c ;
$b = $b - $c ;
$c = $c + $d ;
$d = $c - $d ;
$c = $c - $d ;
echo "values after swapping are : " , "\n" ;
echo "a = " , $a , "\n" ;
echo "b = " , $b , "\n" ;
echo "c = " , $c , "\n" ;
echo "d = " , $d , "\n" ;
}
$a = 1;
$b = 2;
$c = 3;
$d = 4;
echo "Values before swapping are :" , "\n" ;
echo "a = " , $a , "\n" ;
echo "b = " , $b , "\n" ;
echo "c = " , $c , "\n" ;
echo "d = " , $d , "\n" , "\n" ;
swap( $a , $b , $c , $d );
?>
|
Javascript
<script>
function swap(a, b, c, d)
{
a = a + b;
b = a - b;
a = a - b;
b = b + c;
c = b - c;
b = b - c;
c = c + d;
d = c - d;
c = c - d;
document.write( "values after swapping are : " + "</br>" );
document.write( "a = " + a + "</br>" );
document.write( "b = " + b + "</br>" );
document.write( "c = " + c + "</br>" );
document.write( "d = " + d);
}
let a = 1;
let b = 2;
let c = 3;
let d = 4;
document.write( "values before swapping are : " + "</br>" );
document.write( "a = " + a + "</br>" );
document.write( "b = " + b + "</br>" );
document.write( "c = " + c + "</br>" );
document.write( "d = " + d + "</br>" );
document.write( "" + "</br>" );
swap(a, b, c, d);
</script>
|
Output
Values before swapping are :
a = 1
b = 2
c = 3
d = 4
values after swapping are :
a = 2
b = 3
c = 4
d = 1
Time complexity: O(1) because it is doing constant operations
Auxiliary space: O(1)
Last Updated :
18 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...