Lecture32: Understanding Recursion the easiest way || Day-2 || 10 Day Recursion Challenge

Hello ji, how are you all? . This is Love Babbar and welcome to the channel 'Code help' This is our lecture no. 32 , recursion's ten days series is going on , here we are going to talk about day -2 In last lecture we talked about three examples , intend was to make you understand recursion 90% students have understood it , 10 % students has left So for those 10% students , we will try to make you understand once again and this time you will understand it today we will also do problem solving , we will code on leedcode or code studio lets start , in last video we have seen what is recursion ? what are the components of recursion , mandatory and optional component base case and recursive relation are mandatory in optional processing , printing , updating or want to save the answer you know that why we use base case , if we don't use then stack overflow will occur and it will show segmentation fault Why we are using recursive relation ? because it is mandatory you will understand processing , as we will start solving questions lets again move to level-0 it means , we will learn recursion again , lets try to understand it using example I had a teacher , from whom I understood recursion , we will take his example through this you will understand it I said , you have a house somewhere , this is home and you are here , you want to go your home lets make it more easy , your intend is to go back to your home and you are here assume , it is 1km , how will you go to your home , 1 step ,1 step, , in this way you will reach there .

Its simple logic this is called recursion . You solved 1 case , listen very carefully you solved 1 case , you have to just worry about 1 case it will get solved itself in last video we have seen function call stack , recursion tree , no need to think of it every time . It was just for deep knowledge But every time we will not think about it So whenever we will solve recursion question , always keep in mind that we need to solve only one case then it will get solved by itself we need to solve only one case then it will get solved by itself if my problem statement says , you are here and you have to go here then I need to solve only one case , just take one step we need to solve only one case then it will get solved by itself This video is sponsored by Relevel , if you want to enter into the Software development and get a job with high CTC then Relevel is the platform for you , you can see well known companies hiring from here , like CRED, 1mg , meesho Urban company , Upgrade , so, there are lots of good examples.

Relevel tests are completely free There are many startups hiring here as well , and a new feature has been added that is they have added the sample question paper So, that you can take the mock test before giving the real test , so that you can get a overview of what may be asked in the real test So , I can practice .

Till now they have already provided 7+ crore jobs and there are some names who are there in the leaderboard and the benefits of the leaderboard is that you get exciting prizes, expensive ones . Now if you want to take up a job and win exciting prizes while maintaining your position in the leaderboard , then you just have to sign up through the link in the description , register for the test and there are a lot of categories to register in These are the categories here and a new category has been added -" Full stack development' , so register for it , take up a job and then give me a party , Bye!! we need to solve only one step then it will get solved by itself by recursion here we need to add base case , in this case when you reached your home you have to stop Let me explain you again , there is a problem statement which says , I am standing here and I have to go towards my home I will go by taking 1 , 1, 1step , just solve one case then it will get solved itself ,you no need to worry about it I will take 1 step and come here , recursion will solve the left portion I will take one step then will use recursion , I will stop when I will reach my home .

Lkets try to code it lets assume , int dest = 10 , int src =1 , we made a function reachhome( src, dest) , void reachhome ( int scr , int dest ) , we have passed by " pass by value ' In recursion , first we write the base case if( scr==dest) , cout << "pahuch gya " <<endl; , and return statement is mandatory then I said , then we have to make a recursive call lets understand it ,whenever I will take one step , what will happen ? if I was standing at 1 then I will reach at 2 then to 3 , current position is indicated by scr , it means we need to increment the src++ then recursive call would be , scr++, so we called our function , reachhome (scr+1, ), or simply we did src++ processing , move one step forward and reachhome (src, dest) , good we have completed our function , lets run it I have little fever , ran it .

Here it printed " pahuch gya" for your visibility cout<<" source"<< src << "destination " << dest << endl; , ran it see carefully what happened , source1 destination 10, source2 destination 9, source 3 destination 8, source 4 destination 7 then 5 , 6 ,7 ,8 ,9 ,then 10 and it became 10 ,when src==dest , then you printed " pahuch gya" Bhaiya explain it by writing code , your code is like this , if ( s==d) , then you print the result and you returned it This was your base case then you did processing , src++ here you said , move one step forward 3rd part is the recursive call , in recursive call you have written .

This is my function – reachhome ( src , dest ) And here your function ends In total , your whole function looks like this Now understand what's going on . Here we have sent two things in input ,src and dest in this 1 and in this 10 . At first function call occurred for ( 1, 10) s=1 and d=10 , you checked the base case , it gave false then you did src++ , i will become 2 then you did function call s=2, d=10 , again you checked the base case . Still its giving false , src++ from 2 it will become 3 we made function call , (3, 10 ) , in same way you will reach (10,10) , here your base case will be satisfied , so this case will return the answer to the previous one here it printed and then returned , in same way all return to the previous one nad your function call stack became empty then there was main() ,then reachhome(1,10), reachhome(2,10), reachhome(3,10) in this way reachhome(10,10) then it will return to the previous one , start getting removed .

In line no. 28 it will also get removed and your function call stack will become empty got it . In this way you can understand recursion properly 1st thing in mind should be , we have to solve 1 case and all other will get solved by itself by recursive call one more thing I should know that , where I need to stop , have idea about base case in this example , the base case is , when I will reach home I will stop This is the basic , with easy explanation it has became clear to you lets move to next question , which is very famous lets write it 1. Fibonnacci number what type of series is this 0, 1,1,2,3,5,8,13,21,…. if you see the logic ,sum previous two number is making the next number this type of series we call fibonnacci series , now what we need to do I will say give the 8th fibonnacci number So you have to create a function which will give me 8th fibonnacci no., in this case it will be 13 in this way I will be giving you value of 'n' in input , n is the nth term of the fibonnacci series .

You have to this in output Got it , will have to code it . And you will get it very easily Lets again write the series , and try to understand how we will make recursive function , 0, 1,1,2,3,5,8,13,21,…. lets talk about the recursive call , if I tell you your any term is formed by the sum of the previous two numbers our any term is formed by the sum of the previous two numbers so how our recursive relation will be formed , f(n) for nth term of fibonnacci series if this is nth term , then the previous two will be n-1 and n-2 , f(n)=f(n-1)+f(n-2), we got the relation if f(n)=f(n-1)+f(n-2), we got the relation Any number is form by sum of previous two numbers In short , we have found our recursive relation Any number is form by sum of previous two numbers if this is x then previous two will be x-1 and x-2 we have represented it in form of x lets talk about second thing , base case if you remember PMI principle of mathematical induction , which you have studied in 11 or 12 .

The case was you have to do 3 things there was a base case p(0) or p(1) , you have to prove it true 2nd case , f(k) assume it true 3rd step , you have to prove f(k+1) is true . If you see carefully , this is recursion this is your base case , write the base case , this is the case you have to solve , this will happen itself if here we talk about the base case , if someone wants 1st term , then give 0 , if someone asked for 2nd term then give this or for other you can find it because for 1st and 2nd you can't find this , so we are including it in base case .

After that there are previous terms for each if ( n==0) , return 0 ,if(n==1) ,then return 1. That's it it was not hard . Lets move forward , so here you have written the base case , now will solve question here is the question , this is a question on leedcode f(0)=0 , this case and f(1) =1 , this one . f(n) =f(n-1)+ f(n-2) , it is also written here now calculate the value of f(n) We know that , there is a base case , if ( n==0) , return 0 ,if(n==1) ,then return 1 now what to do ? , think We said find the answer, int ans =fib 9 n-1) + fib (n-2 ) , then return ans , that's it lets run it once . Successful lets you every test case and run it .It ran successfully accepted . it is done , it was very simple this was the base case , and I am assuming it can find itself Very easy one , you will also solve this .How we can optimize it more , there are many ways .

You can do it by using DP and also by using for loop this is homework for you , that how can we do it using for loop let me show you recursion tree of this to give you more clarity , we are calling fib for 'n' , n=3 . Here at line no.11 the call has gone for n-1=2 and n-2 =1 , and we know in our base case return 1 in case of f(1) , then for n-2 which is 0 , it will return 0 .

By adding both we got 1 then f(3) called for n-2 which is 1 , it returned 1 . this 1 and this 1 became 2 got it , Bhaiya how you are saying it is 3rd term 0,1,1,2 ,3,5,8,13…then 3rd is 1 . yes , you are absolutely right this is 1st , then 2nd then 3rd , according this should be the answer but , if you see in question carefully , the 1st term has been indexed as 0 , it means 2 is the 3rd term So in this way , we are getting 3 as answer if I have taken it as 1st term and this for 2nd term . Now you have understood it properly lets move to our next question , Count ways to reach the N-th stairs , it has came many times in interviews you will enjoy while solving it I will explain you what happens here , you have been given a number of stairs ,you are at 0th stair at starting You have to reach at N-th stair , at a time you can climb one stair only or two steps you are supposed to return the number of distinct ways in which you can climb from 0th step to N-th step No need to think that much , where are you standing , you are at 0th stair , you have to reach at N-th stair It means you have to climb this in this way and you have to reach here, this is src and this dest At starting we are here and I need to reach here , what is the condition you can climb 1 step or 2 step at a time this boy can climb 1 step or 2 step at a time, it depends upon him he has two choices we again reached to recursion relation , we have solve for only 1 case , all other will be solved by itself we have to find out the recursive relation listen properly you will get it , I am repeating again .

We have to just solve one case If I tell you , how would we have reached here , in short what will be F(n) Bhaiya , either by climbing 2 step or by climbing 1 step In short , you have reached here , by this stair or by this one I am repeating again , you have came here either by climbing 2 step or by climbing 1 step , this is n-1 and this is n-2, we got the relation In short our f(n)=f(n-1)+f(n-2) we solved one case , I am at any stair , either I will come from the previous stair or n-2 , got the relation It was easy , it can be coded very easily , now tell me the base case what we can say , function call is happening for n . In function you have sent ,n, I will just write the conditions that I know, if I was at -1, -2 or at -3 stair which is less than 0 also why are you writing like this , you will understand it when I will write recursion tree . lets assume the stair is below the ground we will say that there is no stair like that , and this is the point ,if there is stair is below the ground , if(n<0) , then return 0 there is no such stair , you solved one case ,if(n<0) , then return 0 if(n==0) , what are the way to stand at 0th stair , he jumped and came back to here there's one way lets understand it once again , this is 1 st case and this is the 2nd , 1st say you are somewhere at basement , then you will pass 0 , in 2nd we are standing at 0th Don't think much , I was standing here , and back again here , number of ways coming here In short I want to say , younare standing at 0th , how can you come from 0th stair to 0th stair there is only one way , you jumped back.

I returned 1 , base case got solved recursion also solved . Coding it, should be easy lets write it , if ( nStairs <0), return 0 , then if( nStairs==0), return 1 then I did recursive call int ans =countDistinctwaytoClimbStair(n-1) +countDistinctwaytoClimbStair(n-2) got it ,this way you got the answer and returned it Lets run it once , some compilation error is coming ,our 'n' is not declared , in place of 'n' , nStairs has been used We are trying to solve this question , there is time limit , this time limit is according to DP , we will learn it in future here we have to use DP for clearing its submission , so here you can't solve this question without DP But still we have understood the code . Let run it once do not scare Bhaiya why all test cases are not accepted , will learn it in DP lets run this code , 2,2,3 , we have to go 2nd stair and 3rd stair.

There are ways 2 go 2nd stair and to go to stair there is 3 ways Our code is working properly . Leta run one more , 5, 8 is the answer In output they have written wrong . We are absolutely right . There is fault in question ,team will correct it So , this thing is clear to you , lets understand what our code is doing As I said , there is total 4 stairs , 1, 2, 3, 4, 5 you are standing at 0 stair , this is your N th stair our function call is , do this way for n-1 , n-2 our function has started from Nth stair , here we took 5 as input, we compared it with base case . It came false then I we called for n-1 , call goes for 4 , again its false , then for 3 again false , for 2 again false , then for 1 again its false then here we called for 0 .Now the base case has reached So , it gives 1 in output then call goes for n-2, for -1 . your base case has reached , then you gave 0 in answer , good , 0+1 =1 2 did same for n-2 , for 0 , its 1 , sum is 1+1 =2 , then 3 did same ,for 1 its calls for case 0 and then for -1 in 0 case , it comes 1 and for -1 it comes 0 , 1+0=1 , you gave it as output N0w its 1 and 2 it gave 3 as answer , 4 called for n-2 , that is 2 and for there is two calls , one will go for 1 and other for 0 , for 1 its calls for case 0 and then for -1 then it will go for 0 , and -1 , 0 gave 1 and -1 gave 0 , 0 =1 , and here 0 said , 1+1 =2 , 3+2 = 5 , in this way , I explained you the calls of one side then here it will go for , n=1,2 , then for 3 .

Here I am not making the whole charge . You will get 3 here . 5 + 3 = 8 here we will see answer for 5 , it is 8 . When we make recursion , it becomes clear make recursion tree in the beginning , for 1 case then it becomes clear . Rest will happen itself once we find the base case , there's nothing left . this test cases are no solved , it gave TLE . Don't worry about it . You will get it , when we will learn DP then we will solve it through DP .

Then it will pass all cases , because this approach is not fully optimized Now you have understood it , here you core code is ready , I will copy paste it here lets move to our next question , -> it is called say digits lets understand the question , I will give you a no. in input , this way you have to tell in output , Four , One ,Two you have to just print the digits , I will explain you the approach .

You have to code it yourself , I will add my githup repository you can get the idea . I'm explaining the approach lets assume that we have this in our input , I will find the integer after taking out its nothing , I have stored a mapping , for 0 ->zero , for 1 its One , for 2 its two …… in same way if 9 comes it will show nine If I make an array , of size 10 and store the strings in it We will make a array of string type , string arr[10] and initialize it with "zero", "one"," three" upto " nine" . we have stored the mapping In any number , its easy to find the digits int digit =number % 10 , here we got the digit I have to make the digit small , I will divide it by 10 , will update the number= number /10 In this way , we have studied it before also .You have to solve this question in this way , here some processing part has been added Lets understand it once.

We have taken a no. here we have taken a no. , then we made a function SayDigit( n) , void SayDigit( int n) , the 1 base case is , if(n==0) , simply return it I said , give the digit , this is the processing part how we will get, int digit= n% 10 , then update the no. n=n/10 then we have to print it , there is a problem , you will get it But did we need to print digit , string arr[10]={"zero" ,"one", "two" , " three" , " four" , " five" ," six " ," seven ", " eight" , "Nine" , " Ten " .

In this way , we have stored the mapping we have passed the mapping here Now I printed arr[], what ever is coming . put endl here for clarity then I made the recursive call we said , printout the left numbers , Here you printed the digit , made one digit smaller . Printout the left numbers sayDigit(n) , you solved one case , then recursion will do it all this is the type of code . There's a problem you will get it , let me show you by running it print 4,1,2 . Lets run it again , why it is coming like this lets print it more properly ,so that you can get more clarity Now I ran it . Is laptop is working slow , I said 412 . In answer it gave four and zero There is some problem , we separated the digit . Lets understand it once here we have to print digit not the number, in this we are having problem . Lets run it We gave 412 , we are getting two , one , four , we getting in opposite order why it is coming opposite , see carefully .If we are taking 412 in input , 1st I separated out 2 from 412 and printed it .

It became 41 after dividing with 10 then I separated out 1from 41and printed it . It became 4, then we removed 4 after dividing with 10 , and printed it , then it became 0 and we reached the base case that's why you got 214 as answer , then how to solve it its very simple , don't print it now . After reaching the base case if you print then 4 will be printed first , then 1 then 2 how it will happen , it simple if you remove it , and put it here then run it it printed four one two before , we were removing digit and printing it , we were getting 214 , If we go this way then this is going to print, when you will come back from the base case , then print it And you did same thing here . I want to give it in homework but , here you have to do little brain storming , try to understand it Make the recursive tree of this problem , you will get it Make the recursive tree of this problem, this is your homework , also make the function call stack Today's lecture was on recursion .

It has been clear to you If you have understood , all the 3 problems .Then write in comment section , we have understood all the 3 problems . We will become a champ in recursion In future we will increase the difficulty level . I have little fever ,It may happen ,that videos may get delayed , don't know what will be the covid report , We will meet in the next video, Bye bye!!.