Swap Two Numbers
Given two numbers a and b, the task is to swap them.
Examples:
Input: a = 2, b = 3
Output: a = 3, b = 2Input: a = 20, b = 0
Output: a = 0, b = 20Input: a = 10, b = 10
Output: a = 10, b = 10
Table of Content
[Naive Approach] Using Third Variable
The idea is to use a third variable, say temp to store the original value of one of the variables during the swap.
- Store the value of a in temp.
- Assign the value of b to a.
- Assign the value of temp to b.
// C++ Code to swap two numbers using third variable
#include <iostream>
using namespace std;
int main() {
int a = 2, b = 3;
cout << "a = " << a << " b = " << b << endl;
// Swap a and b using temp variable
int temp = a;
a = b;
b = temp;
cout << "a = " << a << " b = " << b << endl;
return 0;
}
// C Code to swap two numbers using third variable
#include <stdio.h>
int main() {
int a = 2, b = 3;
printf("a = %d b = %d\n", a, b);
// Swap a and b using temp variable
int temp = a;
a = b;
b = temp;
printf("a = %d b = %d\n", a, b);
return 0;
}
// Java Code to swap two numbers using third variable
class GfG {
public static void main(String[] args) {
int a = 2, b = 3;
System.out.println("a = " + a + " b = " + b);
// Swap a and b using temp variable
int temp = a;
a = b;
b = temp;
System.out.println("a = " + a + " b = " + b);
}
}
# Python Code to swap two numbers using third variable
if __name__ == "__main__":
a = 2
b = 3
print("a =", a, " b =", b)
# Swap a and b using temp variable
temp = a
a = b
b = temp
print("a =", a, " b =", b)
// C# Code to swap two numbers using third variable
using System;
class GfG {
static void Main() {
int a = 2, b = 3;
Console.WriteLine("a = " + a + " b = " + b);
// Swap a and b using temp variable
int temp = a;
a = b;
b = temp;
Console.WriteLine("a = " + a + " b = " + b);
}
}
// JavaScript Code to swap two numbers using third variable
let a = 2, b = 3;
console.log("a = " + a + " b = " + b);
// Swap a and b using temp variable
let temp = a;
a = b;
b = temp;
console.log("a = " + a + " b = " + b);
Output
a = 2 b = 3 a = 3 b = 2
Time Complexity: O(1)
Auxiliary Space: O(1)
[Expected Approach] Without using Third Variable
The idea is to swap two numbers using Arithmetic operators or Bitwise operators. To know more about the implementation, please refer Swap Two Numbers Without Using Third Variable.
[Alternate Approach] Built-in Swap
We can also swap using built-in functionalities like swap() function in C++, tuple unpacking in Python, destructuring assignment in JavaScript.
// C++ Code to swap two numbers using swap function
#include <iostream>
using namespace std;
int main() {
int a = 2, b = 3;
cout << "a = " << a << " b = " << b << endl;
swap(a, b);
cout << "a = " << a << " b = " << b << endl;
return 0;
}
#include <stdio.h>
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a = 2, b = 3;
printf("a = %d b = %d\n", a, b);
swap(&a, &b);
printf("a = %d b = %d\n", a, b);
return 0;
}
# Python Code to swap two numbers using tuple unpacking
if __name__ == "__main__":
a = 2
b = 3
print("a =", a, " b =", b)
# Tuple unpacking
a, b = b, a
print("a =", a, " b =", b)
// JavaScript Code to swap two numbers using destructuring assignment
let a = 2, b = 3;
console.log("a = " + a + " b = " + b);
// destructuring assignment
[a, b] = [b, a]
console.log("a = " + a + " b = " + b);
Output
a = 2 b = 3 a = 3 b = 2
Time Complexity: O(1)
Auxiliary Space: O(1)
Refer here for other approaches: Swap Two Numbers Without Using Third Variable