 What's up guys? Mine is Michael and welcome to my YouTube channel today. We're gonna go over this leak not leak code I've had code forces 631 be okay. It's Jamin likes permutations. So you're given a sequence M integers And it's called a permutation if it contains all the integers from one to M out exactly once Number M is called the length of the permutation. So they are here. So Jamin has two permutations Both have length L1 and L2 and then he concatenates these two. Okay. He adds them both Permutations L1 plus L2 so first L1 elements of a is a permutation of P P1 and L2 as a Permutation P2 so now you're given a sequence a and you need to find two permutations L1 L2 Find the two permutations L1 L2 if there are several possible ways to restore them you should find all them Okay, so basically the problem statement just wants us to split into two permutations such that All it would contain all the values on from left side is going to take all the values from one up to The length of the permutation on the left side and all the values from on the right side Going to take contain the values of one to the Other side of the the length of the array. Okay, so that's basically what they want us to do and Then we have to print each of those If there's different multiple possible ways of doing it So I'm going to show you what I mean on pen and paper and then then after that I'm going to show you the code All right. All right guys, so I'm going to explain to you guys how to do this problem where You're trying to split the the the array into two parts where two permutations and one each of them Well, there are several ways to store them or you have to restore them all and each permutation has to have Contain all the integers from one to M. At least once. Okay, so we're trying to split these The two arrays. So if you look at this look at the first test case The answer is One one four or four one. So and the reason why is because if I split let's look at four one first Okay, let's look at four one first. So let's look at four one and then one four. All right Four one four. Let's look at four one first reason wise because if I would split at four Right all the values from here to here Contain one to four Right one to four and then all the values of here from one end contain all the values from one to one Right. So the so in order to if you think closely the we're trying to split The array into two parts such that all the values contain between from one up until the length of Where we're splitting and the one from the other one is well It has to contain all the values from one to the length of where we're splitting also So if we look at here one to four the reason why it's Four one reason why it's four wise because you're splitting from four all the values from one of from Here to here up to four before four has the values one to four and the ones after has the values one to one and The reason why one four works in the first case because let's say I'm splitting from here all the values from one to one are In the first array and all the values from four afterward From one from four from four afterward, right? They contain all the values from M of one to four. Okay, so that's the reason why these two these two these two These two numbers work right these positions that we're splitting at work. Okay, so now let's look at this this this input statement Reason why this works is because if I'm split at four Right It's like cut this array here all the values from here to here Contain numbers from one to four and all the values from here to here contains numbers One to two Right, so that's how you that's how this problem is that's how this problem statement works, okay? so now if you realize this is that The only way So I'm trying to split into two arrays, right? The only way this could possibly work is if At least one of them is going to be the maximum Right the maximum Right if I were to go through The the first array right if I were to go through all the values in this array Let's say I go through all the values here in this rate and I'm going to split at some some position Right, we have to know is that it has to contain all the values from one up to the maximum number Okay, that's the only way this could possibly work for any of these arrays, right? If it doesn't contain one up to the maximum number It probably it can't work it absolutely cannot work. Okay, so if you look at this Here here. This is the maximum number four, right? You're a we are able to split this from the numbers from one to four At position four is because all the values from here to here contain up from one to the maximum, which is four Okay, so that's how this this array works. That's how this algorithm works. It has to either For the last left side has to contain all the values from one to the maximum and the and let's look at the right side The right side has to contain all the values from one to the So left side contains all the values from one to the maximum, right? So here this is the maximum Let's let's say this is the maximum the right side has to contain it for n minus the maximum Okay, it's the leftover values. That's the reason why it has to contain it. So if Let's say there's a one two three four five six. There's six numbers, right? The left side has to contain all the values from one to the maximum, which is a four One to four then the right side. What is six minus four six minus four is two So the right side has to contain all the values from one to two. Okay, so here that's how we do this algorithm, okay? There's two scenarios. The first scenario is that the left side contains all the values for two to the max Then the right side of one of the max the right side has to contain all the values from one to n Minus max where n is the total number of numbers number number of total numbers, okay? It's length of the right This is the second case, right as we saw in the above in the second case is when the left side contains one to n minus max and the right side contains Values from one to max. Okay, so these are the two This is the left side and this is the right side Those are the two scenarios that could actually possible happen in this in this In this problem. Okay, because we are only splitting up to two Arrays right and because of these two arrays The only possible permutation that works is if it contains one to the max on the left side Or the right side contains a n one to the n minus max So how do we check? How do we check that all the left values on the left side contain one one to the max? And all the values on the right side contain one to n minus max well, you could literally just create a you could use an You could use a map a hash map or an array and literally just have For every index that is in that array from one to max set them all as false and then loop through here to here From here to the left side one to max and check Whichever one that occurs there you're gonna set that as true whichever that doesn't occur They said as false and then in the end you should be able to get the The ones that you should be able to check if they all contain the values from one to max And I'll go over the code right now with you guys all right guys So I'm gonna explain the code now how the code works to you guys. Okay, so first of all What did I do first? from first of all to read in a test cases qt and of course you have to read in test cases t Then I had to read in the length of the array to read in the values of length of the rate And yeah, I had to do that. I had to create my array And what I use I use a set and the reason why I use a set is just to avoid duplicate values Okay, I don't think it really matters for you to set or not But I did it use the set I use the set and I used a pair to add the two lengths the two lengths that we have to have Okay, the two lengths that we have to have So this set is gonna contain Two of the pairs which is like gonna be this is gonna have all the values of the two pairs that are gonna have the left side Like the left side and the length of the right side. Okay, so that's what this set is gonna contain I'm gonna have a value of max number. So what do I do first? I read in all my All the values in my of the array first because that's what you had to do that Like there's no other way to do that right then I had to get the maximum number so I got the maximum number by calling the max function and I get compare every single element of That I'm reading in with this max number So I got get the max the the maximum possible number from the array the max number The array and then I had to pass it in okay So I passed in the max number into this function and then I'm gonna pass in the array Okay, I didn't really have to do that this to pass in the array You probably could make the array a global variable, but I had I did that anyway just cuz alright So once I pass in the number and then the array What I do I had to first create a map to check the first part so for that I kept track the number of occurrences of every single number in the first part right from one to One to the maximum number right so I had to do that so here first I kept track of the I went through a loop through the array again, and then I kept track of the Every single number of occurrences. Okay, that's what I did then after that I Loop through from yeah, wait my bad my bad, okay, so here This array is this for loop loops from zero up to the maximum number right like the maximum number we that we got on The first part in before we got the maximum number of the the total max number of the array right The maximum value the total max value of the array So I loop from zero to the total max value of the array Which is that position of the maximum value that we're splitting at and then I count the number of occurrences of that of Each value account the number of occurrences each value after that I loop from one to the maximum number of array again, and I have to check that I have seen every of them I had to see every single value once. Okay, so if I saw zero if I saw zero values, right? I return false. Okay. I had to see it at just once. I had to see it once, okay? all right then Then after that I had to check the rest of the array have to check the rest of the rest of the rest of the array Okay, so the rest of the array I created a separate map which Checks the number it counts the number of occurrences from after from that position after to the right side that I'm splitting Right and to the right side. I just kept tracking number of occurrences by doing a check second part plus plus Okay of d of i plus plus. Okay Now I had to make sure that I count all of them once. Okay. I had to count them all at once. Okay, so from Here here on forward I'd looped again, and I'm going to loop from one up to the n minus the maximum number n minus the maximum number Okay, and I basically had to check make sure that the number of occurrences this part check part second part at i is has to be If it's zero then I return false, right? It has to be one from right side. It has to be one. Okay Then that means, uh, yeah From the right side has to be the number of occurrences for each element has to be one, right? Because if it equals zero I return false so then after that after this whole thing I return true if uh If I passed if Both of these case cases I passed right like I never returned false and I'm going to return true Okay, so then after if this if it does if the check works, right for a maximum number from A zero to the max number, right? Then I insert in my set maximum number and then n minus maximum number If it uh, yeah, that's what that's what I do Then I check from I have to check the other way also, right? Uh, remember there was two cases There's a one to max number and then the left side can also have One to n minus max So I had to check that also So I checked the left side of n minus max and then I do the same thing If it works, then I'm going to do n minus max number I add n minus max number for the left side for the left pair and then the right side I'm going to do add maximum number. Okay, then after that I just print out the size of my set and then I go through every single pair and I print them out And yeah, that's what I did to get accepted. So that's how you do this problem Um, you could you there's multiple ways you could do it you instead of using a map I use the map you probably could use a simple array and just like Making sure all the values are zero or one, right? You could use a I don't know you could use a bit set actually could you could technically use a bit set? All right, uh, you just have to make sure that that Where you're splitting has to contain all the values from one up to that One up to that number, right for the left side and the right side So, yeah, that's all you have to do for this problem right come subscribe. I hope you guys understand this problem I hope I explained it to you guys and yeah, I'll check you guys later. Peace