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 interrogation non but on Java interviews but too on C in addition to C++ interviews. It is too a good programming questions for freshers. This interrogation was asked to me long dorsum in addition to didn't had whatever thought almost how to approach this interrogation without using temp or 3rd variable, may hold upwards lack of noesis on bitwise operators inwards Java or may hold upwards it didn't click at that time. Given or thence fourth dimension in addition to trial error, I eventually come upwards out amongst a solution amongst but arithmetics operator but interviewer was continue bespeak almost other approaches of swapping 2 variables without using temp or 3rd variable. Personally, I liked this interrogation in addition to included inwards listing of my programming interview question because of its simplicity in addition to or thence logical work, it forcefulness yous to do. When learned bit-wise performance inwards Java I eventually notice or thence other agency of swapping 2 variables without 3rd variable, which I am going to portion amongst yous guys.


Swapping 2 numbers without using temp variable inwards Java

good programming exercise for absolute start timer. By the way, hither is the code lawsuit of swapping 2 numbers without using temp variable in addition to using arithmetics operator inwards Java:

int a = 10;
int b = 20;

System.out.println("value of a in addition to b earlier swapping, a: " + a +" b: " + b);

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

System.out.println("value of a in addition to b afterward swapping, a: " + a +" b: " + b);

Output:
value of a in addition to b earlier swapping, a: 10 b: 20
value of a in addition to b afterward swapping, a: 20 b: 10

Swapping 2 numbers without using temp variable inwards Java amongst bitwise operator

Bitwise operators tin too hold upwards used to swap 2 numbers without using 3rd variable. XOR bitwise operator returns zilch if both operand is same i.e. either  0 or 1 in addition to returns 1 if both operands are dissimilar e.g. 1 operand is zilch in addition to other is one. 


By leveraging this property, nosotros tin swap 2 numbers inwards Java. Here is code lawsuit of swapping 2 numbers without using temp variable inwards 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 inwards binary
int b = 4; //0100 inwards binary
      
System.out.println("value of a in addition to b earlier swapping, a: " + a +" b: " + b);
       
//swapping value of 2 numbers without using temp variable in addition to XOR bitwise operator     
a = a^b; //now a is vi in addition to b is 4
b = a^b; //now a is vi but b is 2 (original value of a)
a = a^b; //now a is iv in addition to b is 2, numbers are swapped
      
System.out.println("value of a in addition to b afterward swapping using XOR bitwise operation, a: " + a +" b: " + b);

value of a in addition to b earlier swapping, a: 2 b: 4
value of a in addition to b afterward swapping using XOR bitwise operation, a: 4 b: 2

Swapping 2 numbers without using temp variable inwards Java amongst partition in addition to multiplication

There is another, 3rd agency of swapping 2 numbers without using 3rd variable, which involves multiplication in addition to partition operator. This is similar to start approach, where nosotros accept used + in addition to - operator for swapping values of 2 numbers. Here is the code lawsuit to swap tow let out without using 3rd variable amongst partition in addition to multiplication operators inwards Java :

int a = 6;
int b = 3;

System.out.println("value of a in addition to b earlier swapping, a: " + a +" b: " + b);

//swapping value of 2 numbers without using temp variable using multiplication in addition to division
a = a*b; //now a is eighteen in addition to b is 3
b = a/b; //now a is eighteen but b is vi (original value of a)
a = a/b; //now a is 3 in addition to b is 6, numbers are swapped

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

Output:
value of a in addition to b earlier swapping, a: 6 b: 3
value of a in addition to b afterward swapping using multiplication in addition to division, a: 3 b: 6

That's all on 3 ways to swap 2 variables without using 3rd variable inwards Java. Its goodness to know multiple ways of swapping 2 variables without using temp or 3rd variable to grip whatever follow-up question. Swapping numbers using bitwise operator is the fastest amidst three, because it involves bitwise operation. It’s too non bad agency to exhibit your noesis of bitwise operator inwards Java in addition to impress interviewer, which in addition to thence may inquire or thence interrogation on bitwise operation. H5N1 prissy play a joke on to drive interview on your goodness area.

Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures in addition to Algorithms: Deep Dive Using Java
Algorithms in addition to Data Structures - Part 1 in addition to 2

Komentar

Postingan populer dari blog ini

Fixing Java.Net.Bindexception: Cannot Assign Requested Address: Jvm_Bind Inwards Tomcat, Jetty

5 Deviation Betwixt Constructor In Addition To Static Mill Method Inward Java- Pros In Addition To Cons

Top V Websites For Practicing Information Structures Together With Algorithms For Coding Interviews Free