Hi, everyone.
So in this video, we are going to solve this question, so we need to find out.
We need to find out the number of ways to reach and from zero.
So basically, these are the steps.
So I want to reach step and starting from ground and ground zero.
OK, so ground zero, I want to step in and I can take a jump off.
When I can take a jump of two, I can take a jump of three.
So from here I can take a jump of one, I can take a jump of two and I can take a jump of three.
So we need to find out how many number of bees are there, for example, if we want to reach three.
So number of ways to reach trees, basically, you can take a jump of one, one and one.
Similarly, it can take a jump of one, then take a jump of the first trigger, jump of two, then take
a jump of one and one more directly.
Take a jump of three from zero DIKLA take a jump of three.
So to reach three from zero.
I have four of his.
OK, so for viewers out there, if the value of an E t.
So I hope you understood my question.
So given the value, often you have to tell me how many number of these are there.
So this question can be easily solved with the help of recursion.
So what resurgences?
See, you want to reach an.
You want to reach in and you can take a jump of 120 so you can reach in from minus one.
You can reach in from Minnesota, you can reach GNR from ministry.
So the number of ways to reach in will basically be the addition of these three to a number of ways
to reach and is basically number of ways to reach and minus one, because if you are standing at 10
minus one, you will take a jump of one plus the number of ways to reach.
And minus two, if you are standing at minus two, you will take a jump of two and you will reach and
if you will take a jump of one, then that will be counted in the number of ways to reach and minus
one, which we have already counted.
Similarly, number of ways to reach.
And minus three, if you are standing at then minus three, then you need to take a jump of three to
reach and OK, you do not have any choice.
You will take a jump of tea if you will.
See I am standing at then minus three and I take a jump of one.
It will take a jump of one, then it will be counted as number of ways to reach and minus two and it
is already there.
So a number of ways to reach and is basically number of ways to reach and minus one number of ways to
reach and minus two and number of ways to reach and minus three.
And the addition of these three will give you the number of ways to reach.
And so, for example, let's take one example.
So a number of ways to reach one is one.
Take one step from zero.
Number of ways to each two is basically I can take a jump of 21 and I can take a jump of the number
of ways to reach two is two and above.
We calculated that number of ways to each three is basically four.
Simple.
And what is the number of ways to reach zero?
Basically you are standing at zero and you want to reach zero.
So there is only one.
We do not do anything.
So now let's try to find out how many number of ways to reach for.
I want to reach Stepha's, how many of these are there?
So you want to reach for so you can reach for and the gems allow this one to entry so you can reach
Plumtree, you can reach from two and you can reach from one to four three.
You can reach from two, you can reach three from one and you can reach three from zero.
Simple.
So what we will do, we will make it as a base case.
We know a number of ways to each do is to.
So I will return to a number of ways to each one is basically one.
So I will return one number of ways to reach zero.
And you are standing at zero.
So do not do anything.
There's only one way.
So two plus three plus the number of ways to reach three is four I will return for.
Number of ways to reach do is to so I will return to a number of ways, each one is basically one,
so I will return one.
So what I will do, I will take the additional four plus two, six and seven.
So a number of ways to reach 47.
It is seven simple, so a number of ways to reach for this number of mysteries, three plus number of
ways to reach to less number of ways to reach one.
So a number of ways to ease trees for a number of ways to each tool is basically do a number of ways
to each one is one.
So four plus two plus one is seven.
Seven is the right answer.
And what are those ways you want to reach for.
So one, two, three and four.
One, two and four.
One, one, three and four.
Similar to one one.
And one entry, three, anyone, one, two, one, two.
So let's count one, two, three, four, five, six, seven.
So seven ways are there to each step four.
So this question can easily be solved with the help of the.
So what do you want to reach and write?
So to reach in, you can reach from minus one, you can reach from minus two and you can reach from
minus three.
So what you will do, you will apply recursion for then minus one applied equation, applied equation.
And of course it will give me the number of ways to reach and minus one, let's call it X and of course
it will give you the number of ways to reach and minus two, let's call it Y because it will give you
the number of ways to reach and minus three, let's call it Z and with a number of ways to reach an
addition of X plus Y plussed.
Simple.
So let's write the code.
So in, let's say, the name of the function, this count is.
So I want to reach in from zero what will the case, because it's very simple, if you are standing
at zero.
Or us standing at one.
So in that case, do not do anything.
So there is only one way I will return one.
Otherwise, what will my answer?
My answer will be.
Number of ways to reach and minus one, plus number of ways to reach and minus two.
Plus, a number of ways to reach and ministry.
And I think our function will be OK, so let's test our function.
So integer and it's taken Porten.
And let's call the function see out.
Countries need to give an.
So our function will work, but there's one slight problem with our function and what is that slight
problem?
So let's take this example.
So, for example.
Number of ways to reach to the value of any number of ways to reach to this one and one and to take
a jump of two or take a jump of one on one, simple.
So if the value of it is to let supply and so who is not and is not and is not one.
So we will call on one.
We will call on zero and two will call on minus one.
So this is a case we know the answer is one, this is a base case, we know the answer is one.
But you are calling one minus one.
So what will happen minus one is not a biscuit, so it will call on this, it will call on minus two,
it will go on minus three, and it will call on minus four.
And similarly, these are not biscuits.
So, again, it will call.
On three.
So Tuticorin, Colin Taseko also, it will call on minus three, minus four, minus five, similarly
minus four, minus five, minus six and minus five, minus six, minus seven.
So it will continue.
OK.
So this will become infinite.
So what we need to do.
So we need to make it as a base case, we need to take it as a base case, OK?
So we need to make it as our base case so that we do not reach minus one.
So we need to make it as our base case.
So it is compulsion here.
We need to take to as our base case.
So this is the first option to handle the situation.
The second option is to handle the situation is basically you can write one condition.
If the value of it becomes less than zero, return zero.
So this is the second we saw minus one is basically zero, so I will return zero, so I will return
zero from you so you can make two as your best case or you can use this condition.
So what I want to say to is calling on when we know the answer to is calling on zero.
We know the answer because both our base case, but who is also calling one minus one?
So I told you, there are two ways to handle the situation.
First, make two as our base case so I can write one condition here.
I can add one more base case if the value of any two I know.
What is the answer?
Answer is two.
I know the answer.
So I can make it as a base case, so then I will reach out to I will directly answer to Sorto will act
as a base case.
The second situation is we can right this condition if the value is less than zero.
So basically, this is invalid value.
OK, this is invalid value.
So for the invalid value, my answer will be zero.
So if you recall, and minus and minus one is invalid value, so I will return zero from here.
So you have both options.
You can make it you can make a call like this or you can make a call like this.
It's your choice.
So first, which is basically you can write like this, you need to make to your best case.
And we know the answer.
Answer is to.
Answer is.
So now your code will work, so let's test our function.
So a number of ways to reach three is basically Ford, so Ford is the right answer.
Similarly, the number of ways to reach forty seven, so a number of ways to reach four is seven, so
seven is the right answer and we already discussed it here.
So a number of ways to Utah is basically seven.
So this is the first way of making your record correct, the second way of making your record correct
is if you do not want to make it as your best case, what you can do, you can add one condition.
And what is that condition?
If.
The value often is basically invested.
If the value of wine is invalid, then in that case, that answer will be zero for anybody to value.
My answer will be zero.
I want to search for for no obvious reason for this seven, it is working.
Similarly, I want to reach out to a number of races, too, and it is working, so you have the option.
You can use this or you can use this.
Or you can use both of them.
So this is it from this video, I will see you in the next one.