Swap two integers without using temp/third variable - Java Interview Questions -8
ฝัง
- เผยแพร่เมื่อ 14 ต.ค. 2024
- Swap two integers without using temp/third variable:
Method 1 (Using Arithmetic Operators):
int x = 10, y = 5;
// Code to swap 'x' and 'y'
x = x + y; // x now becomes 15
y = x - y; // y becomes 10
x = x - y; // x becomes 5
Multiplication and division can also be used for swapping:
x = x * y; // x now becomes 50
y = x / y; // y becomes 10
x = x / y; // x becomes 5
Method 2 (Using Bitwise XOR):
x = x ^ y; // x now becomes 15 (1111)
y = x ^ y; // y becomes 10 (1010)
x = x ^ y; // x becomes 5 (0101)
===================================================
Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:
www.youtube.co...
Follow me on my Facebook Page:
/ naveenqtpexpert
Let's join our Automation community for some amazing knowledge sharing and group discussion:
t.me/joinchat/... Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:
www.youtube.co...
Follow me on my Facebook Page:
/ naveenqtpexpert
Let's join our Automation community for some amazing knowledge sharing and group discussion on Telegram:
t.me/joinchat/...
Paid courses (Recorded) videos:
Java & Selenium Course: www.naveenautom...
API Course: www.naveenautom... ➡️Get Our Courses✔️
📗 Get My Paid Courses at
Paid courses (Recorded) videos:
Java & Selenium Course: www.naveenautom...
API Course: www.naveenautom...
-------------------------------
✔️SOCIAL NETWORKS
Facebook: / naveenqtpexpert
Twitter: / naveenkhunteta
Blog: www.naveenautom...
--------------------------------
Support My Channel✔️Or Buy Me A Coffee
Paypal: paypal.me/nave...
Google Pay: naveenanimation20@gmail.com
--------------------------------
✔️Thanks for watching!
देखने के लिए धन्यवाद
Благодаря за гледането
感谢您观看
Merci d'avoir regardé
Grazie per la visione
Gracias por ver
شكرا للمشاهدة
public static void swap(int x, int y){
x=x+y;
y=x-y;
x=x-y;
}
hope this helps
x = (x + y) - ( y = x );
Hi Naveen, this also would be an optimal solution in a single line
hello Navin sir, i'm a EC student,
In XOR Swapping, X^Y = 1111 (15)
But Y = X^Y is 5 //Instead of 10
And X = X^Y is 10 // Instead of 5
Sir I got a doubt as string is immutable how can we concatenate and assign to same string, just read on net that a new string is created and it reference is set to the object reference.
Hi Naveen can make video to find second largest number in the array
Is xor operator is valid for all the inputs . As per the Xor operator it depends upon on placing of 0&1. I have tested it for many inputs and its working but amazing . please let me know how can anybody thinks like that.
There is one more logic i tried after watching it:
x=(x+y) - (y=x);
I think there is some mistake in multiplication without using third variable
x = x * y; // x now becomes 50
y = x / y; // y becomes 10 // instead of 5
x = x / y; // x becomes 5 // instead of 10
you have use int x=5, y=10
No, your logic is wrong man! It is like this : x = x * y; // x now becomes 50
y = x / y; // y becomes 5 (y=>50/10=5)
x = x / y; // x becomes 10 (x=>50/5=10)
Sir, i could not commit wrong logic actually i have comparing with video or notes, in video you have mention x=5, y=10 or in notes it vice versa.
See the execution has been done and the results have been printed in the console, and i have also tested and tried it.
x=5, y=10;
x = x*y; //5*10=50
y = x/y; //50/10=5 now y value has been changed to 5
x = x/y; // still x has value 50 and on performing right hand side division x/y i.e, 50/5=10 and assignment, the value of x is now changed to 10.
Swapping achieved with * operator
@@naveenautomationlabs Naveen swapping without using third variable by multiplication & division doesn't work if any one of the 2 number is ZERO. It throws ArithmeticException
For more visit:
th-cam.com/video/H_Bj8KZiv6I/w-d-xo.html