Hi, everyone.
So in this video, we are going to solve this question, find the lost index of the element so it will
be provided to you and say the element of it is five, five, six, five and six.
And the indexes are.
So zero, one, two, three and four.
So what do you need to do, given a value to the value of excess five?
So you want to find five, but you need to retain the last.
So five is present here, here and here.
So but I need to return the last index.
So the correct output will be three, four, five.
The correct output will be three, four, six, six, spread into the next two as well as at index four.
But since I need to return the last index, I will return four.
And similarly, if I want to find out then.
So 10 is not present inside this area, so the output will be minus one.
So I hope you understood my question so we can easily discussion with the help of loop.
So how is all this question with the help of all the security we need to find out the last.
So we're finding out the last.
Next what we will do, we will it over the area from and to start, we will trade over the area like
this.
We will start protecting the area from and and we will trade in this fashion.
Simple.
So as soon as you will find the value, as soon as you are able to find your value X, you will return
the index.
But since we are discussing about the equation, so we will solve this question using recursion.
So if you remember the last video, we saw the question first, and so for finding out the first next,
I was traversing the area like this as soon as you will find the element, Yolla done so for finding
out the next.
But I will do I will traverse my area like this.
So as soon as I will find out the value, I will return the index and I told you for iterating over
the area using recursion.
We will take a variable.
So what?
Finding out the first index I took of it, I will start from index zero and I will reach at this index.
And similarly, I will do exactly the same approach.
I will start from this index.
I will start from index and minus one.
Then I will over the using recursion.
And if I reach this index, so this index is minus one.
So if I reach index minus one, that means value not found, value not found.
And similarly if I reaches and so value not found.
So this was the case for finding out the first element.
So this is finding out the first index and this is the problem for finding out the last index.
If you want to find out the last or next, the value of I will start from minus one will move in this
direction.
If the index I reach is minus one, that means value not found.
So this isn't next zero and this is an next minus one.
So let's write the code.
So what we'll do, we will return integer because we need to return the index.
The name of the function is lost.
Next, we will take editor's input.
We will take how many elements are there.
We will take the value which we want to search for and we will take I associate the name of the function
is Lost year next.
At a number of elements is faith, the value of, let's say I want to search for food and I have to
start trading from the end, so what is the end of next five minus one?
So an index is for.
So in order to write the code, so we discussed that if I reaches minus one.
So when will I reach minus one?
So I will start reading.
So this is in mix and minus one.
So I am passing the last index and it is of five.
So the last two, Nexus five minus one.
So I am writing like this.
So if I had this index, this is minus one index, this is your next.
So after searching the entirety, if I reach this index that means the value is not present.
So if the value is not present, I can return in value the next.
Let's say I am returning minus one.
So minus one is an invalid index or you can return minus five.
Minus five is also invalid index.
So for example, I am returning minus T.
If I not found I will return minus three, otherwise I will check.
So if Yafai is equal to X, so in that case I can return I.
So basically I will start outrating from the end.
So if I reach Index I and this index value X is present for the first time from the last, it will be
present for the first time.
So I will return this index.
So I will return, I am returning at the rates but I need to do, I need to do a minus minus so I need
to come one step back i.e. minus minus means I will pass i.e. minus one.
So I will call the function there done.
Next.
Eddie and Alex and I need to move one step back, so I will do I mean, this one and I think our goal
will work so for this thing.
So I want to search for food.
So the last two Nexus food, so outputted before.
So the output is four and four is the right output.
So let's take let's create a new every five, five, six, five, six and seven.
So let's say I want to search for six.
So what is the last element?
How many elements are there?
One, two, three, four, five and six.
Six elements are there.
So the value of any six and the last element will be five in the next five.
So what I'm doing here is total number of elements present day six.
I want to find the next four six.
So this is zero.
One and next to the next three.
And the next four.
So output should be index for.
And the value of eight is six minus one.
And maintenance on basically so the correct output is I should get four.
I should get four.
So I am getting four.
And now let's also test our function for the value of life.
I want to search for five so next zero index on an extra index three.
So output should be three.
Output is coming out to be three and let's test for invalid values.
I want to search for ten.
So output should be minus three because that is not pleasant.
So output is coming out three, minus three.
So our function is working and how it is working.
So I told you, I already explained to you many times.
I am reading from the end to start and as soon as I will find I value.
As soon as I will find the value, I will return the index, as soon as I find the value, I am returning
the index and for moving back, for moving one step back, I am doing I am minus one.
So how it will work for this, Solich?
Let's write another function, so five, five, six, five, six and seven, so I is standing at this
index, so what I will do, I will check so I is not equal to minus one.
And let's say I want to search for five, I want to search for five and seven is not close to five.
So call that equation and do eight minus one.
So call that equation and it will remain same and same and same.
Exact same.
So they will remain same.
Five, five, six, five, six, seven.
Only the value of I will reduce my rent, so I will come here.
So this is Avenue Y, so I is not minus one and five is not present.
It is six.
So again, I will call the recursion.
So five five and it will remain the same.
Number of elements will remain same.
So I will move one step back, i.e. minus one.
So I am moving one step back.
This is I so I is not close to minus one and five is equal to the value which we want to search for.
So I will return the next zero one two entry.
So the value of I is three, I am returning the next, so I will return three.
So whatever value you are getting you are just likely returning.
So I will return three and I will return three.
So that's how this function is working.
OK, so what you can do.
So instead of using this variable you can use this variable and also how so you just need to move one
step back.
Right.
So for moving one step back, I am taking this variable and I am doing I am minus one.
So what you can do, you can use this variable and and you can do and minus one.
So what I want to say is let's copy this function.
There's no need of a Nixey, you can also write like this.
I'm not passing a Nixey.
So this is next to.
I will use the variable end because the variable is basically unused.
OK, in this core difficulty, I'm not using the variable in.
I'm not using the DeBelin, so instead of using the variable, instead of creating a new variable,
I we can use the variable and so for moving, so for I am doing a minus one.
So similarly for and what I will do, I will do and minus one.
So let's remove it.
I will do np minus one.
Simple.
Similarly I so I was the last of the next.
So now the last year Nix's and minus one.
Similarly, when an.
Becomes zero.
Urata is empty, so I think this will work and decis and minus one.
So the working is exactly the same.
See, the working of dysfunction and dysfunction is exactly the same, but bertan dysfunction, I am
not using the variable in.
So we to create this very melee when you have a variable which is unused, so I'm using the variable
N and I just modified.
Discord, so let's see whether this will work or not so lasting next to.
Lasting next to.
And you do not need to pass the value of I just need to pass any number of elements, so let's have
a good.
So this is last and next to.
So Dan is not president insideout, that's why we are getting ministry.
Now, let's give the value so I want to search for five, so the correct answer should be zero one,
two and three, correct output should be three.
So five is present at the next.
So my goal is working.
So basically what I want to say is, see, in this function, I'm not using very Baalen.
So there are two ways.
So first base.
So the first phase removal, since there's no use of N. So why we are passing and so the first phase
we can remove.
And so this is our ability to second base if we do not want to remove any.
If you do not want to remove, then you should remove a.
Then you should remove a.
So I am removing I and and is doing exactly the same work which I was doing, so I followed this second
approach.
I removed I very.
I am removing a variable that is basically remove variable and because it is not used.
So how it will work.
So let's see.
See, the basic approach of solving this problem is very simple.
You need to start trading from the end to the start.
So what is the lasting next?
Last year, Nixon basically and minus one.
So what I'm doing here is basically.
See, we can make the we can work out like this.
So what I'm doing, I am checking for this index.
So this infixes and Minocin I am checking present here are not exits, present or not, if X is present,
written down next, if X is present, effects is present, I am returning the index.
Otherwise, what I will do, I will call the recursion on this area and the question will give me the
index.
So I am calling the recursion on this area so that it is E and the number of elements and minus one
number of elements is then minus on Arazi and I am passing the value X, so I am calling the equation
and the equation will give me the right answer.
Simple.
So that's how it is working.
So let me try it in the score for you, for one example, so that you can understand.
Let's write in for this.
So five, five, six, five, six and seven and.
I will check for this.
So you want to search for five, so the value of X is five.
We are passing a number of elements to the number of elements is basically one, two, three, four,
five and six.
So six.
So first of all, I will check that is not empty, it is not empty.
Check the last index.
So I will check seven is not close to five.
So this is next five.
OK, so this is N minus one.
This is and minus one.
So next zero.
Next one.
And next to the next three.
And next four.
So I am taking seven is not close to five, so on this, Mollari.
Five, five, six, five and six, number of elements, five, the value of access five.
So now check.
So size is not zero and six is not close to five, so call on the small area.
So I am calling on the small area, six and five.
This is my new number of elements for the reducing the number of elements and I want to search for five.
So check the last element.
So five is equal to five and size is not zero, size is not zero.
And the last element we are checking for the last element.
So if are able to find the next, you will return.
So we are done and minus.
And what is the value of and is four.
So you are returning four minus one.
You are turning three and three.
Straight answer.
So you will return three.
So basically this small answer, this is returning three.
That means X is present at the next three.
Whatever value you are getting from the equation, you will likely return.
So I will return three.
So basically value five is present at the next three.
That's why you are getting three from this area.
So whatever value you are getting, you will return.
So I will return three.
Simple, so that's how everything is working and this is a very simple case, so let's try to right
let's take one example when we will hit the case.
So let's take a small example, one, three, three and four NAMAROFF elements for let's say I want
to search for five.
So five is not present in the area.
So the correct output will be minus three.
Let's see how we will encounter minus three.
So check size is not zero.
Now check for the last element.
So four is not close to five.
So our area.
So call on this, Mollari.
So size is not zero.
Check for the last element.
So three is not close to five Colonus Mollari one entry number of elements to societies, not zero.
For the last element, they are not equal.
A smaller number of elements, one size is not zero for the last element, so one is not close to five
on the smaller number of elements will become zero if the number of elements if the site is zero, you
will return minus three.
So you will have done minus three.
And whatever value you are getting from the equation, you are just likely returning that value.
So return minus three to done minus three there, done minus three and we will return minus three.
So minus three means not present.
So that's how you can solve this question.
So there is one more way to solve this question.
So how we are solving this question.
We are iterating.
So we are iterating, we are checking.
First of all, we are checking whether the element is present at this index or not.
And then we are calling that equation on this ETTY so we can solve this question like this.
So in our current approach, we are moving from and to start.
So let's discuss one more approach in which we will move from start to end.
So what I'm planning to do is so first of all.
I will call commission on this area, so the question will give me a valid next.
Or it will in the index.
So in the next, let's say minus one, minus one means not found and valid index means yes, it is present.
So if it is not pleasant, if the value is not present, then what I will do, I will check is the value
present here.
So then I will check if if zero is equals two.
So it will not be zero, it will be I.
So I told you, when you want to add it over the array you will use it next time.
So for our trading.
What the idea I'm using index so I will check this index.
So I will check if Elfy is equal to X is equal to X..
So in that case I can return.
In that case I cannot deny.
In the ls part I will return minus one.
So this Eddie.
Is returning minus, that means when you're not found and values not present at this index also.
So I will return minus one.
If value found, if I'm getting a valid index, basically not minus one.
So if values present here, then also I need to check for this.
So it is it is possible, for example, you want to search for seven.
So seven is present inside that eight and seven is also present here.
Seven is also present there.
So you will return a valid index, but we cannot just dial it on the index.
First of all, you also need to check whether this index is containing that value or not.
So if this index is containing that value.
I will return that value, otherwise I will return.
The value index, which we got, otherwise I will return that valid index simple.
So now let us write the code.
So this is our way of solving.
So end.
Last next t, so I will start out reading from start to end.
Eddie, I need N and I so I will start reading from start to end, so this is lasting next three.
And I need to pass I need to take X, has input X and I.
The value of five will be zero, the value of zero.
I will start our trading from start to end.
So this case is very simple.
Since we are dating from start to end.
So if I reaches here, if I become Sequoia's twin, then the value is not present, so I will return
invited and next I will return minus one.
So if I is, then that means value not found.
If value not found, you can return minus one.
Otherwise result need to do.
First, we will call that equation on this, Eddie.
So where does this Eddie so this idea is E and and I plus one.
So let's call it a collision on this, Eddie.
And it will return index, so index in the remaining at.
Let's call Lastra next three.
Eddie.
And.
X and I plus one, so I'm calling the discussion on this Eddie index in the morning so it can be a valid
index, it can be invalid index.
So if this is an invalid index, OK, so there is one problem and it is a problem.
So if you are getting index and we need to return the last valid index, we need to return the index.
So we do not need to do all this.
This is wrong.
OK, you do not need to check for this because we need to return the last index.
So if you are getting a valid index, you will likely return, you will directly return, otherwise
you will do this step.
Sorry for my mistake.
So if the next remaining.
It is minus one, that means I'm not able to find the value.
So if the value is not present, if the value is not present and so I decided, let's check for this
index.
So if it is possible, that equals, of course, X, that is the criteria.
That is the situation.
I will return a.
In the last part, you can simply done minus one.
So when we will return minus one, so first time checking the value is not present.
The value is not present inside the city.
I'm getting minus one, I am getting minus one because the value is not present and value is not present
at this position also.
So basically, the value is not present in the.
So I am returning minus one and India's part in the part.
If this is a valid index and since I need to return the last index, so I will likely return this value.
So in the early part.
You can buy Claratyne.
The value added, I think our function will work last year, next three.
Six, five and zero, so let's call this function.
OK, so if I.
What is the error here?
OK, so there's a problem.
So the problem is this is basically a.
So the output is three, so let's see.
Let's again.
So they are history and let us see why that is correct or not.
So five, five, six, five, six and seven.
So this is a big zero, next one and next to the next three and next for the next five, I want to search
for five.
So this is five.
This is five.
I need to return the last index.
So last next is three.
So I am returning three.
So a function is working.
So if you want, I can write in this code for you.
So let's bring in how this court will look.
So let's Grayden, for this example.
So this is five, five, six, five, six and seven.
So you want to search for.
Five.
The value of fire is initially zero, so the value of fire is initially zero.
So what they will do, you will check.
So I want to know, so you will call that equation so that it will remain same.
So it will remain same and same, exact same, you are just doing a plus one, so everything will remain
the same, just a plus one.
So initially I was here, now I is here.
So the value of is one.
So what we do.
I is not close to and so I will again call that equation, we will again call that equation.
This is seven.
So again, call that equation five, five, six, five, six and seven.
So this is I the value of Y is two.
So two is not close to end the value of this five and we want to search for five.
The value of X is also five.
So again, we will call the Cudgen five and it will remain same six, five, six and seven.
OK, so this is basically the value which we want to search for.
And the number of elements, number of elements is basically one, two, three, four, five, six number
of elements.
Sorry, this is X.
So now you can check the value of I know.
The value of history, and he's not close to end, so you will call one more time.
Five, five, six, five, six and seven.
The value of I use now four, so I is now here.
So you will again call that equation.
Five, five, six, five, six, seven.
So now the value of is five.
You are at this position you would call litigation.
Five, five, six, five, six, seven, and now the value of I will become six.
So now I is here, so you will return minus one.
So basically this is returning minus one.
So the value of andexanet remaining is minus one, if it is minus one, check for the correct position.
So the value of is five for dissuasion.
So seven equals equals five.
I want to search for five.
So no.
So ten minus one.
So it will be done minus one.
So we are getting minus one from Dedecker, so we are getting minus and from the equation, we are getting
minus one check for the current position.
So current position is containing six and six is not equal to five.
I want to search for five, so I will return minus one.
So this will return minus one.
So I am getting my lesson from director John.
So I am getting my innocence from dedication, so check for the current position now, so current position
is containing five.
So five is it to five, so I will return a what is a I is three, so I will return three.
So I'm getting three from the so the value of andexanet and remaining estie, I'm getting three from
the equation.
So if you are getting it next, which is valid, so you will return always certainty.
If you are getting a valid index, you are just back later to return.
Three.
If you are getting a valid index, you are just likely returning return three and three is the right
answer.
So that's how everything is working.
So let's test the case when the value is not present.
So if the value which I want to search for is 10, then is not inside already.
So output should be minus one and we are getting minus one.
So basically our goal is working.
Descended from this video, I will see you in the next one.