Swap Two Numbers

Easy
0/40
Average time to solve is 10m
profile
Contributed by
1123 upvotes
Asked in companies
Tata Consultancy Services (TCS)CIS - Cyber InfrastructureGenpact

Problem statement

You are given two numbers 'a' and 'b' as input.


You must swap the values of 'a' and 'b'.


For Example:
Input: 
'a' = 8, 'b' = 5

Output:
5 8

Explanation:
Initially, the value of 'a' and 'b' is 8 and 5, respectively.

After swapping, the value of 'a' is 5, and the value of 'b' is 8.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first and only input line contains two integers, 'a' and 'b', representing the two numbers. 
Output Format:
The first line of output prints the swapped value of 'a' and 'b'.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Sample Input 1:
1 2 
Sample Output 1:
2 1
Explanation For Sample Input 1:
The output of the above test case is 2 1.
Sample Input 2:
5 6
Sample Output 2:
6 5
Explanation For Sample Input 2:
The output of the above test case is  6 5.
Constraints:
-10^5 <= 'a', 'b' <= 10 ^ 5

Time Limit: 1 sec
Hint

We can use a third variable.

Approaches (2)
Swapping using a temporary variable

Take input in two integer datatype variables, a and b. Create a temporary variable temp and initialize it equal to a. Now make a equal to b, and finally make b equal to temp. Print a and b.

Time Complexity

O(1), 

 

The Time Complexity is O(1).

Space Complexity

O(1), 

 

The Space Complexity is O(1).

Code Solution
(100% EXP penalty)
Swap Two Numbers
Full screen
Console