Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
So I hope you've given it a try and you've managed to solve this interview question on your own.
Even so, and even if not, let's do it together to make sure that you've done it right.
So our task is to make a swap between variable A and variable B without using any additional variables,
without using some temp variable or.
Right.
And to do so, there are several approaches that basically we can use.
Each of them involves some math operations.
Well, I mean, basically, how can we do anything without using math?
Right.
And this solution simply involves some addition and subtraction operations.
Only the plus and minus operations.
Write something that you know how to do, plus and minus how to use additions and how to use substraction,
subtraction and all that we are going to do here in this solution may seem to you at first glance like
sort of a trick.
Okay, but that's not a trick.
We just use math rules to make assumptions about the result of different operations.
And with that being said, let's move on to our first step.
But beforehand, we will give track some reminder of what were the values of A and B before swapping.
So basically, we will simply not get lost with what was the value of A and what was the value of B
before the actual swapping before we begin all of these functionality.
So we will say that initial A equals two 10 and initial B equals two 20.
And now let's put in variable A, the sum of what is already there, which is actually the ace value,
which is stem and add to the value of B, which is 20.
This means that after these first operation, these first addition we will have inside of variable a
Vinicio, a value plus the initial B value.
Right.
That what we've done here, we just use the addition operation.
So simply saying now variable A will have the value of thirty and variable B still remain unchanged.
Right.
We didn't make any assignments to it yet.
So it's just remains with his value of twenty.
All right.
If these the initial B.
So to summarize the first step, what we've done so far, what we have now, our current status.
OK.
We know that in the variable A. We currently have both the initial A value and the initial B value.
And also in variable B, we currently have the initial B value is very clear so far.
This is the first step.
I think it's pretty easy.
We've just done some basic math operations and we are ready to move on.
Oh.
And just don't forget to update the value of failure like we've written here below, which is 30 in
this case.
And now we are ready to move on to the second step, which is subtraction.
So our goal is to put in variable B, the actual previous the actual initial value of A.
And vice versa.
Right.
And now we can easily get that just by using subtraction.
We can say that B equals to A minus B.
This can be viewed as the following.
Right.
We know that A at this point.
At this current point.
It's the initial A plus the initial B we can see here on the left.
And we just instead of using A, we specify that this is the initial A plus initial B minus B, which
B in this case currently is just the initial B.
So we if we make these subtraction, we will see that ten plus 20, minus 20 equals to 10.
Right.
So now B, B, the variable B has the actual previous A value of the initially A value.
We can see that B has the initial A value, which is ten.
So that's the summary of the second step of the second step, which is subtraction.
And we see that the current status of A remains the same.
It's initially plus initial B and B equals two initial A.
So now we can see the third that we can take a look at the third step, which is also using a subtraction.
So variable B at this point already has the value it should have after the swap operation.
Right.
It already has the initial a value.
So we know that it's OK.
B is looking pretty much good.
We can say that we are in the right direction.
But we do remember that our task is also that variable A will have the initial B value.
Right.
And currently that is not the case yet.
So our task is to remove.
Right.
Remove these.
Michelle A. from the some of initially blessing Michelle B, which is an inside of variable A and to
remain with just the initial B value.
So for that, we are going to use the same step prefigures pretty much the same step of using subtraction
as you've seen in the second step.
So you go like a equals to A minus B, so you just replace the value of A in this equation.
You say that A equals two initially plus initial B, which you can see that it's the actual value of
Fey at this point minus the initial eight, which is the value of variable B.
Now if you just put it here, the real value is that you can see there reminder here on the bottom left
part of this slide.
You can see that A equals to ten plus twenty minus ten, which is a total of twenty.
So the initial A is being canceled and the variable A remains only with the value of initial B, which
is 20.
In our example, in basically you can see that just by using these three steps, you've completed successfully
the swap operation between two variables.
Right.
You can see that, first of all, the initial a value was ten and the initial B value was 20.
And now after the swap operation, after these three steps that you've done by using only additions
and two subtractions, you've managed to swap the values between variable A and B.
And it's not some sort of trick that works only for values 10 and 20.
You can use it for any value, right?
Because it's just like in math we use here at two variables, let's say X and Y, which were just the
initially and the initial B were simply done, some equations, some mathematical operations.
And we've managed to solve this one on on our wrong right.
So compare your results and what you've done so far with what I've done so far are there are also,
as I've said previously, additional ways to solve this exercise by using multiplication division,
bitwise operations and so on.
But we are going to stick with this solution because it's the easiest.
And I think that you can manage to solve it on your interview questions and to let your interviewer
see that you really know how and even that these sort of things can be done.
I mean, sometimes in the pressure of your interview, you're interviewing me may even ask you, is
it even possible?
And you're going to see.
I don't know.
We always done it with the third variable.
I don't know if it's impossible.
You may have an answer.
No, it's not possible.
But you see that the.
That's not the answer.
The real answer, that it can be done and it can be done in many ways.
And one of them is very, very simple, just using addition and subtraction operations.
So I hope that's clear.
Guys, you really are amazing.
And I want to thank you for on joining this course.
Getting to this point, making these interview question.
Don't forget to leave some feedback and hopefully I will see you in another videos like this one.
And I wish you good luck on your interviews and your exams and have a great day.
Goodbye.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.