I will create a stack another further so host a gets created this is known as the call stack this is known as a constant so of course the stack the call will keep on happening till the time the stack is not empty so as soon as you know the first time the function gets called the first time in the function gets called no so the first time when the function can call the array value equals to 1 2 3 and I value is equal to 0 so this metadata gets stored in the stack.


We can store something like this so corresponding to 1 the array value is 1 2 3 and the value of I equals 0 this is one record that gets stored in the stack okay and then as soon as I hit termination condition is not satisfied just again call sitacles to be 2 it again goes to the static - and now I mean the value of radius 1 2 3 and the value of I is equal to 1 so another entry gets added into the stack where you know it will the second time and when you offer a same 1 2 3 and I value equals to 1.


Again it calls recursively again it means to the third time or a value is 1 2 3 I value is equals to 2 again this gets added another entry in the stack 3 it is 1 2 3 and I when you go to write again it goes back fourth time at this time again the value of arrays 1 2 3 and I value 3 now right so again this gets created for the time 1 2 3 and then I values equals to 3 now as soon as I value is equal to 3 now it is the termination condition it is saying the return for the first time so as soon as it says the return the last entry this function value actually gets written this return is just for the last function call it is not for all.


As soon as it does that it did this gets to come out this comes out ok so this comes out so this entry gets removed ok so first what will happen this will get entry will get removed so this will get deleted this whole thing will get deleted and then it will come the after this stuff the topmost is 3 1 so I came to the third stage right I come to here this third Street right the Thursday the value is equal to two and I still have to do this thing so I go and print three first.


I print 3 here and 3 here and again as soon as it is done now everything is done so this will also get taken out so this will also come out of the stand now we move the topmost is now this one so it goes to this to this and here again this print function estimate so true gets printed so this part also now gets removed and now I am only left with the first one and once this first also printed this also gets removed so it's because of the stack you know is that actually whatever that came last will come out first and whatever that came first will execute in the end because of which this gives this a nature of a complete route you goes to the end then you start coming out and then only you are fully done so unless until this call stack is done you cannot complete the execution.


That is why it goes to step 1 it goes to step 2 then 3 4 and then again comes back to 3 then again comes to and then comes back this concept is very important ties if you understand this concept please if you are not getting it because the time constant I cannot keep on repeating but what you can do is that after this video is done you can always go back and rewind and please listen to this concept trust me 99% of people don't realize what this concept is and they are only using 30% of how strong the recursion is if you understand these things and if you can use 100% I think you know your concepts we really create in the recursion now that makes sense cool.


I think most of the people have the question around this and this should have got answered with this one so evading a nice point from the Merriam Albert situation gets satisfied itself by just you know the question is that our situation said certified with only I equals two array dot length why I put something like you know greater than there you are true we are only happy with this one but I am just putting it to avoid any kind of error situation so for example in the starting itself suppose somebody calls this function like this ready array and something called five so again this is not a valid right so even to rule out this kind of situations.


In the case, if I just added I equal to array dot length in that case if condition will not hold and we'll go and honestly it will go and call recursively right so to avoid that kind of error input I have also added even greater than equal to so that those cases can also be covered but this is how you have to think of whenever you have to solve our problem don't just solve the problem in hand-tying two things futuristically you know try to think about all the boundary cases and then only you can separate yourself from the rest of the people see guys today this is the word today's news go for average people you have and you know I have to make a difference from your solution to the other solution.


How do you do that the only way of doing that is that try to think futuristically try to think about this scale and try to think about the boundary cases when you're solving the problem right so if you just use equal to equal to you're just trying to solve this problem statement but if you also think about the boundary cases the errors or the invalid input that will come to the problem statement you should consider those things also cool I hope this is making sense so we shall your question is in perspective of coding is explained in practically.


I hope this question is mostly around the recursion so again I am coming back to the next version I think this your answer questions will get answered with that so another question and I think the doubt among the people is that say the same question you know how people say that say most of the problem most of the problems can be solved by you know both alternative activity means at in the for loop all while loop or through the recursion also most of the time the problems can be solved with both identities as well as the recursive approach then why recursion is so important.


Why recursion is so important the reason being is that as it can be when the flexibility that you have in the recursion that you can apply some operations while you are going down and while coming back also that is not that is ideal because identity is a one-way solution you only move in one direction either it in the reverse or the forward direction so that is one of the limitations with I should use the second is that many of the situations, for example, take the example of a tree or take the example of a graph in this kind of data structures right since the arrangement is not linear right it's more about the pattern right because every for example in the tree every node will have the child and every child will have another child so it's more about the pattern right so in the questions where is mostly around the pattern identity becomes a little complex because you have to set the pattern linearly.

Post a Comment

Previous Post Next Post