 What's up guys? My name is Michael and welcome to my YouTube channel. Today we're going to go over the next problem of contests that happened yesterday, code 4, 6, 29. This is the kth beautiful string. I was not able to solve this problem, but after reading the editorial, I think I understand the solution. So I'm going to go over it with you guys, okay? So just bear with me, okay? So what is kth beautiful string? You're given the integer n, that's greater than two, and you have to write down all the strings of length n, which contains n minus two letters a and two letters of b in the order, okay? So here, based on the problem statement, we're just going to generate all the possible strings of length n that have two b's and the rest are a's, okay? That's what this means. n minus two letter a's, that's what this means is two letter b's and the rest are n minus two a's, okay? That's what we're doing, that's what this problem statement is saying, okay? Now here, recall that string s is less than t of length n, if there exists an i, okay? So this problem statement is just explaining the less than operator, okay? When you use compare to, in Java it's compare to, but in C++ it's less than. So this is basically saying that if you're compared to the characters and the characters are less than each other, then that's what this is saying, okay? So here they gave us this example, if n is equal to five, so our string is length five, right? And then we are generating all the possible representations of the string that have two b's and the rest are a's. So you could see, based on this input statement, there's two b's, b b and the rest are a's, okay? Here, and all these are sorted pretty much, the strings are, the order does matter, yeah? So what they did was they generated all the possible combinations of two b's and the rest are a's, and they sorted it. That's what they did here, okay? It is easy to show that the list of strings contains exactly n times n minus one over two strings. So this is the number of combinations of strings that they have, the total combinations is n times n minus one over two, where n is the length of the string, okay? So now they want us to print, given a number k, right, that is between one and the total number of combinations of strings, we have to print the k-th string from the list. Okay, so you might be wondering, how am I going to do this problem? Well, if you were to brute force everything as in write every single possible combination of these strings, all n times n minus one over two strings, right, then sort it, then get the k-th string, you would get your answer, except because of time constraints, that's TLE and that's too long. I actually did that in the first thing and I got too long. Timeline exceeded, it was not as efficient, okay? Then I tried something else. I tried generating only, I tried generating all the combinations except up to the k-th one, right? What that meant was that I, what I did was I created a max heap of size k and I added every single possible combination into the size k and get that, the k-th string. When I did that, I also got TLE, okay? I got TLE on that case as well. So, I was wondering how are we supposed to do this, okay? Without generating all the possible combinations and then sorting it and then getting the k-th string, okay? So, to do this actually, it's actually very straightforward. You have to figure out the pattern that is occurring based on what they gave you, okay? So, let's look at the example. So, we have AAABB, okay? And then AAABAB, okay? So, from this, you could see that what's going on is that no matter what happens, if I sorted all the strings, right? I generate every possible combination, whatever length it is, right? And I sort it. I know that all the A's are going to be in the beginning, okay? All the A's are going to be in the beginning based on sorting, how like, because sorting A is less than B, right? The character, the character format of A, the character number and ASCII value of A is less than B, right? So, we know that the smallest, the lowest possible, like, from k equals to 1, right? It's going to have the format of all the A's are going to be in the beginning and then the two B's are going to be in the end. And that's for any size, any size of N, okay? Any size of N. We know that's going to be the case because of how sorting works, like how they compare each character, see? Yeah, so we know that for whatever length of N, it's going to have this format where all the A's are going to be in the beginning and the B's are in the end. So, since we know that there's only two types of letters, letters A and B, if I were to just find the position of the, where these two B's are after the kth iteration of getting the kth string from the kth string, so like, after all this pattern iteration, right, where the B's are moving, if I figure out where the B's are moving and get that position of the last two B's, right? So, let's say k is four. Let's say I want to find the fourth string, right, after it's all sorted and I generate all the combinations. If I just find where these two B's are, like this, the first B, leftmost B, and the rightmost B, if I just find what positions that is after some fourth iteration, I could just add A's to the rest of the string and that would just be my answer, right? So, if I figure out a way to get the position of the leftmost B and the rightmost B, I would be able to get the string that I have to return, okay? So, that's what key value, okay? So, I'm going to do now the rest of it on paper in order to show you guys how to develop the pattern of the B's, okay? So, what I'm going to do is I'm going to write the position of every single B, every single B for each test case, every single leftmost B and rightmost B of the index for every k, okay? For every k iteration and I'm going to try to develop a pattern in order to find the answer for this, okay? So, I'm going to go to on paper now and I'm going to show you guys how to do that. Alright guys, so I drew the corresponding k's and the corresponding strings of A's and B's and then the corresponding positions of the left B and the right B. So, here the left B is 3 and the right B is 4, left B is 2, right B is 4, left B is 2, right B is 3 and so on and so forth. Since we know that the values of the B's will always be on the right for the first k and then the values of A start on the left, we know that the leftmost B will start in this position. What is this position? Since the B's are always going to be on the right and the left B is going to be 1 less than the position of the right B, that's what this position will be. So, the right B is in the position of the farmost right and that's n-1. So then our left B is going to start at position n-1-1 where n is the length of the string. So, this position is n-1 and this position is n-1-1 which is n-2. So, the left B is going to start at position n-2. So, if we could figure out where the position of the left B is after a certain amount of iterations up to k, then with the left B's position we could find the right B's position and then after that we could stop and then set the B's positions of where they are in the string and then fill the rest of A's and that will be the n string of the kth beautiful string. So, that would be what this code would do. So, first let's find the left position of the B. So, we're going to start at n-2 because that's the first starting position and then we're going to iterate downwards and as we can see based on the pattern, the B's position goes down by 1, it stays, then it goes down by 1, stays, stays, goes down by 1, stays, stays, stays. So, it either stays or goes down by 1. So, we know that n-2, the leftmost B is going to start at n-2 and it's going to iterate down to 0. Okay, and once we find the position when we stop at whatever k value is, then we could just set the position of the leftmost B and the position of the rightmost B. Okay guys, so how do we know when to stop when we're iterating from n-2, our leftmost position of our B going down to 0? Well, let's say suppose that our k that we want to stop at is 6. Okay, let's say we want to stop at 6 and we want to return this string and we want to find the position of our left B and that position is this B, right? This position, which is 1. How do I get this position of 1? So, what am I going to do? Well, let's look at how many different positions that the leftmost B could be at. Let's count. 1, 2, 3, 4, 5, 6. Okay, that'll be the number of different types of positions that it could be at. It could either stay at this position or it could be down on the right, any of them to the right, see any of these to the right, right? Leftmost B. Alright, so that'll be the total number of positions that this leftmost B could be at. So, it could be 1, 2, 3, 4, 5, 6. Now, let's say we want to stop at position at k as we go to 5. Well, how many positions can this B be at? Well, let's count. 1, 2, 3, 4, 5. So, the total different number of positions that our leftmost B could be at will equal to k. And if we were to check from looping from n-2 down to 0, then remove all the number of positions that it can't be at, we will get total number of positions that this leftmost B will be at. So, let's use this as an example. Let's go back to k equal to 6. There's 6 total positions that this B could be at. 1, 2, 3, 4, 5, 6. Alright, let's iterate from n-2. This position go down. Alright, let's say I know that it won't be at this position 3. Like, it won't be at it. So, what I'm going to do, I'm going to do 6 minus, there's 1B at this position 3. So, I'm going to take that out. It cannot be that. Alright, then I'm going to check this position. Go down, remember n-2, right? It was starting at 3. Now, I'm going to go down to 2. Well, how many number of positions are there at 2? There's 2 of these. And let's say we know that it won't be at this position of 2. Alright, then we're going to subtract 6 from 2 again. So, I'm going to subtract these 2. It won't be at this position. Alright, now we're at position 1. Let's say I know that it will be at this position. And we're going to stop and break, okay? So, this position, we know that there will be 3 positions that are possible at position 1. See, 1, 2, 3. And we'll keep that. And that would be our leftover where we stop at, alright? So, I'm just telling you guys how we're doing this. So, I'm going to start at 6. And I'm going to subtract number of positions that it can... Let's say we know it can't be at 3. So, there's 1 position that's at 3. Alright, there's 1b at 3. We're going to subtract 6 from 1. That would be 5. They'll leave us with 5, right? 5 of these. And let's say we know that it can't be at this position 2. Well, there's 2b's at position 2. So, I'm going to subtract 5 minus 2. We're like our leftover 5, right? That we have after subtracting 1. 5 minus 2. So, that'll get rid of these 2. And then I know that there's 3 left over. And the only possible way the leftmost b can be at is at these positions. This one, this one, and this one. Okay? So, that's like an example of how we're going to do it. Alright, guys. So, how do we know the number of b's where it stays at that position? Right? How do we find that where it stays at the position? Because we have to subtract that number from the total number of ways that it could be at, right? We have to use that number to discard it. Discard that number, okay? Well, let's look at this. At position 1, index 1, the b's stay 3 times. The leftmost b stays at 3 times. At position 1. Okay? What about position 2? How many b's stay at position 2? 2b's stay at position 2. Alright, what about at position 3? 1b stays at position 3. Hmm. Well, that basically essentially means the number of b's that stay at the position that I'm currently on at i is going to equal how far it is from the end. So, if you look at this, let's say at position 1, number of b's that stay at position 1 is 3, right? 3 of these stay at position 1. 1, 2, 3. At position 1, that is actually how far it is from the end. The end is at position 4, right? 4. And 4 minus 1 at the current index 1 is equal to 3. And this is the number. It's the number of times that the letter b stays at position 1. 1, 2, 3. See that? Do you guys see it? Do you guys see the relation? I could do it at position 2 now. Let's see position 2. How many times does b stay at position 2? 2 times it stays at position 2. Well, from the distance from the end for the endmost position minus 2, this position that I'm currently on at 2 is equal to 2. And that's the number of times that b stays at position 2. Well, about 3. Well, if b stays at position 3 once, right? We see a 1b at position 3. The number of times it stays at position 3 is going to be 4 minus 3, which is equal to 1. Alright? So, basically, the number of times that the letter b stays at position i is going to be the distance from the end, okay? So, I'm going to write that from here. Number of times stays at position i is going to equal to distance from the end. And what is the distance from the end? So, we know the position at the end based on whatever length n, right, is going to be n minus 1, right? Because it's index at 0, at index 0. So, our position is going to be n minus 1, right? So, the number of times b stays at position i is going to equal n minus 1 minus i, okay? Do you guys understand? Because this is the distance, because we're trying to find the distance from the end, and the end position is n minus 1. See? The end position is n minus 1 if we're indexed from 0, 1, 2, 3, 4. Our string length is 5, and position is 0, 1, 2, 3, 4. So, it's 5 minus 1, which is 4, right? This is the equation where we get from the number of times b stays at position i is going to equal to n minus 1 minus i, okay? What we're going to do is we are going to subtract this number from k whenever it is not at the right position, alright? So, we're going to subtract k minus this distance n minus 1 minus i when it's not at the right position, alright? Until we get to where it is at the right position, alright? So, when it's not at right position, we're going to subtract k minus n minus 1 minus i. So, that's the equation that we formulated from this. Now, let's say our condition is over. Let's say we got the right position that our leftmost b will be at. Let's say where our k is equal to 6, right? And we eliminated that leftmost b can't be at. So, this position we can't be at here. Can't be at here. Let's say that we know it will be at this position of 1. Now, how do I find the rightmost b now? Let's say I found this position that it's right, okay? After eliminating all the ones that are wrong. How do I find the right position, like this position when k is equal to 6? Well, in this case if we label the indexes my right position is at index 2, right? So, this is the right position at index 2 and my left position is at index 1. Now, wait a minute. Based on these choices there are only 3 possible choices that my right position could be at. It could be at here, here, and here. So, to get the position at 2, at position 2 5 minus 3 is 2. So, that means that to get the rightmost position all I have to do is take the length of the string subtract by what's left based on after eliminating all these the values of k. So, it would be n minus k. That would be the right position. So, our right position I shouldn't write not at right position. Here, let's read it right now. So, here the b of right is actually going to equal to n minus k when k is finished removing the ones that are not right. So, yeah, that's how you get minus k. After the 3 choices there's 3 choices left I take total length of 5 minus 3 is 2 and that's exactly the same as the position of my right position. Okay guys, so now, the final thing we have to figure out is when is the condition that we have to stop at. When is the, when do we know that our I value the position that we're at is at the right one. Let's go back to our 6. When k is equal to 6 we're trying to return this string and we're trying to return 1. So, when we're subtracting let's see what our k value is. So, our k is equal to 6. We start at k equals to 6 which is the total number of positions that this b could be at. So, let's iterate from n minus 2 which is at the back down to 0. Well, our b value is supposed to be at 1 so it can't be at 3. We're iterating from n minus 2 to 0 so it's not 3 so we know that it can't be at 1. Alright, when i is equal to 2 we know it can't be at 2 because our i value is supposed to be at 1. Alright, we know this because we just know from the answer. We know after labeling this from the input statement that our i our value of our first b should be at equal to 1. It can't be at 2. So, we cross these out so 6 minus 1 and then minus 2 and then now we know that our i value is at index 1. So, this is the right one that we're supposed to stop at and this is equal to k is equal to 3 but wait a minute when k is equal to 3 that's exactly the same as a number of b's that I repeated here. So, we're supposed to stop when our k value is either less than or equal to the number of b's that are repeated. Okay? That repeated at index i. So, when our k value is less than or equal to the number of times that b stays at position i that's right. So, it's less than or equal to n minus 1 minus i. Okay? And yeah, that's basically how you do it and now let's go to the code now. Alright guys, so I'm going to start reading it right now. So, here's what I have the basic code of a C++ program. This is my including statement just include all the values of the header files for C++. I'm using define ll as long long. Okay? So, that's just to make typing faster. Okay? Oh, whoops, my bad. So, first I'm going to read in test cases t. So, I'll do that. Create a t and I'll read in test cases. And I'm going to do yl t minus minus to read in every single test case. Um each test case is written on line n and k. So, I'm going to read in n and k. Okay? So, read in n and k. Okay? So, now after that I'm going to do the code that we just did. Okay? So, I'm going to start at let's see. Okay? I'm going to start at 4 i is equal to n minus 2 and I'm going to go up to down to 0. Okay? That's the position that we're checking at from leftmost position, right? Down to 0. Um then I need to find the distance that I need to find the number of times b stays at position i. So, I'm going to call that b stay at position i. Um What the heck? What's this? What's going on here? Oh, l. Okay. Um I'll do ll then. What's the for loop? Counts up to max. Oh, whoops. Minus minus. What is going? What's the problem? Define for loops counts up from maximum. i equals n minus 2. Okay. Yeah. Now it's gone. Okay. Um, now I'm going to find the number of times b stays at position i and that's n minus 1 minus i. So, num times stays at i num times b stays b stays is going to equal to n minus 1 minus i. Okay. Um now I need to do the do do do do do do do do I need to check if k is less than or equal to n minus 1 minus i then do it. So, if k is less than or equal to num times b stays at i and that means I'm at the right position right? That means I'm correct. I'm done. Alright. That means I'm done with the loop. So, I need to set my b my left b position which is i and then my right b position. Okay. So, the left b position is do do do do do Okay. So, I'm going to create a string that has all values of a. So, I'm going to call this current string and it's just going to have uh how do I fill a string up with all a's? Hold up. How to fill up a string with all a's? Characters in C++. Let's see. How do I do that? Do do do do do do my internet's bad. Sorry, guys. I forgot about this. No, I don't want to use memset. Um Oh, I could just do this. Okay. With and then the character. So with is going to be n character is going to be a Okay. So, this is going to create a string of length m and then all the values are going to be a now now I need to essentially just uh so I know my left position my b at left position is going to be i so I'm going to do current that i is going to equal to be okay that's my left position my be left position right be left uh yeah and then I need to do current string at n minus k is going to equal to be also and then I'm going to break I'm just actually I'm going to see our current string and then I'm going to break okay so I'm going to exit out the for loop uh now otherwise if it's not less than number of times b stays I'm going to subtract k minus equals to num times b stays okay and yeah that's pretty much all the code is that there is to it let's run it mmhmm okay um so 7 7, 5, 1 a, a, b, b alright that's right uh 5, 2 a, a, b, a, b yeah that's right with the output 5, 8 b, a, a b, a, b, a, yep that's right 5, 10 b, b, a, a, a, yep that's right 3, 2, b, a, b, yep that's right and then let's try 20 and 100 and that's right also a, a, b, a, a, b, yep okay let's save and then submit this oops I don't need to save it I'll just doo doo doo doo doo do do do do do do do do do do and it got a seed yeah, I got it accepted okay so that's how you do this problem and I'm actually gonna wake up really early tomorrow. Raycom subscribe, I'll check you guys later, peace.