 What's up guys, my name is Michael and welcome to my YouTube channel and today what we're going to do is we're going to do this problem. I saw this problem on the code where we have an array and we need to split it into k sub arrays, such that the maximum of each sub array is as large as possible, right? Now now we're going to do a variation of this problem where we're going to basically just split the array into k sub arrays, such that just all the possible get all possible combinations of it. Okay, so what do I mean by this? Let's say our array is 123456 and we need to split it into k equal to three. So we're going to split into three sub arrays. And what I mean by a sub array is that they're contiguous sub arrays. So I take one and then I split it by one, then I split by two, and then I have 3456. What I'm not doing is I'm not taking one and then taking four. Okay, I'm not taking one and then taking six. Okay, so as you can see here, we have 123456, right? We're one and two are individual sub arrays, and then we have 3456 as the last sub array. And in each of these, the length is only three, right? We only have three sub arrays in each of these because k is equal to three. We're splitting the array into three different sub arrays, three contiguous sub arrays. And the second one, we're also splitting it by three. So you have 123456. As you can see here, we have 23 is three is was in the previous in the previous array. But now it's in the same array as two. So we have 23456. Then we have one, then we have 3234, then we have 56, right? Then we have one, we split by one, and then we have 2345, then we have six. Now the next one, we cannot have one, then 2345, and then add six to it. We cannot do that. And the reason why we cannot do that is because then our k is not going to equal to three anymore, right? Our k would only have, our k would be, would only have two values, right? We need to split this array into three groups. So that's why this is the last possible third group you could have in this situation of one, then 2345, then six. Now in the second, in the third one, we have one, two. One, two is in one group now. We have three, then we have 456, then we have 12, then 34, then we have 56, then we have 12, then 345, then we have six, then we have 123, then we have four, then we have 56, then we have 123, then we have 45, then we have six, then we have 1234, then we have five, then six. And the reason why we cannot have this five inside this one, two, three, four is because then we won't have three subgroups, okay, we won't have three subgroups, and we'll try to split it by, okay. So I think you guys understand what I mean by this, and by splitting it into three different subgroups, okay, sub arrays. So how do you do this problem? First of all, we need to each row here is going to represent a different combination that you could split it by, right? We have one, two, then three, four, five, six, that's one possible combination. Then we have one, then one, then two, three, then four, five, six, that's another combination. So each of these rows are individual 2d arrays, right? If you could see here, we have each of these rows are 2d arrays. So the first thing we need is we need a global variable called whatever it is, whatever I'm going to call it all combinations, where it's a three dimensional array, like a three dimensional array, and each of the row is going to represent each combination that you could possibly have. And in each row, it's a two dimensional array, right? We have one, two, then two, four, five, six, then we have one, then two, three, then four, five, six, then we have one, two, three, four, then five, six, and so on and so forth. So the first thing you need to do is I'm actually going to create, recode this up as if I've never coded it before, okay? So here I have, yeah, I'll just recode this up since I never, assuming I never coded this up before. So I'll code it up for you guys. So let's say I have my array of one, two, three, four, five, six, it's a vector. Then I need my all combinations. And all combinations, like I told you before, it's just going to be a vector vector vector integer, right? It's going to just be a three dimensional array where each row is going to be a two dimensional array of each sub arrays that we're splitting by. So if I'm going to have this function, I'm going to call, I'll call it solve. I need all combinations, right? I need it. And the reason why is because in each combination that I'm generating, I need to add my new combination into the all combinations, this variable of all combinations every single time. Okay. What else do I need? Well, I need my array, right? If I don't have my array, I can't split it. So I need to pass that in. Okay. What else do I need? Well, I need my K value, right? Because I need my K, like I need to split it into three different sub arrays. And if I don't have my K, then like, I don't know how many sub arrays I'm splitting it by, right? I don't know how many different arrays I could split it by. So that's why I need my K. Now, now it's going to get a little tricky. It's going to, this is going to get a little tricky, okay? As you can see here, we have one, two, three, four, five, six, right? The next one we have one, then two, three, right? So this, the, the location that we're splitting by the index we're splitting by got moved up by one, right? We have two, three. So this little next location was now split this two, three was originally in three, four, five, six. Now it's coming up to two, three. So that's where this is now. So that's split about that. Then the four that we split it by the index where we split it by, we originally split it by three. Now it's four. So now this four is now in two, three, four. So what we need is we need an index that we're going to keep incrementing up by one as we recursively call this function. And that's going to represent the location that we're splitting by. So we need to pass this in. I created an index called zero index, index equal to zero, and I'm just going to start from beginning and yeah, I'll pass them into the pass end to my function. Okay, here's the part where it's going to get a little tricky. So we have each our total combination array, right? That's three dimensional, right? What do I need now? Well, in each row, it's a new combination, right? It's a new combination of different sub arrays that I'm splitting by. So we have, see in each row, we have one as it's individual, then two, then we have three for five, six, then this one is going to be one, then two, three, and four, five, six. Then the next one is another combination of one, two, three, four, then five, six. So in each row, what we need is if we're going to keep modifying our previous previous row, we need to recursively pass in a combination array, a combination array that has each sub array inside of it, right? A combination that has each individual sub arrays. So this row is going to represent how many different sub arrays, and in each of them, it's going to have each sub array. So what is that? That's a two dimensional array, and in which each of these rows are the three sub arrays that we're splitting by. So we need to have this vector, vector into each combination, I'll call each combination. Okay. And then what else do we need? Well, we need to the, since we're recursively adding this new sub array into each row, right? We would also need is that we need each individual sub array, okay? So look at if we have each combination, each of these combinations have individual sub arrays that we split it by one, then two, then three, four, five, six. So because of this, we need to our each sub array, which is going to be just a vector of integers. So we have this vector integers, each sub array. This is going to be the each group that we're splitting by each, these individual groups. So we're going to pass it in. So here I have, I'm going to pass in, have my array, my K, I'm splitting by the index, and then I'm going to have each sub array, then I'm going to have each combination. And then I have all my combinations that I have. Okay. So now let's actually write this method. And then we could do this, we'll be on our way. So I'll just take this out and just record it void, solve, put this here, or whoops, and I'm going to write the method types. So the first array is just going to be vector integer passing in the K is just going to be an integer. Right. The index is just going to be integer also, each sub array is going to be a vector of integers, right? Each sub array is a vector of integers. Each combination is going to be a vector vector combinations. All combinations is a triple vector, a 3d array. So I believe that's right, missing one. Yeah. All combinations. Okay. So let's see which one we need to pass in by value and which one we're modifying. So we're going to pass in by reference for array because, yeah, it's not good to keep a copy of that. And the integer is we could leave that is that each sub array, I'll explain why we're not passing by reference later, later afterwards, separate and then we have each combination. We're going to pass by reference because we need that and then all combinations we're going to pass by reference because we need that. Okay. So what am I going to do in this function? Well, I'm going to recursively call this function called solve. And I'm going to pass in my array. Okay. And how am I going to do this? Well, every time I call this function, I'm going to split the array. I'm literally just going to split the array, right, into its individual sub arrays. So every time I split an array, the number of remaining sub arrays gets decreased by one. Do you understand? Because if I have three, if I have three, three groups I need to split it by, right? I have K equal to three. And then I've already split it by one. So I've already split it by one. Then I have two remaining groups I need to split, right? Then once I split it by two at this two, split it at this two location, then only I only have one group remaining that I need to split it by. Then once I split it by here at this after this two, then I have three for five, six, then I have zero groups I need to split it by. And then at that point, my function ends. So every time we recursively call this function, I'm going to decrease my K value by one because every time I split it, the number remaining number of arrays I need gets decreased by one. So I'm going to use K minus one. Now, what about our index? Well, what we're going to do is, as you could see, looking at through this pattern, is that we need to increment our index by one every time, okay? So we have two, we have one, and then two, right? In this case, two was split at this part right after the two, okay? Then we have three for five, six. Then here we have one, then two, three. So the location that we split it by is now after the three, and this three is actually in the same sub array as the two. So we split it there, and then we have four, five, six. Then we have one, then we have two, three, four. So we're going to split it at this location at four. Then we have two, three, four, and then we have five, six. So every time I call this function, I'm just going to increase my index by one. Because that's going to be how the location that I'm splitting by gets incremented by one every time. See, four, five, and five, six, and so on and so forth. Then I have my each sub array, and then I just have my each column, each column, and then I have my all combinations. Okay, all right. Okay, so what do I need to do now? Okay, so we have our recursively call our function here. What do we need to do now? Okay, well we need to, every time we recursively call this function, we need to add our current sub array that we're splitting by to each row of the combinations right. So as you can see here, let's say we have one, two, three, four, five, six, and I split it by one. Well, let's say I already have this one here, that I already split it in my new sub array. I need to take this one and add it into my, each combination that I could possibly have. Right, because if I have one, here I split it by one, I need to add this one into my combination array. Right, then if I split it by two, like right after two, then I need to take this two that I just knew sub array that I created of two, then add it into my new combination array. Then if I split it by three, four, five, six, I'm going to take the sub array and add it to this new combination array. So if the K value, and not K, if each sub array is not empty, right, if the area that I'm splitting it by is not empty, then I'm just going to add it to my combination array. Right. Each of these, after you split it by, you need to add this new sub array into your combination array. Okay. Now, now we need to think about our base case. What is our base case? Okay. So we are decreasing the number of sub arrays. We're splitting each sub array, right? We have the original one, two, three, four, five, six. We're splitting it by one, and we split it by two. Let's say I'm on the last sub array. So what is that last sub array, right? Because we're decreasing the sub array every single time we're splitting it, like the number of sub arrays K minus one every time, the last one is going to be when K is equal to one, okay? So like if K is equal to one from here, this location, right? So let's say I split it by one. So now originally I had three sub arrays I needed to split by that got decreased by three. So the three minus one is will be two. Then I split it after this two, then this two gets added to the array and it gets decreased by one. So now then now I have one, when K is equal to one, that's my last sub array. So that's going to be the last sub array that I need to split it by. Okay. So let's say I'm on the last sub array when the case equal to one, what do I need to do? Well, I need to loop through from the index and just add all the remaining values that I need, right? I don't need to like, like pop things or push anything. At this point, all I need to do is start where I'm starting at this index. I'm sorry, I just loop through to the end and add it to my last sub array. And then I need to take that and add it to my combination, right? So that's all I need to do. So here I'm going to create a array called last. And I'm just going to loop through from my index, start from I into my index, I'll loop through to the end. And then from here, what I'm going to do is I'm just going to take last pushback added to my array index I, and then from here, I'm just going to add it to my combination array. So each combination. Okay, you guys understand what you guys following along, we're going to push by added to our last combination. Okay. And then I'm just going to return after this. Because we added to our last combination to your basics. And then once we're done with this, we just return it. Okay, we just return it. Right. Return. And then we need to get our new combination. Now let's look back at into our code. Let's look back into our code. Let me just make sure I follow this along. Okay. Yeah. Yeah. Okay. Yeah. Pushback. Okay. Yeah. Okay. Now, now we're going to add this last array into our combination array. Okay. Now there's some tricky situations where the last, like where we're splitting back could be empty. So let's actually check if it's not empty. It's just like a case of like every time you add something, you should check if it's not empty because otherwise it might give you like an empty thing. Okay. Okay. So now we push back or each combination array, then we need to pop it back. Okay. So each way you got pushed back. Okay. Each combination. Once we pushed this back of our combination, right, one, two, three, four, five, six right? I need to add this to this type of combination into our global array, our total array, right, that we have. So here what I'm going to do is I'm going to go here, I'm going to go to all combinations and I'm just going to add the current combination. Okay. Here's going to be a little tricky part. It's going to be backtracking. What do you see here? We have one, two, then three, four, five, six. We have each combination, which is one, then two, then three, four, five, six. Let's say we have this first combination already. Look at the second combination. Do I really need to do, what is the difference between the first and the second? The only difference is the location I'm splitting by, right? The first two values are still there, one, two. And the only difference is that now my location I'm swimming by is there. So do I really need to copy all the values from four and put it here? Really I don't. I still have my previous combination, right? My previous combination is one, two, then three, four, five, six that I generated. Now what I could do is I could just, the only difference that we have from here is just the last element, right? This last element is three, four, five, six. What all I have to do is literally just remove this last element, okay? And the reason why is because if I just remove this last array, all I have to do, right, I have the one and the two here already, all I need to do is just take the next index, the value of the next index and just add it to my previous array of three and two and three, right? Do you guys understand what I'm saying? Right? If I, if I'm on the last one, right, let's say I already added this into my new combination array, I don't need to copy all the previous elements. All I have to do is just remove the last element and then add, add it at a new element, change the last array again and then add another combination. So for that, what I'm going to do is I'm going to just do each combination dot pop back. What this does is it removes the last sub array of three, four, five, six, that's gone. And then I could just go to the next one, add the three into my previous two, and then add the four and five, six, then I just remove four, five, six, add four into my previous two, three, then I have five, six, there's removed the five, six, take the five, add it to my previous two, three, four, five, then remove, then that's it. Okay, so that's basically our base case. All right, we have our base case, we have the, what we're adding every single time and so on and so forth. All right, now we need to think about how we're going to pass and solve. Okay, so each of the array that we're having is going to be this, okay. Let's say we're at two, one, two, and then three, four, five, six. We added this already. Then we go to the next one, right, we go to the next one, okay. Now we, how do I get from here one, two, and three, four, five, six, two, one, to this array of one, two, three, four, five, six, okay. So let's say I already popped the last element of three, four, and five, six, right. What I need to do now is that I need to generate the next possible array, right, the next possible array. So the next array I need to split by is going to be four, five, and six, right, I move the index up by one, I added the three into the previous one, and then I need to add four, five, and six. So the next array that we're adding is has to be empty. So what I'm going to do is I'm going to actually clear each sub array. So I'm going to do each sub dot clear, and what this does is it's going to completely erase all the elements in it, and then have our a new sub array that we're adding. Okay. So now what am I going to do? I need to loop through. I'm going to loop through here and just continuously add the next, what am I missing? Oh, so remember, let's say we have two, three, one, two, then three, five, six, what am I going to do is I'm going to go to the next location I'm going to split it by. So we have one, two, then three, four, five, six, right. I'm going to go to three, then split three from five, six. Then after this index, I'm going to go to four, split it, then I'm going to five and split it. Then when it calls go back to the beginning of one, two, I need to loop through from here to the end again, and split it after the two. Then we have three, split it there, then four, go to the loop to the end, split it one, two, then three, four, then I split it for four, then go to the five, six, then split it, so on and so forth. So in this case, I need to actually put this into a for loop. And I'm going to just increment my index to split it every single time, okay? And yeah, now every single time I call this, I need to add a, every time I add a loop through here, and split it of where I'm increasing my index to split it at, I need to essentially is regenerate a new array, right? So like we have two, then you have three, right? You have two, this two was originally two, and then it becomes two, three, then you have two, three, four, so on and so forth. So before I generate this, I'm going to actually push back the current value at my current index, okay? And then, yeah, then after I push back the current value at the current index, recursively call my solve function, then, then what I need to do is just check if the combination array is empty. Okay, so like let's say I am at, yeah, so let's say I'm at like one, this part one, two, three, four, five, six, then I move to my next index to split at three, then I split at four, then I split at five, and I'm done here. This recursive call is going to get at one, two, okay? So how do I get from one, two? Well, simple, I need to go back to my previous combination. Each combination I need to pop it. So remove the last elements. So before here, we would add like one, then two, three, four, five, then six, right? At the end of this, it already popped the six out to generate the next one, right? And then it tries to generate one, and then two, three, four, five, six, where the six is at the previous one, and it can't add it, right? So then it pops, it goes back to here. So now, now we're going to have just one single element of one. Then in order to get the next one, I'm what I'm going to do is I'm going to pop and remove the two, three, four, five. All right, I'm going to remove this previous, previous array, sub array in our previous combination. So to do that, I'm just going to go do if each sub is not empty. I believe it's each sub. Yeah, your sub is not empty, I'm going to pop it. So yeah, then when it comes back up to the top, it goes to the next one. So it'll generate the next one, and so on and so forth. So yeah, that's basically the gist of the code. I'll just run it real quick for you guys to show you what it does. Make sure this runs properly, actually. Oh yeah, I should show you guys how to print it out. Because now you have like all the possible combinations, but you don't, you can't print it out. Okay, I don't know why it's sticking forever. Hold up, I plus one new sub club. Each sub array is not just pop it. Each sub array. Oh wait, each combination. Oh, my fault. This should be each combination. So yeah, each time you push the sub array back. Yeah, that was supposed to be each combination. All right, so every time you call this, and you finish your combination of one, two, three, or five, right, you need to pop the last combination out because, you know, when you generate the next one, you pop the six, you pop the one, two, three, four, five, then you can go on to one and two. So you need to pop the combination. It's not the sub array. The sub array is just the individual element here, right? So yeah, yeah, I'll show you guys how to print this now. So what we're going to do is we're going to loop through every, our three dimensional combinations. I'll print out a bracket, then I'll loop through from j equals zero to all combinations outside, print out another bracket, I'm going to loop through from K to all combinations outside. So I'm going to go through all my, all my, my two dimensional, three dimensional array, and we go through every single two dimensional, two dimensional array inside of it. And then I'm just going to go through every single individual array and print it out. So go through three dimensional array, print it out, two dimensional array, print this out, and then each individual array, print it out, then just, if it's not, but yeah, if I'm not at the last element, now I'll just print out a comment at the end. Then at the end, I'll just print out another bracket. So yeah, and then in each of these, I print another bracket also. So that's pretty much just the code. Hope you guys enjoy this video. This is a fun video to generate all possible, split it by K combinations. Yeah. Hope you guys enjoyed this video. Great, come subscribe. I'll check you guys later. Peace.