 Okay, guys. So the next coding interview pattern that people normally see is something called subsets. So what are subsets? So for this, it's basically just generating all the possible subsets. So every single possible permutation or combination or subset with whatever it is. Okay, so you're going to recursively generate all the subsets. Now there's two ways to do this. One is to do it iteratively using some bit masking. But the other way normally is to go over it with recursion. So how do you do that? Well, first, let's actually explain what a subset is. So let's say I have given the numbers one, two and three, right? Subset, what they mean is like all the possible individual sets you could have in this array. So in math, it's called the power set. But let's just list all the different possible ways. So how what are the different possible ways we could have the numbers one, two, three. So to have numbers one, two, three, we could have, we could have just the number one, right? That's one set, right? You have number two, right? And the other one, we could have the number three, right? But there's more to it. There's more to than that, right? We could also have one, two, right? And we could also have two, three, right? And we could also have one, two, three, right? And then we could also have one, three, right? Because that's another set we could have, we're going to pick the number one and pick the number three, right? But we also could have the empty set, right? Because you could pick nothing, pick nothing. So out of all these sets, right? The generating subsets for all the possible subsets you could have for a set in this array is literally just how many different ways can you choose? Pick a number and put it into a set. So remember, the definition of a set means that it's like a grouping, but the, there's no duplicates, okay? So it's like a grouping, but there's no duplicates. So you might think that you could have the set of three, two, but three, two is the same thing as two, three. Okay, these are the exact same thing. Three, two is the same thing as two, three. The ordering doesn't matter per se. It really doesn't matter. So it's the exact same thing. There's also no duplicates. So we cannot have two, two. Yeah, so these we cannot have. This is the exact same thing as that. So yeah, so just ordering all the subsets you could see right here, we have one, two, three, the empty one, two, two, three, one, two, three, and one, three. Okay, so how many numbers do we have? How many number of sets we have? We have one, two, three, four, five, six, seven, eight. So we have eight total sets. Okay, but oh wait, how many numbers do we have? Well, we have one, we have two, and we have three. So we have three numbers, but there's eight. So it turns out that the total number of subsets that we have is two to the power of three, which gets you eight. So if we have n numbers, so let's say I have, let's say our array has like n numbers. If we have n numbers, it would just be two to the power of n. So that's the number of subsets, right, to the power n is number subsets. So that's just the gist of how subsets work, what they are, and exactly what we're trying to do, right. So as a programmer, this is the math of it, right, we're trying to generate every single possible subset in this array and see if it turns out right. So we need to know that we need two to the power of n. So if we have an array of size n, we need two to the power of n values in our total subset. Okay. So how do we do this in, with recursion in programming? Well, we do something called backtracking plus recursion. So it's not this difficult at all. It generally has the exact same formula of most problems. So if you know this simple formula, you could apply it to most of the problems. And we could go over as many problems as we want, as we want, because it's literally the same thing. So I'm going to show you guys the pseudocode of how this works. And then let's just explain it. All right, guys. So how do we generate all subsets? How do we generate all subsets to our value or function? Okay, so we need a function. Okay. So remember, we have this giant array, right? And we have our individual array of one, two, and three or our values that we need to generate. And we need to put, we need to generate all the different subsets that gets put in this giant array. Okay. Well, how do you do that? Well, first of all, we need to generate them for every single size. So each size. Okay. So what is the size when each size is equal to zero? So when each size is equal to zero, well, what is the subset with a size zero? It's just itself, right? And when each size equal to one, what are the sub arrays that have size equal to one? Well, we have individual one, two, and three, right? Each of these individual subsets, right, have a size equal to one, right? Because that's what we have. And then what about the size of each size equal to two? Right? Well, what are the different sub arrays we have the size values equal to two, we have one, two, two, three, and then one, three, right? And then we have an each size of size equal to three, right? And that's our final one, two, and three. So every time we generate our sub array or each sub array, right, we need to generate them for each of the sizes. And each of the sizes will be zero, one, two, and three. Okay, right, you guys coming understanding what I'm doing, like each of these sizes that we're generating it, we'll have size zero, one, two, and three. So for that, we need to have a function that generates all the subsets for each of these individual sizes, right? Do you understand what I'm saying? Right? Each of these sizes, we need to have a giant function, right? And we need to call this function for each of these individual sizes. Okay. Well, so for that, we are going to have a variable called each size. Okay. And each size is going to loop from size zero to the size of n. And n will be the size of our current array. So this current array, right? This current array has a size equal to n, right? So in this case, n is equal to three, right? We have three elements here, right? We have three elements in here, right? Three elements in this array. So we need to generate all the sizes, right? So each size is going to loop from zero to n. So what does that look like in the code? So basically, we have our main function, right? And then we're going to loop through from each size, it's going to equal to zero, and we go up to n, right? Each size is going to loop from zero to n. And we're going to call, we're going to generate all subsets for each size, right? That's what we're going to do. You guys understand what I'm saying? We're going with this, right? Like, we got to generate all the subs to the size zero, one, two, three, right? So on and so forth, right? And is, and is like the total, the current number of elements in our current array, right? So we have one, two and three, and n is equal to three, right here. You guys understand what I'm saying? Yeah. Okay. So that's what we're going to do. And in our function gen, all subs, we have to take in this value each size, right? Okay. So what else do we need? Well, um, well, we need to loop through our array. We need to loop through our array. So we need our current index that we're at, right? Like if I'm looping through one, two and three, right? I need my current index, right? Where I'm at, right? Remember, I have to pick numbers from our array, right? I got to pick numbers from each of these array, right? So I can pick number one, I can pick the number two, I can pick number three, right? So we need an index to see which number we're picking from, right? All right. So that's what we need. Um, we need our current array, right? We need our current array. Our current array is just like one, two and three. This is like the array that we're doing it. And, um, we need our subset each subset, right? Remember, we're generating all the values in each individual subset, like each of these, like zero, one, one and then two. So we need to like this array, we need this, this subset that we're adding values to it and then adding it to our all subsets, right? And then we pick another value, add it to it and then when we're done generating, we add it to our all subsets, right? That's what we need. So we need a current subset. Yeah. So, and then we need a, we need a variable for like the whole array, like the whole, the giant array, the giant array that has all the subsets, right? Remember, we need, we have this giant array and it has all the subsets like this, one, two, three, so on and so forth, right? We need that giant array that has everything. They're all subsets. Okay. So, um, what is our base case? Well, um, if our current sub sub array, right, our current subset has a size that is already equal to each size that we're generating, right? So if our current subset has a size that's already equal to each size we're generating it, we're going to add it. So we're going to add to all subsets, add it, add it to all subsets, right? Like when we, when we're done generating for a size zero or then we add it to all our subsets, right? When we're done regenerating a size one, we add it to all the subsets. We're done generating with size two, we add it to all subsets, right? That's basically the gist of it, right? And then we're going to return. So we're going to exit return. Okay. So that's this part of the pseudocode. Okay. Um, the main method, I'm going to just like solely highlight it. Okay. So add all subsets. Otherwise, what are we going to do? Well, we need to loop through each of the individual values that we're generating. So we're going to loop through from, each individual value. So let's say I'm at the current value of one, right? A current value one, well, I need to add it to my, um, my array, my array, I need to add it to my array. So, uh, I picked the number one, I need to add to my array, right? And then I need to pick the number two and I add it to my array, then pick number three, add it to my array, right? But then also I need to pick the value two, add it to my array, and then I could pick the value three, add it to my array, right? And I could pick one number one and adds my array and number three added to my right. So there's a lot of combinations we got to do, right? So, what are we going to do? We're going to loop from, let's just call a J, we're going to loop from our current index to the end, right, of our current array. And we're just going to add it to our current subset. So current subset, add the value, the current value of J to our current subset, right? So we got to add this. So we add the current, our current value that we're currently at, add it to our subset. And yeah, it makes sense, right? But then we also have to recursively go to the next element, okay? Because when we're already at the number one, and we loop through one, two, and three, right? Next time we could start at two, right? And then loop through two to three. And next time we can start at three, and loop through three to three to the end, right? So what do we need to do? We need to call gen all subsets. And this time we're going to start from, we're going to do the same thing, each size and everything. But we need to start from index plus one now, okay? Because that's the next value of our array, right? The next value of array. That's the next value that we have for our array, right? We generate all subsets, we add the next value, right? This time we're starting from two instead. So first time we started from one, and we loop to the end, add all those values that have the current size is equal to one or zero, one, two, whatever. Then we, this time we need to start at two, right? Start at two and go to the end of the array, add those values that have the size of two, so on and so forth. Then we have to start on the value three, and then add and so on and so forth, right? So that's why we got to go to the next value. That's why index plus one. But now after we generated, added that current value, we also have to do, there's a case where we have to remove it. So we need to remove it, remove current value, add J from, and why do we have to do that? Because there are certain cases where let's say I picked the number two, but there's an array that doesn't have the number two, right? Like remember we had like, let's say we had a one, one, remember we have our total array has one, two, three, right? Like our current array has one, two, three, right? Well, if I picked the number one and two, right? There's also an array that doesn't have the number one, right? And that just has the number two. So in that case, we have to remove the last element from our array every time we're generating, because there's a certain situation where we have it inside, and then we generate all the rest of the numbers, but then there's a certain situation where we don't have it. So we generate, so we remove it from all the numbers, okay? So yeah. And I believe that's the rest of the pseudocode actually. So yeah, I hope you guys enjoy this, understand what I'm going at. But basically, the gist of it is that we have, we need to generate all these different sizes of it, right? So we're going to have a function that passes in each of the size and it's going to go from zero to n. We need a current variable called a current subset, and we're going to expand it, like we're going to keep adding values to the subset and we're going to remove values from the subset, okay? So we're going to keep adding current values from a subset and we're going to remove values from the subset, right? Why we're looping from the beginning to the end, okay? And if we reach our current size, like if we reach our current size of length one, two, three, all the different sizes that we need to generate, we're going to add our current subset to all subsets, okay? We're going to add our current subset, so add current subset to all subsets and then we're going to return. We're done. Then we got to generate the next one, like next size, so on and so forth. So yeah, all the gist of the pseudocode, right? So let's actually just type this out again. Let's actually type this out. So yeah, this is basically the pseudocode, okay? And I didn't include the variables in the function because like, yeah, I don't know. But you have to declare those variables yourself. But yeah, if our current subset has size, our size that we generate is equal to each size, right, that we're generating, we're going to add it to our current subset to all subsets and we return. Now, otherwise, we're going to loop from j, we're going to loop from our j from our current index that we're at to the end of the length of the array. We're going to add our current value at j, right, our current any or each of the value at j to our current subset, we're going to generate all all subsets from index plus one now from the next value of our index. And then we're going to remove our current value of j from our current subset. Okay, so that's the gist of this whole code, right? And then in our main function, we're going to call like, remember, we're going to call all subsets where we pass in each size is going to loop from zero to n. Okay, so yeah, that's the gist of the code. That's a bit just the pseudocode how it works. And I'll show you guys the actual code. All right, so in our main method, right, of subsets, we have our current very value of nums, which is our array, and is going to equal to the num, the size of our current array, right, and we're going to generate all the subsets subsets for each size. So each size is going to start from zero, and we're going to go go a loop up to n, including n each time. And we're going to call our function generating all subsets. Okay. And then we need to create a new array, because that's the, that's basically the current subset that we're having. Okay. So now in this function, backtrack, which I call the backtrack here, but you could call it whatever you want. Remember in our pseudocode, it was like generate all subsets, but you could call whatever function you want. Okay. If our current size is equal to each size that we're generating, all subsets, which is this global variable that I declared up here, that is a 2d array of all subsets, we're going to add it. So we're going to push back the current subset that we have. So I call it Kerr here Kerr is the current subset that we have, right, then we're going to add it to it, and then we return. Okay. Otherwise, we're going to loop through J is going to equal to the index, our current index for each value that we're at, and we're going to loop up to size n, size n, right, and do J plus plus. And what we're going to do is each time we're going to add the current value of a nums of our current value that we're on to our current subset of current, right, pushback, which is adds it to the end of it of our current subset. And then we're going to call, we're going to call backtrack, right, but this time we're going to pass in J plus one, which is the next value, the next generating the next value of our current subsets. And I use the same, same variables in here. And then I need to remove it. So I call pop back. So removes the value that we just added at the end. And then we're done. And that's basically just of generating all subsets. And you could use this exact same pseudocode for almost all every other problem, you can use it. Okay. Generally, all the problems have this sort of pseudocode, right, we have a current current subset we need to generate, right, for all the different combinations, all the size of each combination. And if it's equal to the size of regenerated, we're going to add it to all combinations, right? And if it's we added it, we're going to return, we're done. Otherwise, we're going to loop through it and add each individual value to it and in the individual subsets. And we go to the next one, go to the next value, and then remove it from our current subset and so on and so forth. But yeah, that's that's the gist of it. That's the gist of this code. I hope you guys understand this video. It took it. This was like kind of hard to explain, but this is a cool concept for you guys to understand. Yeah, write comments, subscribe, I'll check you guys later. Peace.