Hello all and welcome back.
So in this video let us continue our discussion and solve the second part of the question which we attempted
in the last video.
So in this variation we have to actually update the answer in the list provided.
So as the question basically suggests, we will be given some list in which we have to update our answer.
So now let us see how we will be solving the same.
So the question is updating all elements or all indices in the provided list.
So greet everyone.
Let me just copy this thing.
Maybe from here once.
Return all indices of an element in provided list.
So as the name suggest, this time we will be getting an answer as well.
Answer list as well.
So I'm just calling it answer list.
Now I will tell you what is the major motivation in or I would say suggestion in this type of question.
Let's say when you're trying to solve something and you have to actually append your results in a,
in some list, or the user wants that particular list which he or she is giving to you.
That is where the results should reflect.
A very good industry use case for this can be that let's say Flipkart or Amazon.
They want to they have some list of defaulters.
Okay, defaulters are basically some, I would say users who have not who maybe are, let's say fraudulent
practice, who have not paid for their orders, whose email and stuff is wrong okay, or who have not
actually verified their account.
Now let's say they are maintaining some default list.
Okay, so let's say defaulter list.
Now in this they have they are trying to add by basis or different different use cases.
Let's say someone has not updated their email ID.
Someone phone is wrong.
Someone is not taking up the orders and everything.
And let's say you are now given with the case where you have to add okay, so in this particular use
case, instead of index you can just think that you are just checking if the order was paid or not.
If any order was there which was not paid by that user.
Now, based on that, maybe you need to actually add to this particular list instead of instead of actually
returning the result or printing the result, then this kind of a question will actually be used.
So what I'm doing is pay attention in this particular use case if I'm creating one answer list, right.
So there is a new list.
It can of course have some element as well.
When I'm calling this when I'm calling this okay.
And it should not be return.
Actually it should be update because I have my answer list with me.
So I will be updating it in this particular list.
I will be providing it with this particular elements.
I will be providing this thing for sure.
And along with that, the index and the answer list.
Okay, so this is all what I am providing to my particular function.
And I want that the result should be updated in this particular answer list.
So I hope the question is clear.
Everyone in this we are just updating.
We are just updating into the list which is already given to us.
Right?
So understanding will be a lot easier.
Again, if length of l one dot index is.
If length of L one is equal to index, we are going to return.
Okay.
I will just try to explain you this intuitively as well so that you are aware.
And I will give you a more relatable example.
Let's say you are in a school or in a college.
A teacher calls you, a teacher calls you, and he gives you a notebook.
He gives you his diary or his notebook.
Okay.
So he gives you his diary and he asks you that, hey, go to your class and tell me or write the name
in this particular diary.
So teacher is giving you this diary, right?
Maybe something more are there in this diary.
Maybe it's an empty diary.
We don't care.
But teacher is specifically asking you that.
Hey, go to your class.
Like go in your class and add the names of students who have not done their homework.
Okay, so add the names of the students who have not done their homework.
So let us say these are the students.
So 012345.
Let us say these are their names.
And two and five have not done their homework.
Two and four.
So what you will be doing now is so the way I want you to intuitively think of this particular question,
or whenever you are given any list which you have to update in recursion.
And this list kind of a question is that you will be passing this diary.
So this diary is the main thing which we have to take care of, right.
This let me draw it with red color.
So this diary is the main thing which we have to take care of.
Now this diary actually you will be passing to each and every of the student.
So you will be first given this diary to the first student.
He will be checking if he has done the homework and let us assume they are very honest.
Okay.
So if he has done the homework, he will not write his name.
This will call to the next student.
Okay, so that is something which we are doing right.
We are actually calling to the next I would say element where it checks if it is two or not.
In this particular example that is what we are doing.
And if it has done the homework, it is not going to add the name.
If not, then it basically is going to add the name.
Similarly, this first one is going to provide it to the next student.
Now again this student, he will get that ID and he will update here as well right.
Going forward the same thing is going to go.
And when we reach to the end, we are just going to return and the diary will be updated, right.
Because this person and this person, they will be updating the diary.
Right.
So let me just write this and it will become a lot more clear.
And you will appreciate the industrial as well, this intuitive example.
So pay attention everyone.
Now pay attention.
If my L1 of index is equals to x what I have to do.
Now think of it like that.
Student okay, if you are a student who have not done the homework, the answer list, whatever the
answer list is, you just append your index.
You just append your index.
If you are this particular student, whatever diary you are getting, this diary this is our answer
is analogous to our answer list.
Here you are just going to append your append your name into this particular thing.
And that is what I am doing here.
Once you have done this, once you have done your work.
Okay, so let us say you are the first student here.
Once you have done your work, which is either adding your name or either not adding your name based
on some condition.
In this particular thing it was the homework.
Just call recursion and actually you are all set index plus one because you are sending it to the next
person and the diary is as it is.
Right.
And it might seem a lot easy because of the way I have explained.
I hope, but you are going to get your answer.
See 135 because you have followed the full principle of recursion.
Base case your work recursive call.
Again, if I make this recursive call above, then I hope you know what is going to happen.
So this time, instead of you instead of this person filling in the name, first, he is sending the
diary back.
Okay, so he is sending the diary back.
This time he is saying that hey please you first do the work.
Then I am going to actually add my my information.
So the diary is going to go to the end.
In the very start.
This person is not going to fill anything in the diary.
This person because he has not done the work, he is going to fill its name.
Then it will go back.
It will go back.
The second person again, he will get that ID the first person.
Fourth.
I would say index name is written.
He will also write his name and he is going to return that ID back and we will get everything but this
time in the reverse order.
And again, if you have understood this, if you have actually followed everything, then you will be
able to tell that the answer now is going to be just in the reverse order, which is five, three,
one.
Okay.
So yes, the these questions are this simple.
If you are able to actually understand the underlying principle behind them.
And I hope you are able to actually see that why we are like, again, this was printing all the indices.
This is just updating here.
So tomorrow if you are working in a company where let us say you have some list of defaulters and you
have to work like this.
Again, this is the kind of a solution on which your mind will go, and you will be able to code the
same as well.
Okay, based on if you want the earliest defaulter.
So in this all the students who are at the last, their name is going to come into the first.
Right.
So in that understanding, if you write your code properly, it's going to work.
And please make sure that you understand the difference here.
The list is getting updated.
This particular list is getting updated.
Okay, so let us just let me just quickly show you this in Python as well everyone.
So let me just cut it short so that we are able to understand it quickly.
Pay attention.
See now we have defined the function.
One answer list gets created.
It is an empty list initially and we are calling this particular function, which is update all indices
of an element provided list in provided list.
So this is our provided list.
Again, think of from an intuition intuitive example of the teacher and student teacher has given its
diary.
These are the students first and see answer list is pointing here only and list is pointing here x is
two is the index at zero two.
It is not base case is not also satisfied.
So we are going to move forward.
Now we are going to call the next function.
But pay attention.
What is going to happen here L one is still this answer list is still this.
So the diary is actually getting passed.
You are not creating any new diary.
You are not asking you to write on any paper.
No, the diary you are giving to the next person, right?
You are giving this particular information or the address of that ID.
You are telling that, hey, this is where the diary is kept.
Go and update on that this time.
This case is of course not satisfied.
L one index.
Yes, it is equals to 2L1 of one.
So I am going to append it to my diary.
And you will see that this has been appended.
And the change has actually happened at the whole answer list level.
Now when I make the next function call because I am not returning here.
Now pay attention when this gets this diary, this answer list, it is actually updated with the index.
It is not a new diary.
We yes, we are passing it in the argument, but we are passing the same diary, the same answer list
every time.
And this is something which I want to explain to you why this visualization in Python, Twitter and
via the intuitive example in one which I have drawn here.
Because if you understand using this, you are never going to forget and you are going to write awesome
code because your understanding will be top notch.
Okay, so now because this particular diary is this particular function is again getting the same diary.
It is having the defaulter or it is having the index of one.
Now, when we go forward and I do the same thing.
Okay, I'm going to call for the next function again.
This is also not doing anything.
This is going to just update it.
And finally when I return when I return from all the functions.
Okay.
When all the function does their work and we are returning, each and every one of them have done their
work on that ID and I will be able to see the final, I would say result in my particular answer list
or the diary from my intuitive example.
And when I thus when I just print this answer is, you can see that it is still pointing it to here.
And this has been updated and we are able to see the answer.
Normally I have seen with this intuitive example it is lot easier to understand because now you can
think in terms of exactly what is going to happen, what's actually happening.
Okay, so yes, I hope that yeah, that was all in the question.
Please make sure that you try and write it down once.
The code is a lot easier in recursion, which you will be seeing if you understand the basics right.
But understanding them with I would say all, uh, test cases, everything.
That is something which you want to check.
Now let me just run it for some other cases as well.
It's empty.
If I run it for 1043, if I run it, it's one second.
Yeah.
For three we are getting zero.
So this code is going to run for everything again.
You will do this practice of running it every time.
And as I told as I told you, please, please make sure that before going to Python tutor or anything
you dry run the code yourself and draw the recursion tree and everything.
Because if you do this for all the questions which I am even discussing as part of this course, you
will actually be able to solve any question.
I can assure you that these are the types of questions which are actually asked in interviews of Google,
Microsoft and all these man or tier one companies in Goldman as well.
These kind of questions were actually asked where we were very much interested on how a particular candidate
can change.
The result can actually understand the intricate, intricate details and everything when he is writing
a particular code.
Okay.
So please make sure that you understand it very nicely.
And in the next video we are going to do the third variation which was update indices in the global
list.
Okay.
So thank you very much everyone.