Hello all and welcome back.
So in this video we are going to continue our discussion on the question which we solved in the last
class.
And now we are going to solve the second part of this question.
So the question was like this.
We are given a particular number n.
And first we have to print one to n, which we did in the last class.
If you have not seen that request two, please see that once and the next is printing n2 one.
So now let's actually write a recursive code to print numbers from n to one.
Okay.
So first let me explain you that a little bit.
This time what is happening is that we have to print numbers from N to one.
And I will be actually getting my n is equal to five here now.
Till now what we have been doing is we have been following the PMI and that is something which we have
to continue doing so.
But this is some how we have written the code.
Right.
So we have to first write our base case.
Then we used to make a recursive call induction hypothesis recursive call.
And last we used to do whatever R work is there.
So we have to assume in the second part that everything has been done.
And then using R work we need to either prove or we have to basically do something which we want in.
The last question is when we saw that we have first written the base case, which was n is less than
or equal to zero.
Then we were returning in the recursive call, we were calling the same function with the smaller input
and assume that everything is going to get done.
And when you used to do R work of printing N, which was five in the last case, right now, one thing,
if you will see here, if we try to do this question in the same way, what actually will happen.
So let me just copy this particular thing.
Let me just make it print n to one.
So base case will be the same right.
Yeah.
So pay attention.
I think this should be clear.
That base case is going to be the same.
Right.
But this time I have to print n first.
So earlier while I was printing five first, this time I have to actually make sure that I am sorry
one.
First I have to make sure that I print five first.
So if I actually think of this, if I actually think of this, if I print my n, if I print my n here,
which is five, right?
Because n see, you have to understand that what input you are having in your particular function.
In this particular function I will be getting five as my input.
So I have n which is the first thing which I want to print and if I am able to print and then call my
function directly on n minus one.
I think that this will take care of everything.
So I have just moved this print n line above.
Before making my recursive call and if I run this now, I will be able to see that everything is.
Working and I'm able to see the result as I want.
Right?
So you have to understand one thing that.
What I have just changed here is that instead of work, instead of actually calling my recursion.
Function, I have just done my work first, and then I have asked a question to do exactly the same.
Thing.
Okay, so in this way, what?
The takeaway is that you can actually write a code where these steps.
Okay, they are interchangeable or you can do any of them first.
But that would actually also depend on the type of question.
Now we are of course going to do lots of questions there.
You will see that.
What exactly will this have an effect?
Many questions will be there where no effect will be there.
Okay.
So let us say if you are finding the sum of digits of a number right.
You can do any of these things.
You can take a sum and you can take that.
But that should be fine, right.
But in this type of question you will see that if we are working on R code first, or if you are printing
something, then that's gonna come first in the output.
In this case, five was coming out first on our output screen.
And as we make the recursive call it's gonna go till one.
Okay.
So with this I would say question.
I hope I am able to explain you that this is the case which happens, that you will be able to actually
exchange these calls.
Okay.
So you can make the base case.
And then you can call either make the recursive call first and do our work.
And that would be based on question.
Here it is.
They are not interchangeable as such.
It's just that you have to see the question in this particular question because we are doing our work
first.
The reason was we want to see five the value of n.
First we have to go from n to one.
Okay, there is just one more concept which I would like you to explain here, and which is the main
reason I chose this question.
So there is a concept of head and tail recursion.
So let me write down the same.
Head and tail recursion.
Now what exactly is head and tail recursion?
Is that in the first question when we were calling when we were making a recursive call first.
So recursion Call.
Okay.
It is actually the example of head recursion.
So just like in a human body.
Okay.
Of course we are.
So see this is all the work which we say that we are doing the work and head is basically when we are
calling recursion first.
Okay.
So this is when it is known as head recursion in a similar manner.
Okay.
In a similar manner when we have when we do our all our work first and call recursion, in the end it
is known as tail recursion.
And that is something that is of course something which we have done.
In the second question, where we have call our recursion in the end, recursion in the end, and it
is known as tail recursion.
So I will just repeat it and make it a lot more clear.
See we have these three steps.
Always.
The base case is the first okay.
Then we have the recursive call and our work.
If you do, if you call recursion first then it is known as head recursion.
And if you are actually calling recursion after doing all your work, then it is known as tail recursion.
Okay.
So in the end, if you are calling the recursion then it is known as tail recursion.
So you have to make sure it is not that you can use like either of them.
I will be discussing more on that as we move forward via questions, but based on question, either
we have to call recursion first or we have to.
I would say call recursion in the end.
Like for example, in this question, you see, it is very easy if you call recursion in the end.
And in this question it was easier if you are calling recursion in the start only, right?
So based on the question, we are using it though majority or I would say lots of question will come
where you can either call either use head recursion or tail recursion, but that will become clear as
and when we will solve those questions.
And I will make sure that I am explaining or giving you the solution with both of them, so that you
have the clear understanding on, uh, like where you can use what and you will be able to code better
using the same thing.
And of course, this information is lot crucial and you should be aware of that.
Okay cool everyone.
So I hope this is clear.
Now in the next class I'm going to give you some homework which you have to make sure so I can write
the questions.
You will find the files okay.
In that file the question will be properly explained.
So in the assignment part now you have to do two questions.
The first question which will be given to you is some of digit of a number.
And the last question, which will be the second question which will be given to you is power of a number,
where you will be given a power and a base, sorry, and a base and an exponent.
And you have to tell.
So for example, if this is three and this is two, the answer should be nine.
Okay.
So these are the questions which you will find in your homework section.
So please make sure that you try them out.
Please try to solve them using recursion.
Also make sure that you are following all the steps moving forward.
We are going to start recursion with arrays, where again I will be explaining you and actually doing
lots of questions so that your thought process becomes clear and you will be able to do even hard level
or leetcode hard or the question.
We will also be doing questions which are asked, I would say in companies like Amazon, Google, so
that you get a feel of what exactly are those questions and where recursion is actually helpful in solving
or I would say writing a better code for that particular solution.
Okay, cool everyone.
So let's meet in the next video.
Meanwhile, please make sure that you are solving this question on your own by using recursion.
Thank you.