We have seen that recursion is nothing but a function calling itself until the base condition or the
terminating condition.
So the base condition tells the function when it can stop calling itself and then start returning.
So that's what the base condition is all about now.
The wrong way of trying to identify the base condition is trying to visualize the whole recursive sequence
and then identifying when the function should stop calling itself.
Especially you will find this difficult in complex questions.
Now there is a better way of identifying the base condition for a recursive solution, and that is by
thinking of either the last valid input or the first invalid input.
Okay, so there are these two ways you can either think of the last valid case or the first invalid
case.
Now these two are pretty similar in that you could in some cases write either way the solution, or
in some cases you will find that one of these is more easier to implement.
But again, the point I'm trying to make over here is that to identify the base condition, don't try
to visualize the whole recursive sequence, but rather just think of the last valid input or the first
invalid input.
Now let's take two examples to understand what I'm talking about over here.
So over here we have two pseudo codes.
So this over here is the pseudo code that we have seen to find n factorial.
And notice that in this case the base case over here is the last valid input okay.
So you can see that over here the last valid input is one.
And in this case we are just returning one.
And the function stops calling itself.
Now we know that for example n factorial or let's say five factorial for simplicity is five into four
into three into two into one and one over.
Here is the last valid input.
The next input over here would be zero which is not valid right.
So at this point we have decided to stop calling the function and we are returning one.
So this over here is one way of identifying the base condition.
And the other way over here is to think of the first invalid input.
For example, over here we have a function that can be used to print the values from n to one.
Okay.
So let's say n is equal to five.
So the output would be five, four, three two and one.
Now over here notice that the base case has been returned as the first invalid input.
So the last valid input in this case is one.
And after one we get to zero which is less than one.
Right.
And this over here is the first invalid input.
So again you could you could write this in the other way as well.
Like you could tweak this and write it such that the base case is based on the last valid input.
But I'm writing it purposefully in this way over here to give an example.
Now these two things are very helpful, especially in complex questions to come up with the relevant
base condition.