Given two numbers a and b, the task is to swap them.
Examples:
Input: a = 2, b = 3
Output: a = 3, b = 2
Input: a = 20, b = 0
Output: a = 0, b = 20
Input: a = 10, b = 10
Output: a = 10, b = 10
[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++
// 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
// 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
// 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
# 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#
// 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
// 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);
Outputa = 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++
// 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;
}
C
#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
# 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
// 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);
Outputa = 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
Similar Reads
Swap three numbers in cycle Given three numbers, swap them in cyclic form. First number should get the value of third, second should get the value of first and third should get value of second. Examples: Input : a = 2, b = 4, c = 7 Output : a = 7, b = 2, c = 4 Input : a = 10, b = 20, c = 30 Output : a = 30, b = 10, c = 20 Prer
5 min read
Swap every two bits in bytes Swap all the pair of bits in a byte. Before swapping: 11-10-11-01 After swapping: 11-01-11-10Examples: Input : 00000010 Output : 00000001 Input : 00000100 Output : 00001000 Approach: x = ((x & 0x55555555) >> 1) | ((x & 0xAAAAAAAA) <> 1 extracts the high bit position and shifts it
3 min read
Swap Two Numbers Without Using Third Variable Given two variables a and y, swap two variables without using a third variable. Examples: Input: a = 2, b = 3Output: a = 3, b = 2Input: a = 20, b = 0Output: a = 0, b = 20Input: a = 10, b = 10Output: a = 10, b = 10Table of ContentUsing Arithmetic OperatorsUsing Bitwise XORBuilt-in SwapUsing Arithmeti
6 min read
Swap bits in a given number Given a number x and two positions (0 based from the right side) in the binary representation of x, write a function that swaps n bits at the given two positions and returns the result. It is also given that the two sets of bits do not overlap.Examples:Input:x = 47 (00101111)p1 = 1 (Start from the s
12 min read
Sorting Array with Two Swaps Given an array A[] that represents a permutation of the N numbers, the task is to determine if it's possible to sort the array using two swaps. If it is not possible return false otherwise return true. Examples: Input: N = 4, A[] = {4, 3, 2, 1}Output: TrueExplanation: Swap(A[1], A[4]), now A[] = {1,
7 min read
Largest number with one swap allowed Given a numeric string s, return the lexicographically largest string possible by swapping at most one pair of characters.Note: Leading zeros are not allowed.Examples: Input: s = "768"Output: "867"Explanation: Swapping the 1st and 3rd characters(7 and 8 respectively), gives the lexicographically lar
8 min read