Hi, everyone.
So in this video, we are going to discuss this question, find the first index of the element so it
will be provided to you and let the elements of the adays.
Five, five, six, five and six, and these are the Nexus zero, one, two, three and four.
So the question is there will be element to this element.
Let's say the value of X is five.
So five is present at index zero at the next one at the next three.
But you need to return the first index.
So the output for five should be zero.
Similarly, if the value of sex is sex, so it is present at an next as well as at the next four buttons,
we need to return the first index.
So my answer will be to.
Let's say you want to search for 10, so 10 is not present inside this area, so my output will be minus
one Y minus one because minus one is an invalid index.
So I need to return the index and minus one is invalid index so we can easily solved this discussion
with the help of phone number by loop.
So this area provided to you, you will just iterate over the area and as soon as you find the element,
you will return the index.
So using for Lopevi Loop to solve this problem is very simple.
So what we will do to make this problem, we will use recursion so using it to solve this problem.
So the question is also very simple to apply in this problem.
So how we can apply recursion.
So in the last video I discussed with you, I index how we can use our index.
So I index is very similar to the volume.
We will just iterate using the index.
So our function will be we need to return an integer.
And these are basically index function.
I will take that as input, I will take a number of elements.
I will take element which I want to find out and index.
So initially, the value of five will be zero.
I will pass the value of I zero from mean and what they need to do.
So envelope what will do we will I treat using Nixey and as soon as you will find out the value you
will return the index.
So we do exactly the same.
So if you are able to find out the value.
You need to return the index, so I will return the index, return I.
Simple otherwise, check for this position, if not found, go to next position and check, so for going
into next position, I will call this function.
And it will maintain the same X will remain the same.
I just need to do a plus one to move to next position.
So we are doing exactly the same approach, exactly the follow up approach using recursion.
OK, so I will just I read I am meditating over Director Armatrading over the area using recursion.
I am I reading over the area, so there are two ways for outrating, first, using the for loop or develop
who I did over the area.
So in this problem, I am using recursion to iterate over the area.
So frustrating over the array we need index I.
So I hope you got the question and how you can solve this question.
So let us write the code.
So what will do?
Let's say the name of the function is that there will be integer because we are going to return the
index and then the other function is first, first and then the first.
So I will take it as input, I will take the number of elements cise.
I will take the value which I want to find, and next, I do it right over the area.
So basically it is very simple.
So follow up what you will do.
So if you are using for low priority over the area, what you will do.
So you will start from zero, you will trade, and if you are not able to find a value, you will reach
next.
And so if you reach index and that means the value is not present and you will return minus one minus
and miss in the index, basically the value not found.
So we will do exactly the same.
If.
I equals sorry if.
If I reaches if I reach the last index, basically, and if I reach index, and that means Vaillancourt
not found, if we had not found, I will have done minus one simple.
Otherwise, I am standing at the next site.
Let's check for the next guy.
So if if I equals X.
So that means excess at the current index, so I will return the index, return I so envelope at will
do so then.
So this is the code for for loop equals zero elastin in AA plus plus.
If.
If I equals two X. You need to return the index, so I will return.
And if I will come out of the for loop, that means I reach then.
So if I reaches, then that means the value is not found.
So in that case, you can return minus one.
So that is how we can solve the problem with the help of Orlob, so we are doing exactly the same.
If I if I stiches and if I do Jizan, I am returning my Nesson.
I am standing at the correct position.
I'm standing at the next day.
So if X is present the next day, I will return.
I exactly like this.
Otherwise, if it is not present at the next day, we will move to the next index and we will check.
So for moving to next and next I do I placeless but here I will do, I will pass I plus one.
So now we need to search.
So now we need to search, we will move ahead and we will search, so Eddie and I plus one and that's
all.
So this is the complete goal.
So for moving ahead in the area with 80 plus plus so far, moving ahead for migrating, fighting over
the area, using recursion.
I will do I plus one simple.
So let's have a good.
Copy the function, Foster Nix.
Out foster niks.
Eddie size is five, let's say I want to search for.
For.
And I will be zero, I will start searching from index zero.
So let's say for this prison here also and for this prison.
Here also, so the correct outputs would be when the next one.
So for this president, the next one.
Let's consider one example when the element is not president, so I want to search for five, so five
is not present inside the third, but will be minus one.
So output is minus one.
Now let's search for three teens present at next to.
So please present at next to.
So our goal is working now.
Let's write a novel called.
How it is working, so it is exactly like the for loop you can see we are using for loop, we are just
iterating, we are just iterating over the area using recursion.
So let's get another caller on this example.
So I have one, four, three, four and four.
So a number of elements is five.
I want to search for for let's say I want to search for four.
So the output should be one.
So this is index zero, index one, index to index three and index four, so currently what is the value
of.
So this is the value of I.
I am passing zero.
So check, so I check.
So zero is not close to five, then check.
So one is, of course, two for no.
So I will move ahead, so I will come here.
So if you are not able to understand it like this, what will happen?
Because call one, four, three, four, four.
And this is a.
So, again, this is an excellent one, is not equals two and five and so forth is equal to four, so
Afie is equal to X, you've done I, so the value of phase one when so this function will return one.
And whatever value you are getting, you are just likely returning.
So I am returning one and one is the right answer.
Simple.
So basically, what is happening, this is your ready.
This is a value of zero you are checking.
Whether X is correct position at this position or not, if it is not present, then you are moving ahead.
So then we are moving ahead.
So we are checking at this position.
X is present or not.
If it is present, you will return.
If it is present, you will return the value of it.
If it is not present, you are moving ahead and then you are again checking if if present return otherwise
movahed.
So similarly, if the X is not present inside the area, so you will reach here.
And so if you're Richard Nixon, you will return minus one, so we will return minus one value not found
simple.
So this is it from this video.
I will see you in the next one.