How To Swap 2 Numbers Without Temp Or 3Rd Variable Inwards Coffee - Interview Question

How to swap 2 numbers without using temp or 3rd variable is mutual interview query non only on Java interviews but likewise on C as well as C++ interviews. It is likewise a good programming questions for freshers. This query was asked to me long dorsum as well as didn't had whatever thought well-nigh how to approach this query without using temp or 3rd variable, may survive lack of noesis on bitwise operators inward Java or may survive it didn't click at that time. Given roughly fourth dimension as well as case error, I eventually come upward out with a solution with only arithmetics operator but interviewer was perish on cry for well-nigh other approaches of swapping 2 variables without using temp or 3rd variable. Personally, I liked this query as well as included inward listing of my programming interview question because of its simplicity as well as roughly logical work, it forcefulness y'all to do. When learned bit-wise functioning inward Java I eventually notice roughly other agency of swapping 2 variables without 3rd variable, which I am going to portion with y'all guys.


Swapping 2 numbers without using temp variable inward Java

good programming exercise for absolute outset timer. By the way, hither is the code representative of swapping 2 numbers without using temp variable as well as using arithmetics operator inward Java:

int a = 10;
int b = 20;

System.out.println("value of a as well as b earlier swapping, a: " + a +" b: " + b);

//swapping value of 2 numbers without using temp variable
a = a+ b; //now a is thirty as well as b is 20
b = a -b; //now a is thirty but b is ten (original value of a)
a = a -b; //now a is twenty as well as b is 10, numbers are swapped

System.out.println("value of a as well as b afterward swapping, a: " + a +" b: " + b);

Output:
value of a as well as b earlier swapping, a: 10 b: 20
value of a as well as b afterward swapping, a: 20 b: 10

Swapping 2 numbers without using temp variable inward Java with bitwise operator

Bitwise operators tin likewise survive used to swap 2 numbers without using 3rd variable. XOR bitwise operator returns nothing if both operand is same i.e. either  0 or 1 as well as returns 1 if both operands are unlike e.g. 1 operand is nothing as well as other is one. 


By leveraging this property, nosotros tin swap 2 numbers inward Java. Here is code representative of swapping 2 numbers without using temp variable inward Java using XOR bitwise operand:

A       B       A^B (A XOR B)
0       0       0 (zero because operands are same)
0       1       1
1       0       1 (one because operands are different)
1       1       0

int a = 2; //0010 inward binary
int b = 4; //0100 inward binary
      
System.out.println("value of a as well as b earlier swapping, a: " + a +" b: " + b);
       
//swapping value of 2 numbers without using temp variable as well as XOR bitwise operator     
a = a^b; //now a is half-dozen as well as b is 4
b = a^b; //now a is half-dozen but b is 2 (original value of a)
a = a^b; //now a is iv as well as b is 2, numbers are swapped
      
System.out.println("value of a as well as b afterward swapping using XOR bitwise operation, a: " + a +" b: " + b);

value of a as well as b earlier swapping, a: 2 b: 4
value of a as well as b afterward swapping using XOR bitwise operation, a: 4 b: 2

Swapping 2 numbers without using temp variable inward Java with segmentation as well as multiplication

There is another, 3rd agency of swapping 2 numbers without using 3rd variable, which involves multiplication as well as segmentation operator. This is similar to outset approach, where nosotros convey used + as well as - operator for swapping values of 2 numbers. Here is the code representative to swap tow release without using 3rd variable with segmentation as well as multiplication operators inward Java :

int a = 6;
int b = 3;

System.out.println("value of a as well as b earlier swapping, a: " + a +" b: " + b);

//swapping value of 2 numbers without using temp variable using multiplication as well as division
a = a*b; //now a is eighteen as well as b is 3
b = a/b; //now a is eighteen but b is half-dozen (original value of a)
a = a/b; //now a is 3 as well as b is 6, numbers are swapped

System.out.println("value of a as well as b afterward swapping using multiplication as well as division, a: " + a +" b: " + b);

Output:
value of a as well as b earlier swapping, a: 6 b: 3
value of a as well as b afterward swapping using multiplication as well as division, a: 3 b: 6

That's all on 3 ways to swap 2 variables without using 3rd variable inward Java. Its goodness to know multiple ways of swapping 2 variables without using temp or 3rd variable to handgrip whatever follow-up question. Swapping numbers using bitwise operator is the fastest alongside three, because it involves bitwise operation. It’s likewise dandy agency to demo your noesis of bitwise operator inward Java as well as impress interviewer, which as well as thus may inquire roughly query on bitwise operation. Influenza A virus subtype H5N1 prissy fox to drive interview on your goodness area.

Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures as well as Algorithms: Deep Dive Using Java
Algorithms as well as Data Structures - Part 1 as well as 2

0 Response to "How To Swap 2 Numbers Without Temp Or 3Rd Variable Inwards Coffee - Interview Question"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel