Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Swap Number Without Temporary Variable

Easy
0/40
Average time to solve is 15m
16 upvotes
Asked in companies
CiscoDell TechnologiesMicrosoft

Problem statement

Given two variables ‘X’ and ‘Y’. Your task is to swap the number without using a temporary variable or third variable.

Swap means the value of ‘X’ and ‘Y’ must be interchanged. Take an example ‘X’ is 10 and ‘Y’ is 20 so your function must return ‘X’ as a 20 and ‘Y’ as a 10.

Detailed explanation ( Input/output format, Notes, Images )
Constraints:
1 <= T <= 2*10^5
-10^9 <= X,Y <= 10^9

Where ‘T’ is the total number of test cases, ‘X’ and ‘Y’ denotes two given integer variables.

Time limit: 1 second
Sample Input 1:
2
10 12 
-4 -5   
Sample Output 1:
12 10
-5 -4
Explanation of sample input 1:
Test Case 1:
Given ‘X’ is 10 and ‘Y’ is 12. After swapping ‘X’ will be 12 and ‘Y’ will be 10.

Test Case 2:
Given ‘X’ is -1 and ‘Y’ is -5. After swapping ‘X’ will be -5 and ‘Y’ will be -4.
Sample Input 2:
2
1 2 
0 -5   
Sample Output 2:
2 1
-5 0
Full screen
Console