No.
Let us go into the code and write a code for this particular function.
As you will write the code, we will think of the base case and we will solve it based on the framework
and also draw the recursion tree for the same.
So let me define a function def first index of an element.
Let us say we will be giving a list l one and let us say x.
I am taking the number which will be there.
So let me just write pass as of now and let me just call this particular method.
So I am taking the same example 3252821.
And let us say we want to find for two.
In a very similar way we will be just finding for I would say other cases as well, but that should
be easily doable.
Let us say ten and let us say one okay.
Great, everyone.
So now let's solve this particular question.
Let us first think of a base case.
So as of now we have seen that we are actually following a very good, I would say, framework.
The very first step in this particular framework is base case.
Right.
In base case, as we have already seen in lists and array, we normally take when the size is 0 or 1
because that is like the minimum size or minimum I would say array, which we can get right.
So if your length of L1 I feel is zero, then we can very comfortably say that we are not going to find
this right.
Because in a zeroth element, how can you find any, uh, in a zero size array or list?
How can you find any element so we can return minus one safely?
Now, of course, I have made it clear that if it is not found, I have to return minus one.
If the question requires anything else based on that, you should return that particular value.
Now let us focus on the next part, which is how should we go about this first index of a element first,
right?
We see already in the last question, which we did, that we can either do the step or I would say our
R work first or make the recursion call first, but that is also depending on lots of things.
For example, in a one to n and n to one function, we saw that how calling the function first was helpful
if we want to print one first, right so that everything will cripple down to towards one, and then
it will be printed first.
And then while the function calls are returning, we will print n in the last while the vice versa.
Basically where we were printing n first that was two inches printing n to one use case, right?
So if you have not seen that video, please make sure that you see the same.
Now in this particular question as well.
I personally believe that if we are checking first only if we are able to check first before making
the recursive call, that will be helpful instead of making the full recursive call and then actually
seeing that, okay, if we are able to find the element or not, right?
Because provided that I have to give the first element.
I have to tell the story.
I have to tell the first index.
What if it's actually the very first element?
So if you are working on the first element and asking a recursion to work on the leftover part, then
please make sure.
Then please make sure that you are actually finding the same thing first.
Right.
So I will just quickly write the same thing so that you have these things in notes as well.
So for first index.
We have our base case right.
Second part what we can do is if l one of zero okay.
If l one of zero we have let us say equal to x then we can return this particular index or we can return
zero.
We will see what exactly that is going to do.
And then we can make our recursive call.
So now let us code this and see what exactly is going to happen.
Let me, just for the time being, remove this thing and yeah, cool.
So pay attention everyone.
If my L1 of zero is equals to x.
Now the thing is, what should I return?
What should I return?
Thinking that okay, if I am able to find it on the very first element only, let's return zero for
the time being.
And let me now call recursion, because I believe that it is going to give me the answer.
So let me call recursion on the leftover part right?
So x is going to be same y.
I am going to return the recursion part for this particular thing.
So let us first check it for three and try to run.
It seems to run three basically is coming the very first time on the zeroth index that seems to run.
This particular code seems to run for the element of three.
But is this code then correct?
Let us check.
Let us run it for ten.
If I am running for ten, it is returning minus one.
So this also seems to be right.
But now let us see.
And this is also something which I want to make sure that you understand.
That is why I am doing this question in front of you.
That please make sure that you are running your solution on multiple use cases.
Your solution should not just, I would say pass the use case on which you are thinking, okay, but
other use cases as well, or other test cases as well.
Rather.
Okay.
So while development of course we talk about in terms of use cases.
But while you are solving these questions please make sure that you are running it on different, different
test cases and not just on a single.
You can write your code in the logic based on a single test case which you have in mind, but please
make sure that you are testing on each and every of these edge cases.
I would say different different scenarios and everything.
Okay.
So now let us test it for two.
And I am seeing to get zero now.
And I feel that is the wrong answer.
I should be getting one as an answer right.
So we saw that how our solution was working fine for two of these test cases, but it failed here,
right?
So now let us deep dive and see exactly what would be the problem.
Let me first explain you that here only and then we will see the same.
So what would have happened is that when I would have called my particular function this particular
function, then I would have sent this 3 to 5 2821 and I would have sent two.
Now first we would have checked base case.
That is of course not true.
Then would have checked for this particular use case.
If L-1 of zero is equals to x, and then we would have made a call.
Right now when I made this call, I would have sent two, five, two, eight, two one and two again.
Now in this particular use case in this particular I would say call, what would have happened is that
L one is now this particular array or list.
Right.
L one of zero is coming out to be two.
And we say that okay, we are returning zero.
But one thing which we have missed here is one thing.
One very important thing which we have missed here is for this array.
This is the indexing this array or list.
But here this is the indexing.
So when I am actually slicing my list and sending it into the next function, then each and every of
this element is actually getting its index minus one.
Right.
So when I when you see this two it is having zero index.
But in the original array it was having index as one.
So when the slicing happens of course the index of your elements also changes.
In the next step it is going to move one step behind again.
So five actually this particular five sorry for this color.
Yeah one second.
So five is actually going to be coming on your zeroth index.
This is going to come on your zeroth index.
Right.
So that means that though we are right this function this particular function is right when it is returning
answer as zero.
The same index is actually not true with respect to our main function, or with respect to the array
which we send at our end.
Right.
So that means that we will have to handle this use case as well.
So let us see this once again.
I will let me show you this a little bit more nicely so that your understanding is clear.
So pay attention everyone.
What I exactly want to tell see when I am calling the first index of a number.
Now of course the function gets created and I am calling it.
So this is the full list as of now.
And this is the indexes pay a lot of focus on this particular indexes okay.
Now when I make the next call let us make it for the base call.
So this is what I was saying when I make the next call.
If you will see if you are going to see this is how we are.
Shifting our particular elements in our list.
Right.
So the index is actually reducing by one.
So for this particular function whatever is at index zero it is in index one for this particular function.
Right.
So that means that whatever answer we are getting from here, we cannot directly return that.
We cannot directly return that because that's the problem.
That's again a problem.
We have not written our function or we have not done work, done our work properly.
If because if you will see in our work we have missed out the use case.
If, let's say what if we are in any intermediate function or something, right?
So when it is getting returned, that is giving us a problem.
So let us solve this everyone.
Let us try to solve this.
The simple solution for this, a simple solution for this can actually be that whatever index we are
getting, whatever index we are getting, let us add one to it.
Right.
So for example, if we have some function like this.
So whatever is zero here, whatever is having a zero index here, it will be one here.
Then it will be two here.
Similarly it will be three for this particular function.
And that will be the final answer right.
So let us try this once and see if it is able to solve our answer.
Solve our I would say problem or not.
So I am what I am saying is return whatever I am getting from below plus one and let me run it now.
And this seems to work now, so I am able to get my answer.
Okay, let us run for three as well.
Three it is.
I am getting zero, but now let me run for ten and ten.
I see that I am getting zero.
Oh sorry, I am getting six.
Now what was the mistake here?
We have to actually check one more thing.
What if the element is not found?
What if the element is not found and minus one is getting returned?
Then don't you think that it is a problem?
Because we are.
Even if the element is not found, even if the answer is minus one, we are still adding one and returning
the answer upwards.
And that is a very big problem, right?
So what is going to happen there?
I can just quickly show you that.
If I get x ten.
So for the purpose I am just reducing this array to let us say this length so that it is easier to visualize.
Okay.
We will be able to see all the functions as well.
Pay attention everyone.
So we are going to make the call.
We know that we are not going to find it right.
So let us go to the very last I would say call.
Now pay attention if this if it would have been two okay then yes, it is zero here and it is three
here.
We would have added the three with three.
In these three functions we would have added plus one.
And of course we would have gotten our answer right.
But here there is a problem right.
It is ten.
Now when we see for ten the base case is going to get hit.
We are going to return minus one.
And without even checking, without even checking, we are returning everything.
Right.
So see this value, this value which should have written minus one.
This is going to again get added by one and we are returning the same thing.
So that is again a problem right?
That is the reason we are getting three here.
So that is a very very big problem.
The fix is very simple.
See the understanding is to find these small small problems.
And that is why I am making sure that I am moving incrementally in front of you so that you also understand
this function.
Now, solving this is of course very easy if what I would say is if my function or let me just get Answer
from recursion.
It is like this.
What I have to check is if answer from recursion.
If it is minus one, then I have to just return it as it is.
But if it is not the case, else I will just return whatever answer I am getting from recursion plus
one.
Now this will actually handle all the cases.
This is going to handle all the cases and we can see the same as well, right?
So pay attention everyone.
See.
So actually let us say if you see one minus one and zero so two is coming at 110 is not there.
So minus one and first is coming at okay.
Let us see.
It is.
Oh sorry six.
Yeah.
So that works fine right.
I write it at zero.
But yeah it is coming at six.
So I think that works for all the use cases.
Again I have shown you all the small small mistakes which you can do in these types of questions.
Please make sure that you are actually checking your answers and try running your code.
So you are making all these things which I am showing you by drawing on the screen as well as through
Python Tutor.
Just one advice don't directly jump to Python Tutor.
Please first, make sure that you are able to draw on your notebook or somewhere first and get the answer.
Or get the mistakes at your end first.
Okay, because that is something which will help you to solve the question in any interview or I would
say any particular platform.
Whenever you are attempting them.
So with that, I hope the first index is clear.
The recursion tree I kind of already showed in front of you, which you can make as well.
So recursion relation is also pretty easy.
So I hope this question is clear.
Now I would like you to test out this last index of an element.
Of course that is very easy as well.
So I will just give you the in this particular notebook only I will just change the this thing.
And you have to write this function.
Okay.
So you have to write this function.
Now I believe that this will be pretty easy to do.
This will be pretty easy to do.
The all the things remain the same.
If you have understood first index of an element properly, then last index of an element should be
a lot easier to write if you have.
If you take care of all the things which I have actually told you while solving the thing.
So I expect that you will be solving this thing on your own, and I will be providing you with the solution.
Please make sure that you are covering all the use cases, all sorry, all the test cases, and also
make sure that you are drawing the recursion tree when you are writing the solution.
Okay, so thanks a lot everyone.
With that, we are actually very close to covering all the concepts in recursion and arrays as well.
In the next few videos, I'm going to solve a little bit variation of this particular problem and I
will be teaching you.
I would say many things which we have to take care when we solve these I would say list, array and
list and recursion kind of a problem which will help you to be a better coder or write better code in
in and all.
Okay, so please make sure that you solve this for last index of an element.
It should be pretty.
I would say simple to right now because I have already discussed in depth about first index.
Just make sure that you are following all the steps as it is.
Okay?
So thank you everyone!
Let's meet in the next video.