 What's up guys, my name's Michael and welcome to my YouTube channel. Today we're going to do the next problem, which is ternary XOR, which is actually a misnomer because the solution doesn't use XOR at all. But whatever, we're going to do this problem. So a number is ternary if it contains only digits 0, 1, and 2. So for example the digits 1, 0, 2, 2, 1, 0, 22, 11, 21, 2002. And the reason this is because they all contain only the digits 0, 1, and 2. So you're given a long number X, a long ternary number X, and you know that the leftmost digit is going to be 2. And the rest of the digits are going to be 0, 1, or 2. Well define the XOR operation of A and B as C is equal to A, X, or B of length M, where C of I is equal to A of I plus B of I mod 3. So what they're doing is they're going to take the digits, add the digits up, and then divide by 3. Find the remainder when they divide it by 3. So that's what they're doing here. This is actually not how the XOR operator works, but they redefined it as a ternary XOR like this. Your task is to find numbers A and B, ternary numbers A and B of both length N, both without leading 0s, that A ternary XOR B is going to equal to X. And max AB is the minimum as possible. So they basically want you to minimize the max of AB. So that basically means that AB has to be about the same, the magnitude has to be around the same. And you want to minimize that. So you want to minimize that. Alright so you have the first line of input contains test cases and the rest contains just ternary number X considered of N digits 0, 1, and 2. And yeah, it's guaranteed that sum of N does not exceed 5 times 10 to the fourth. Okay that's good to know. So to do this problem, you actually have to do it by hand. So I'm going to show you guys how I came up with the solution. I got AC, which means if I had skipped a second problem, I probably would have solved this question and I probably would have scored way higher than I did on the contest. So without further ado, I'll tell you guys what you see in the pattern. Basically compare the inputs and then the outputs and then you will see the pattern. Let's look at the first input statement 22222. And then they split the 22222 into this A and B and we see that all the values are all ones. OK, so it splits the 22222 into 11111111111111111. OK so from the first input statement, I could probably guess that if I see a 2, I could probably split into 1-1. Just from looking at the first input statement. Okay, maybe that's right. I don't know. Okay, let's assume it is, okay? I still got the second input statement. Okay, so I have a two and they split the first digit two into one, one. Okay, that seems exactly like our first, what we assume it was, which is split the two into one, one. I see a one and they split into one, zero. Okay, so that means that if I want to have two numbers where it's the minimum of max A and B, right? One of them has to be larger than the other, but I want both of them to be as small as possible, if that makes sense, right? Yeah, okay, so that's, because I want to minimize A and B, but I want the, like I know one of them is gonna be larger than the other one, and I want to minimize both of the two numbers, okay? So that's what I wanna do. So this one, this one became one, zero, right? So that means I know the top one is gonna be larger than the bottom one. So I know A is gonna be larger than B, just I'm seeing at this. So if I see a one, maybe I'll split it as one, zero. Okay, I'll set the first of two strings. I'll set the first one to have one and the second one to have zero. Okay, maybe that will do. Okay, that seems reasonable. Okay, I see a two again, but instead of splitting one, one, I see zero and two. So there must be either, there must be certain condition that caused me to split the two to become zero and two instead of one, one in the above, in the above. Because we saw that in the above, they split into one, one evenly. So it might have to do with evens or something, I don't know. So that's what we have to think about. We have to think about this, okay? We have to think about this. Okay, then I see a one and it split between zero and one. Okay, that makes sense. That's exactly what we assumed that we got from the first number one. But instead of putting one on the top and zero on the bottom, they put zero on the top and one on the bottom. Okay? Well, what does that tell me? Well, I know that the first A is gonna be larger than B, right? Because they put a one on the top and then zero on the bottom for the second digit, right? So I know that what they're doing is they're making A larger value than B, okay? And then they put, instead of putting the larger digit of splitting the one into zero and one, they put the smaller digit of zero on the larger number. That's what they're doing, okay? So instead of putting the one, like the large digit of one in the larger number, let's see, in A, instead of putting the larger digit of one in A, they put the smaller digit, they put the zero in A and they put the larger number in the smaller number B. Right? Okay, I'm gonna reiterate that. It's the same thing. Okay, we saw from the second digit that they put the large digit one in A and the small digit zero in B, okay? When we saw one, they split it between one and zero. Okay? So what they're doing is they're saying this, they're making sure A is a larger number and B as a smaller number based on the places that they put, right? From adding, like, you know, comparing the values of ones and zeros. So they've set A as a larger number and then B as a smaller number. So when they saw the one again, instead of putting the zero in the larger number, instead of putting the zero in the smaller number, they put zero in the larger number, okay? So from this statement, I know that a certain condition happened and they're gonna put, to balance out the values of these two strings to prevent one to being super duper large and the other being super duper small, what they did was they put the larger digit, the smaller digit in the larger value and the larger digit in the smaller value, okay? If that makes any sense to you guys, I hope that makes any sense, okay? So that's what they did here. They did that the same thing for the next one. And the next one, instead of putting the larger digit in A, they put zero in A again, okay? So they put the smaller digit, they put zero in A and put one in B. That's what they did. They put zero in A and they put one in B. So they're not maximizing A. We know they're not maximizing A. Otherwise, they would put all the ones into A, right? They would put the zeros into B. So we're not maximizing the A as a larger size possible, okay? So what they're doing is we're gonna make sure we're balancing these two values of A's and B's to make sure that when I do a minimum of the minimum as possible of max A and B, that'll be the minimum as possible, okay? So that's what they're doing. They're making sure that these values are balanced, okay? Okay, so that's what we got from the second input string. Okay, the third input string, they put two. What does that tell us? Well, they split two into one and one. So that's exactly what we guessed in the first input statement. Whenever we see a two, we're gonna split to one and one, all right? Except in this case, in the second input statement, we saw that for a certain condition, this two split between zero and two, okay? So only in the second condition that this anomaly happened, so we're gonna ignore that for now, but we are going to come back to it later as we develop more of a pattern, okay? All right, in the third, in the fourth, the last test case, okay? We saw a two and they split between one and one. Okay, that makes sense. That's exactly what we saw in the other two statements. Then they saw a two and they split to one and one again. Okay, that makes sense, makes sense. Okay, we see a zero and they split between zero and zero. Okay, so if I see a zero, I'm gonna split it between zero and zero. So that's what we know now. Okay, so what we know, I'm gonna put a note. This is what we know. If C two split one one, split A to equal one, B to equal to one. If C zero split A is equal to zero, B is equal to zero. Okay, so digit, when I say A equal to zero, B equal to zero, A equals one, B equal one, this is like adding the digit, right? I'm seeing like, this is just like notes. Yeah, okay, let's look at the next digit. Two becomes one, one again, okay, that makes sense. Two becomes one, the next one is two. Two becomes one, one, okay. Two becomes one, one, zero becomes zero, zero, okay. Two becomes one, one, and then one becomes one, zero. Okay, well, this seems exactly what we saw already. So this like further solidifies what we saw. So we saw that one splits between one and zero. So if C one split one, one, one, one, one, one, one, one, and split to one and zero, except if, except later on, we put, we switch it to zero and one, except if, on the second condition, except if condition, whatever condition that happened in the second one, we are going to put zero and one instead, okay? So let's look back, we have to look back at the second, because this is the second input statement that is an anomaly to us, that is causing some strange things to happen, okay? So let's look back at it again, okay? If we see a one and a zero, they put one and zero, okay? So they have a larger number and they put one and zero. Then later on, there's a two here for some reason and I don't know why, but that's just how it is. Then they put, let's see, zero and one, for one, they put zero, one, but differently, differently. They put zero, one, differently. So they put zero into the larger value and one into the smaller value, that's what they did. So what our condition is, is right here, split into one, zero, except if already occurred, occurred put zero in larger value, one put one in smaller value. Okay, so let's look back at it again, okay? It must be a certain pattern that is occurring. Why did they put two here, zero and two? Why did they put zero and two here? Now that's the big issue. Why would they put zero and two here? And then why would they put zero and one here? And why would they put zero and one here again? Wait a minute, what do we see here? We see that in A, everything afterwards is our zeros and everything for B, the smaller value, everything afterwards is two, one, one. Wait a minute, two on one is exactly the same digits as this. So can we say that if we hit a one, zero, we put zeros in all of the values of the larger value and then put the rest of the digits in the smaller value? So that's what we're gonna say here. See, I'm not actually gonna put, so I'm gonna change this if C one split one, zero, zero. So I'm gonna change this, okay? We're gonna change this and say, sorry guys, if you already wrote this down, every time you see patterns, it could change. All right, so if you see a one in your digit here, put one, zero, put a digit one in the larger value, digit one in larger and rest zeros, then put rest of digits from input into the other string. Okay, so that's basically how you would do this problem. We are now going to go to the computer and then code this up. We're not gonna use my code. My code was just really long. So I'm actually gonna follow the editorials code because their code is really short and it's really efficient. So yeah, let's use the editorials code, okay? So right here, what I did was I created the input statement to T, which is the number of test cases. And then I had a string S and then a number N, okay? And this N is the length of the string. So I read in N and S and I did wild T minus minus and they're read at each test case, which is N and S from the input statement right here, okay? From these, okay? So what am I gonna do first? I'm going to loop from zero to the end, okay? So I'm gonna loop from zero to the end, okay? And then what am I gonna do? If my string at I is equal to, let's look at our C, let's look at the test case one, okay? If we see a one, then we have to put one in the larger and then the rest of the zeros and then put the rest of the digits from the input into the other string. So if we see a one, what am I gonna do? Okay, first of all, what am I gonna do? What am I going to do? Hmm, let me think, let me think, let me think. Okay, well, I need a large value, right? I need a large string and a small string. So I'm gonna to create string large and then string small. And I'm gonna fill these up with values from the our input statement. So large is gonna have, large is gonna have two, let's just fill it up with like a, okay? I'm gonna fill large with a bunch of a's and then small with a bunch of a's also. Okay, this is just like, what's the problem here? Missing close parentheses, what? Something's not right. Wait, what is wrong with something? Smaller, larger, yeah, okay. And then what am I gonna do? Actually, I'm not gonna put a's, I'm gonna put zeroes. This is just like the beginning of, so I'm gonna create two strings and I'm gonna put zeroes. The reason why, because like zeroes is zero and then we don't have to deal with anything with zeroes because you have to, there's zeroes anyway in the string. So this is good for as a placeholder, all right? You can put anything. All right, so if we put, if we see a one, we're gonna put digit one in the larger number. So larger i is going to equal to one, okay? And then the rest are gonna be zeroes. So I'm gonna loop from, what is my current index? My current index is i. So I'm gonna loop from i plus one to the end and then set the rest as zeroes. Then I also have to set the rest of the digits from the input into the other string. So I'm gonna do a four int, let's say, j is equal to i plus one, j is less than n, j plus plus. So I'm gonna loop from here, this is the rest. I'm gonna put zeroes in the rest of the larger. So larger's at i is gonna be zero. Actually technically we don't have to put zeroes in larger because it's already zero, so we don't have to put that. So I'm not gonna put larger that because we already initialize larger to have all zeroes. So let's just leave that as it is. And then we just have to put the rest of the digits from the input into the other string. So the other string is smaller, so I'm gonna do j is going to equal to s at i. So smaller at j is the smaller string of the smaller string, right? And I'm gonna set each digit to be the same digit as our input string at i, okay? Or, no, not i, it's not i, it's j. Whoops, sorry, at j, okay? Smaller's gonna have the same values as the input string, okay? Okay, now we have to think about another thing. Oh yeah, then we should break because then we don't need to do this again. Yeah, we don't have to do this again after we see a one. We could just set it and then just be done with it, okay? Let's see. Hold up. Otherwise, let's do this. We have to set, let's look at the editorial code. Okay, so else, what they did in their code was do larger at i. They're gonna set all of them to zero, right? So what they did in the editorial is what, so if we see a two, so okay, so remember there's three cases, right? If we see a two, we split it to have one one. If we see a zero, we split it to have zero zero. And then if you see a one, we did what we did, right? We already did the case of if we see a one. So what the editorial did for the in case of a zero is that instead of checking to have two more if statements of checking, if we see a two, we split to one one. We see a zero, we split to zero zero. What they're gonna do is they're gonna set both of these values to have the value zero plus input string at i minus zero. So they're gonna convert the input string or input string into its corresponding number form. And they're gonna divide by two. So what does this do? I forgot a parentheses. Okay, so how does this accomplish what we do? So instead of doing two if statements, let's look at it. If we see a two, right, we're gonna split into one one. So what they're gonna do is they're going to, so how assignment works is actually left to right. So we evaluate the right side first and then we assign left to the right value. So let's evaluate the right side first. If we see a, so let's say we see a two, right? And our answer is supposed to be split it to one one. So what happens is if we see a two, we're gonna convert two into its corresponding number, which is two, right? If you subtract zero, the character zero, you convert the character two to become the actual value two and then we're gonna divide by two. So that will be the have a value one and then we're gonna add one with our character zero and then that'll set larger and smaller to have one, one one, one and one, right? Because we set smaller to have value one and then larger is gonna equal to smaller, which is the same value one. Now let's see what happens if we put zero, if we see a zero. Well, if we put, if we see a zero, then we're gonna convert the corresponding zero into its corresponding number character, right? Ascii value and we convert the character zero to its corresponding number value and zero divided by two is zero. And then so zero plus the character zero is still gonna be zero, right? So then smaller is still gonna equal zero and larger is gonna equal zero. So this is actually a cool trick that you could use to prevent using two more if statements in this case. They use a trick of computing it numerically so you don't have to put two more if statements. But yeah, this basically accomplishes it because if you see a two, it becomes one one and if you see a zero, it becomes zero and zero. Smaller and larger and smaller become both add zero, zero. So yeah, that's how this code works. Then after that, what we're gonna do is we are going to print larger and smaller. And L, okay. Oh, whoops. My fault guys, I'm keeping making mistakes. It's actually like this. That's actually, this is how you're supposed to initialize. Whoops, whoopsie daisies. Yeah, it wasn't the parentheses issue. That was just how you're supposed to initialize. You're supposed to put size and then the character you're initializing for a string. So I'm supposed to put size n and then all the characters that I'm initializing. It's not the other way around. Okay, yeah, now I got accepted. Okay, yeah, so that's how you do this problem. I hope I explained it correct with you guys. Now I have to edit this video. Rate, comment, subscribe, I'll check you guys later. Peace.