Transcript for:
Understanding Recursion Fundamentals

[Music] ah finally the work is done let me just call my friends for the lunch today hey haryana let us all meet for the lunch today what do you say hi views sure so let me ask you yeah that'll be cool [Music] hi the dancer would you like to join us on lunch today hi Rihanna sure let me just take with Chira can I let you know okay [Music] would you like to join us on lunch today so let me check [Music] all right thanks [Music] sure see you at the lunch okay bye here we go hi Rihanna see you at the lunch yep hi views so see you at lunch all right okay [Music] what you just saw right now was a real life example of recursion you see each person's response was dependent on someone else and Ajay in this case was what we call a base case for a recursion to get resolved or to return a value similarly in a recursive function a function keeps on calling itself until it reaches a base case that returns a response so that all other function calls before it gets resolved as well let's better understand this scenario by replicating it with our code so I'm going to take five functions over here and each function represent a single person so let me write this out real quick just like this so as seen before it all started with me so okay I'm gonna call this function over here right so I'm calling piyush over here and piyush is calling Rihanna for asking for lunch right but the response of Rihanna is dependent on someone else that is vedant over here so she is calling vedant right now and but the response of vedant is dependent on chirag and chirag's response is dependent on Ajay so as you can see these functions are calling some other functions and the response of that particular function is dependent on some other entity or some other function right but in this case Ajay is base case so this is what we called base case and this is what is responsible for resolving a recursion as the recursion will keep on going on going on going right so Ajay responds with true so all of these other functions will also respond will true if it responded with false then all of them will responded with false right so if we just try to run this node recursion Basics and yeah obviously I haven't console log this now if you try to run this yep you're gonna see we get true over here that is piyush in the end will return true because all of the other functions did return true but then you will say how is this related to recursion isn't recursion about a function that keeps on calling itself but here you have written multiple different functions yes you're right and this is just a representation to show you how a recursive function will work when we go on to convert this scenario into an actual recursive function but first let's see how a normal recursion looks like so I'm going to comment this out and I'm going to create a function over here called recursive function and inside of it I'm just gonna say recursive function because that is what recursion is all about right it calls itself like it calls the function okay so I'm just going to call this function once and then this is going to keep on calling itself let's just uh you know console log it over here I'm going to say called let's see how many times this get called and as you can see now that we are stuck in an endless loop that this is being called again and again and again and again so this is the exact reason why we need a base case to stop a recursion so how do you add a base case so let's say this is going on right so to add a base case we have to add an if condition or any sort of condition so I will say if this is true then return something right it can be anything like it can be as one particular condition that breaks this recursion as This is Gonna Keep on looping forever so let's get back to our previous example this example and let's try to recreate it by using a recursive function so what I'll do is I'll just name this as let's say go to lunch and we're going to start with the first person right so I'm just gonna provide it one over here and I'm gonna say person so we have totaled five people over here right as soon as it reaches the fifth person I'm gonna break it so this is one by default so I will say if person is equals to five then return true else we're just gonna call it with person Plus one because obviously we're gonna move to next person right and you know I'm just gonna console log it over here just the value of person to see where we are currently and I'm gonna console log this one over here as well I'll just say outcome to know what's the outcome of all of these calls and yeah I think let's try to run this and oops my bad I need to return here as well right so that it uh Returns the value to the previous functions as well so now if I try to run this yep you're gonna see first person second person third fourth and as soon as it reaches the fifth person it returns true so that all other previous functions can also return true now I know this might be a little bit hard for you to understand it one go but don't worry as we move forward in this video you're gonna get a much more clear understanding as we discussed more and more example of recursion now I know you might be thinking why do we need recursion in the first place so recursion is a very important topic when it comes to a lot of algorithms such as tree and graph reversal algorithms a lot of sorting algorithms divide and conquer algorithms backtracking algorithms dynamic programming and much much more so recursion is a very ground level important topics for what is about to come in this course and data structures and algorithms in general in JavaScript so let's see an example on how you can convert a function that is written by using for Loops into a recursive function so we have this function over here called multiply which takes an array and what it does is it multiplies all of its elements so if I console log multiply and provide it with a simple array let's say one two three four right and now if I try to run this you can see we get 24 so 4 multiply by 3 12 12 to the 24 24 into 1 is 1. so we get 24 over here now we are using simple for Loop over here and multiplying each element with another so how do we convert it into a recursive function okay let's comment this one out and what I'm going to do I'm gonna write another function called multiply which again will take an array and now what are we supposed to do over here so in this for Loop what we were doing is we're taking an Global State called Product which was by default one and for each iteration we were multiplying every single element into that Global result but in recursion we don't have any Global State as such so what we can do is we can divide this problem into small small parts and then we can multiply them all together so let me show you so what we can do is we can say return and we can take take the current element that is the last element or the first element so I'm going to take the last element which in this case um is going to be array dot Len minus 1. so I've taken this last element over here and what I'm gonna do I'm gonna multiply it with rest of the result because that is what we're supposed to do right if we have one two three four as an array we're supposed to do 4 multiply by 3 multiply by 2 multiply by 1 right so in this case we have accessed 4 over here now how do we access three what we can do is we can simply call this multiply function and we can provide it array and remove one element from the array array dot slice 0 comma array dot length minus one but wait there is going to be one moment where this array becomes empty right the length of this array becomes empty so I'm gonna put an if condition over here if array dot length is less than or equal to 0. then I'm gonna return 1 because there's nothing to multiply over here right so I'm just going to return 1. else we're going to do this I think this should work let's let's find out yep you see we get 24 over here so let's Deep dive what's going on over here if right at the beginning I try to console log the array you're going to see and now if I try to run this you're going to see first we have one two three four in the first call we have one two three four then in the second call since the array dot length is not equal to or less than zero we're gonna go to this line over here we're gonna take the current element that is 4 and let me just explain with this so we have we currently have 4 over here 4 multiplied by this multiply with array dot slice 0 comma array dot length minus one that is 0 to 3 so this will be 1 comma 2 comma 3. this is what we're gonna call so now we have multiply with 1 comma 2 comma 3 again length is not equal to or less than zero so we're gonna go to this statement again now for this function call what will be the scenario it will be array array dot length minus 1 which is in this case 3. so I will say 3 multiplied by this multiply function but again we're going to slice to array length minus one so now this will be 1 comma 2. right now again this will evaluate this one uh first of all let me just take this and replace it over here so now we are at over here now again it's gonna take the last element that is 2 and multiply it by multiply and now array will have one just like that and again when we call this then we will have 1 and then we reach the base case which is if array's length is less than or equal to 0 which is going to be again something like 1 right so then we're gonna return one and now we're gonna return each and every value for this recursion and we're going to multiply them all of them together right so that is when we will get our final output of 24. so here that is how a basic recursion works now you might be thinking which one is better is the looping approach better or the recursive approach better so to be honest there's no right or wrong answer to that it's up to you and depends on the particular problem that you're solving when it comes to writing code you have to make sure that it's easy and understandable to read and recursion can sometimes result in a short and more simpler code so that is why we emphasize on using recursion a lot and that is why I have a question to ask you why did a programmer go broke because he kept on calling himself and his bank account reached the base case of value 0. uh you know because he kept on calling himself and each version of him spent that money so you know there was one point it reached never mind all right now let's see one of the most all right now let's see the most asked recursion questions that are asked during our DSA interviews so the first one will be factorial of n so what is this factorial all about so when we have a number which in this case will be n so let's say n equals 5. so factorial of 5 will be 5 into 4 into 3 into 2 into 1 so I'm going to get a function called factorial which will take n as an input and now it's supposed to return the factorial of this function so I would highly recommend you to go and try this question yourself first because it's very similar to the previous question that we just did so yeah pause the video and try this question yourself and then continue with this video so okay I hope you've done this question if not let's continue we have function called factorial and we're taking n as an input so if we say console log factorial and let's say we take 5 as an input right now right now we have 5 over here so I'm gonna say return n into so as you can see whatever the value of that number is that is Multiplied first right I mean that is the part of our calculation so what I'm going to do simply do I'm going to say n multiplied by whatever comes next right now what are we supposed to do we're supposed to do n minus 1. so can I just not take this function and call it again with n minus 1. simple right so now it's going to be 5 into n minus 1 which is going to be 4 into 3 into 2 into 1 but again we have to take care of our base case so what can be the base case in this so as soon as this reaches the value of 0 we're gonna return 1 because it's not gonna go further any further than that so I'm just going to Simply say if n is equal to 0 then just return 1 because if you multiply anything by 1 it won't make any difference right so that is why I'm returning 1 over here else you can return n into factorial n minus 1. cool let's try to run this all right we get 120 over here let's see so 5 multiplied by 420 into 360 into 2 120 okay fine we have 120 over here and that is what we're supposed to get awesome now the next question is the range of numbers so in this question we're supposed to create an array with the range of the provided number so we're gonna get one start variable and other the end variable so as you can see we have this start num and and num and we are calling this range of number from 0 to 5 and you have to use recursion to do this so again I would highly recommend you to solve this question yourself first and then move on with this video so I hope you were able to do this let's see how we can do it so first of all we're gonna check the base case first so obviously the start number has to be less than n number right to generate it from start to end so simple we're just gonna use our common sense and say if n num is less than statnam then it makes no sense so to continue so this is going to be our base case if that's the case then I'm gonna simply return an empty array because there is no chance that we can just generate another array right else what we can do is we're going to say const numbers variable and I'm gonna say range of numbers we're going to call this function again with start number and end number minus 1. so with each iteration with each recursive call so if we have n number as 5 so it will be 4 then 3 then 2 then 1 then eventually zero so it will go below start number right and as soon as that happens it's gonna return as an empty array and since this is going to return as an array over here this numbers will be an array so I will simply say numbers dot push and push the current element that is the end number so num over here and I'm just gonna return numbers let's try to understand what's going on over here so if I try to draw run this so first of all we have range of numbers with 0 comma five now in this case the end num is not less than start num yeah obviously and over here we are calling this function again first so okay fine we're gonna call this function again start num which is going to be 0 and end num will be 4 in this case first it was 5 then it was 4. then again it's gonna go back so num less than start now no that's not the case so again it's going to come back over here it's going to call this function again fine I'm going to call this function again but this time it's going to be 0 comma three similarly it's gonna be zero comma 2 and then in the end it's gonna be 0 comma 1. let me just write all of these in separate lines all right cool I think it's clear up until this point now after this again it's gonna call with start num and num minus oh my bad this is not zero this is one actually yeah sorry for that now after this iteration it's gonna go over here with one and one and okay this is still uh satisfies the condition but in this case it will be 1 comma zero right now in this case what's gonna happen it's gonna satisfy our base case so n number is less than start number it's 0 less than one fine so what we're going to do we're gonna return empty array fine in this scenario now we get an empty array in this numbers over here so we can do numbers dot push and num so in this case end num was 1. so we just pushed one over here right cool we get one over here and then we're gonna return numbers simple so numbers in this case is this one array now we're gonna go to this case now in this case we get in numbers this array with value 1. now after this we're going to say number dot push and num now in this case end num was 2 so I will say 1 and I'm gonna push 2 over here simple again it's gonna return numbers and in this case end num is 3. so I will say 1 comma 2 comma 3 similarly in this case and num is 4 1 comma 3 comma four in this case it's 5 1 comma 3 comma 4 comma five so you see how this bottom up approach worked we went to the very end of the array and then it returned us with the empty array and then we started adding elements to it in a reverse manner so we first added one then two then three then four then five so this is the beauty of recursion and how simple it gets while writing code with recursion I know it might be a little bit complex but with time as you would have written a lot of recursive code you can understand this very easily all right now in our next question we are given an integer X and we are supposed to return true if x is a palindrome and false if it's not a palindrome so what is a palindrome so I have explained this question in a much in depth in this video right over here so I'm gonna play that clip over here for you to understand so palindrome number is a number which reads exactly the same when you read it forward or backwards so if this is 121 so if you you know reverse the number one to one so it's exactly the same as it was forward as well as backwards but that is not the case with 10. so if you read 10 it's 1 0 but if you read it in reverse manner it's 0 1 therefore in this case output will be false but in the first case output will be true so up until this point what we were doing was more of example of linear recursion that is recursion in one single line but with this question this Fibonacci numbers question over here we're going to see an example of recursion where multiple recursive calls are being made at once so what is a Fibonacci number so in Fibonacci number we're given a number n which let's say in this case is 5 right so how does this Fibonacci series works so we start with two numbers zero and one now to get the third number we add 0 and 1 and we get 1 now to get fourth number we add previous two numbers that is one and one two now to get fifth number we add third and fourth number and so on and as you can see over here to get F of n we are supposed to add F of n minus 1 plus F of n minus 2 and trust me this is a very frequently Asked question when it comes to data structures and algorithms interviews when the interviewer is looking to ask you a question regarding recursion so let's before jumping into the code let's understand how this will visually look oh no no no you're not gonna explain this algorithm when algo agrawal is here because people would like to see this algorithm explained visually and that too from algo Agarwal so let's get it let me explain you Fibonacci number how to do Fibonacci number by using recursion in the most easiest way possible so let's see what is a Fibonacci number as explained Fibonacci number can look something like this so if we want to calculate what's the Fibonacci number for five so okay Fibonacci series looks something like this it starts from 0 and 1 and to calculate the next numbers we have to add the previous two so zero plus one equals one fourth number will be one plus one two two plus one three three plus two five and so on so the fifth number will be one two three four five so three will be the final output when the input is five all right so let's see what are the steps that we are going to take to explain this so let's say we have a function called FIB which takes an input of five so let's see the rules first see as I mentioned it starts with zero and one and the index of our array also start from 0 and 1 right so what are we gonna simply say we're gonna say if the input that is provided to us if it's either 0 or if it's one if it's index 0 or if it's index one then we're gonna simply say return that so I will say if and we're considering n in this case so let me just you know remove this and I'm just gonna write n over here so if n is less than or equals to 1 that is if it's 1 or if it's 0 then obviously we're gonna return and right simple enough but if that is not the case what are we gonna do we're gonna call this function again but this time let's say if our original number was 5 if we want to calculate the fifth position so for fifth position it's going to be the Fibonacci number that comes before it and the number before that number that is the sum of previous two numbers so what do I mean by that okay if that is not the case we're gonna say else return what is the Fibonacci number of n minus 1 and add it with the Fibonacci number of n minus 2. that's the logic right if you are supposed to calculate this position right fifth position then you need to know only the previous two that's it you just need to know the previous two and you'll be good to go you can calculate this so that is all we are going to do and this is what our algorithm is going to be it's that simple let me explain you guys with much much easier visual approach so okay over here we have a Fibonacci number function with 5 as an input so what we want to do to calculate 5 we just need to calculate the Fibonacci number of 4 that is n minus 1 and Fibonacci number of 3 that is n minus 2. simple is that yeah now we need to calculate the Fibonacci number of 4 over here so how do we do that again same thing we need to calculate the Fibonacci number of three and the Fibonacci number of 2 simple as that same with here Fibonacci number of 2 and Fibonacci number of one let me just write this out same for this one as well all right cool so there it is there's the table I know my handwriting sucks but please forgive me for that so let's see let's see what's going on over here on all of these cases we have reached our base cases this is our base case this this this this this this this and this as well so our base case is if n less than or equals to 1 just return n okay so this is one that is less than or equals to 1 it is 0 less than or equals to 1 okay so this function over here will return one this function over here will return 0 this function over here will return 1 this one zero one one zero one cool now that it has returned the all of these base cases now what's going to happen over here we know we need to add both of these so to calculate Fibonacci number of 2 we need to add this plus this as you can see over here so 1 plus 0 is going to be 1. so this function right here is going to return 1 so where else is Fibonacci two okay this will also return one this will also return one and yeah I think that's it okay cool now let's focus on this part Fibonacci number of three we know the result of this we know the result of this one plus one two so we're gonna return 2 over here okay one plus zero one or we already we already calculated that over here one plus one two so this is going to return two in the case of Fibonacci number of four we're gonna return two plus one that is three and finally three plus two is going to be 5. so Fibonacci number of five will be five apologies I mentioned that Fibonacci number of five will be three that is not going to be the case since we start counting Counting from zero so zero one two three four five so this is what is going to be Fibonacci number of five so here that is what Fibonacci number is all about I hope you like my explanation if you did hit that like button right now and I will see you in some time with the explanation of some other algorithm ah this guy man he always interrupts my videos if it weren't for you guys if you guys weren't liking the way he explains I wouldn't allow him on my channel anyways let's move on and I'm gonna now write the code for this explanation but first before moving on to writing the code for the recursive approach I'm gonna show you how you can do it by using just four Loops so first we have taken an array ARR with initial values of zero and one because obviously we start from zero and one right and then we're gonna run a for loop from I equals two because we already have zero and one right so we're gonna start from Two and we're gonna go to all the way to n so as we Traverse further if the eye is 2 we're gonna say array I minus 2 plus array I minus 1 that is adding previous both values right and this is how we're going to populate our array with all of these values and and the end if the N is equals to 3 we're going to return whatever is in the third index of that array so let's find out run this real quick if I say n equals to 3 and run this you're gonna see we get 2 over here if I change to 5 let's say then we're gonna get 5 because in the fifth index that is one two three four five oh I'm I mean zero one two three four five fifth index we have five over here all right but we're not gonna use for Loop right we're not gonna do this the old-fashioned boring way so we're gonna use recursion to rewrite this code so I'm going to take this function and I'm gonna say function FIB recursion and we're gonna take n as an input and let me just comment this one out for now all right so as explained by algo agrawal what are you supposed to do since we already know that if n equals to 0 then it's going to return 0 but if n equals to 1 it's gonna return one so we already know what our base case is going to be right so I'm simply going to say if n is less than or equals to 1 then simply return n simple right so that is what our base case is going to be and now here we're gonna Branch out this into two different recursions so I'm gonna say return FIB recursion to n minus 1 plus fib recursion to n minus 2 and this is gonna form that complete tree that you just saw and in the end it's gonna add all of the values together cool so let me just try to run this recursion and run it yep you see we get five if I say three run it if we just you know add a pretty big value and it's gonna give the result accordingly yep great now let's move on to our next question which is reversing a string so again what I'm gonna do I'm gonna take a function over here called reverse string and this will obviously take a string over here so what are we supposed to do in this we're going to input a string and the output is going to be the reverse of that so if it's h e double l o the output will be o double l e h right so obviously before moving forward in this video you have to go and try this out yourself first so I hope you were able to do it if not let's see let's find out how to do it all right so we have a string over here right and what are we supposed to do we're simply supposed to take one character from it at a time and just put it at the back of the string but we are supposed to do it by using recursion okay so let's say we have hello over here we can simply take h at once and we can put it over here right plus this and then we can supply this remaining string back to this function and then it's again going to put the logic that we're going to take the e and let's put it over here right so how can we do this let's see I'm going to say return reverse string and I'm going to Simply take the string which was provided to us Dot sub string now you might not be familiar with these functions right now because I haven't made a video on strings yet but don't worry that video is gonna drop very very soon but I'm gonna explain you what this function does over here this sub Str function what it does is we are supposed to provide it a number that is the starting position of the desired substring so the index of the first character in The substring is zero so what it will simply do is if we provide it with one over here so what we'll do it'll start from 1 and it will take the complete string just like this right and then what we can do is we can we have taken this string and then we can put H at the very last so we can take the first character now so I will say Str dot there's a function called KR at so what Cara does is it Returns the character at the specified index so we need the first Index right so we're going to say 0 that's it that is all we were supposed to do but wait we haven't written the base case over here yet so there's going to be one point right where all of the characters of this string will be exhausted and we will be provided with just an empty string also hold on I've just written it inside of this bracket this is how it's going to be it's going to be outside of that function yeah so it's gonna at one point exhaust the complete string right so we're supposed to say if Str equals to empty then we're gonna return empty right else we're gonna do this over here don't worry I'm gonna drag on it as well in just a second let me just console log this and run this reverse string hello and if I run this yep we get the reverse of this string so let's see what's going on over here so we have this hello over here right in the first iteration uh this is hello okay so in the first iteration we're calling this reverse string function starting from the index one so this will what this will do is this will call reverse wait reverse string starting from 1 so e double l o right and then we are adding the first character which is in this case h plus h right that is what we're going to do now let's talk about this what will this result in this will result in starting from index 1 so it will be double Lo plus e right similarly for others as well let me just write this out real quick yep just like that so we've reached over here right and now we're calling reverse string with the empty string so when we call it with the empty string then this is going to return with the empty string right okay let's replace it with the empty string then right over here so empty string plus o will obviously return as o right now this thing is returning us oh cool let's just take it and replace it over here oh right so o plus L will obviously will be o l right so this l o is returning us o l fine I'm gonna remove this and replace it over here o double l fine let's take it and replace it over here let's take this and replace it over there so you see this is how you get a reverse string by using recursion I hope now you must be able to find recursion really easy right because now you're able to think how recursion works there is a pattern if you see these questions right you're supposed to call these functions and there's always going to be a base case and this base case is usually very obvious if you just put a little bit of common sense you're going to understand how easy it is to find the base case and once you find the base case it's I think it's pretty much easy to you know do the recursion based questions and this next question is called subsets and trust me this has been asked to me multiple times in the past during DSA interviews so I would need your full Focus to understand this problem so let's see also one more thing that this is an example of what we call a back tracking algorithm which we're going to talk about in much in depth in our future videos but for now you're gonna get a brief understanding of what backtracking algorithm is and how you can implement it using recursion so we've been given an integer array nums of unique elements so we're going to have a array just like this with the unique element there is no element will duplicate itself right so we're supposed to return all of the possible subsets that is the power set now what is the power set so we have this array over here as an output and this will contain all of the possible combinations that this particular numbers can create so as you can see we can either create a combination of an empty array okay or we can have one two one two three one three two three one two three so these are all possible combinations now keep one thing in mind one comma three and three comma 1 are absolutely same so it doesn't matter the sequence doesn't matter if both of the numbers exist or you know if the unique number exists in a single array so don't think 2 comma 3 and 3 comma 2 as separate entities because there's the same so that is why the solution set must not contain the duplicate subsets it should contain only the unique subsets and also solution can be returned in any single order true so let's try to First understand this problem in a visual format and this time I'm gonna explain you this in a visual format is back let's continue so let me explain you how you can efficiently write this subsets algorithm and let me explain you visually how this is going to work because this is a pretty complex algorithm and you need an expert like me to explain you not some Noob like roadside coder all right so okay cool let's get started so we have been given an array over here right let's say we can say it's one two and three and we are supposed to generate the all possible combinations of this array that is all possible subsets that this array can create right so let's see what is the approach that we are going to take so we are going to take two Global variables first the result and other is the temporary variable or the temp array and then we have this function called recursive now inside of this the approach that we are going to take is this so by default we have an empty temp array okay so we're going to take an empty temp array over here fine so now what the approach that we're going to take is now amongst all of these three to create a subsets either a subset can include one or it cannot include one right similarly either you can include two or it cannot it can include three or it cannot right so that is exactly what we are going to do every time we're going to make two recursive calls in one recursive call we're gonna say okay take one but in the other one we're gonna say no don't take one so let's say to create our first sub array we're gonna say take one so we have added 1 into this array fine and the other array remains empty cool now similarly we are going to do for two as well for all of these we can either take to or we cannot take 2 right so now our array contains 1 comma 2. and this array doesn't contain two so it's just one similarly here as well it can either take 2 or it will not take to and it remains empty so if you notice in our algorithm that is exactly what we are going to do so we have this recursive function which take nums and I and we have this base case to okay let us not talk about the base case over here first in the temporary array we are first going to push the current values that is the current value of I so when we call this function right so okay I haven't written that so when we call this function for the first time with the nums array and the 0 as the default value so here in the temporary array we're going to say temp dot push numbers of 5 so nums of I is 1 right so as mentioned either we can take one or we cannot take one so I'm gonna first say just take one and push it to this temporary array and just call this again so if you see over here we are calling this function again this right over here is basically our function that is being called but with a new array called temp and right now temp contains only one and the updated value of I that is 1. now why did we do this why did we do I plus 1 so that we can move to the next element so we have already taken one right we don't need to operate on one anymore now we need to take two or we need to reject 2 so again in the next iteration we're again going to do the same but if you see below this we're doing temp dot pop that is we are rejecting one so in this one we just added so temp in the first case was this over here but temp in the second case that is temp dot pop so we removed the element from the temp so now temp is empty and now we are sending an empty array to this recursion so this right over here will be F of empty array and I plus 1 that is 1 over here now if you call this function again you're gonna go back you're gonna say temp dot push numbers of I now we are pushing the current I so what is the current I 1 and what is 1 1 is the index of two so we are pushing one inside of this temp so now you can see we can either take to we are taking 2 inside of it so our array will look something like this or we can either reject to that is we are popping the two from the array so we are removing the two in this case our array will look something like this and again we are going to make the same function calling both of the cases similarly in this recursive call as well so I've actually written this completely let me just show you yep there it is I've written this complete diagram over here so in the first iteration we are either taking one or we are rejecting one and this iteration as well either we are taking 2 or rejecting two in this iteration as well we are either taking 3 or rejecting 3 right and that is what we are going to do for all of these and in the end you're gonna see we get all of these final elements as our answer so this is what our answer will look like this right over here you can tally this with all of these results and you're going to find that's exactly the same we haven't yet discussed the base case so when we happen to reach this condition that is I is equal to n because we are increasing length of I every time right in this case I is 1 in this case I is 2 in this case I is 3. so as soon as we reach the length of the array that is when I is equal to nums dot length we're gonna say we're gonna take this result array that is the global array we're going to push whatever we have currently in temp So currently we had one two three so we're gonna push that inside of this result array so this is the let's say a result array and we first post one comma 2 comma 3 then again again we're gonna push one comma two and so on with all of these array over here and then it's gonna give us this final result so yeah awesome I hope you like this explanation again if you haven't yet subscribe to the channel hit that subscribe button right now and don't forget algo Agarwal needs to write code as well so open your Instagram app right now and search roadside coder and message him to give algo Agarwal opportunity to write code as well oh man not again anyways I'm just gonna go on and continue to explain you the code now since you already got an overview of you know high level overview of the code let me just quickly write that code down so I'm going to create a function over here called subsets and as explained it's gonna take an array or called nums now we're going to take a few variables first fall is going to be result result will be an empty array then we're gonna have temp which contain which will contain the temporary elements of this array that is the subsets of this array and then we're gonna you know keep on changing the temp now next we're gonna create a function called I don't know let's say recursive subsets and this will take nums and I and in the end we're just gonna execute that function nums comma I and return the result don't worry I'm gonna explain everything step by step so let's go inside of it so we're gonna start this by using our nums that is our array over here and the initial value will be 0 because that is where we are going to start we're going to start from the first Index right so we have provided the initial value of i as 0. now let's just first get the base case out of the way so simply I'm just gonna say if it reaches the very end of the array then we're gonna return the result right so I'm just gonna say if I is equals to nums dot length that if it reached the length of the array then simply return result dot push and whatever we have inside of our temp array so our strategy was this that either first we're gonna take an element or we're not going to take an element right inside of the temp so fine so for the first element or for the current element which is the 0th index that is 1 over here I'm just going to Simply say temp dot push either we're gonna take nums of I right or we're not going to take nums of I so first of all we took the nums of I and we're gonna called um recursive subsets with nums comma I plus 1. and next We're Not Gonna Take it so temp dot pop so we're going to remove it and then again call it recursive subsets nums comma I plus 1. so in both the cases we're gonna move forward but we've took the first element and then we're gonna move on to the second element and in this case we didn't take the first element and then we're gonna move on to the second element and again these are going to Branch out into different different combinations right so this is how you run this algorithm so let me just quickly grab it and go log and try to run this and I'm gonna give this array over here to this one and let's try to run this and yep there you see we have all of the combinations of subsets for this particular array if I just add one more number over here it's going to become huge now yep you see it gave us more combinations for this but what if we just have one number yep as expected oh this is also running this previous algorithm let me just comment that one out so yeah that is it for this video and if you like this video give this video a huge fat Thumbs Up And subscribe to the channel for more such awesome interview videos and if you haven't yet access the complete data structures with JavaScript series click this card above my head to access the complete series