 What's up YouTube? Mike the Coda here. Today, we're going to go over one of the recent problems on the last week's leak code contest. Largest number after digit swaps by parity. I don't know why so many people downloaded this. It's like so many people got like, uh, there's like 119 downvotes, and there's only 96 upvotes. So this is pretty, I don't know why this is downloaded. But anyway, let's go to the problem. So you're given a positive integer num, and you want to swap, you may swap two digits of num that have the same parity. So what that means is that they're either both odd or both even. Okay, so that means that you cannot swap like an even or odd, and you cannot swap an odd or even. So you may only swap any two digits of num that have the same parity. And then our job is to return the largest possible value of num after any number of swaps. So if we were to go to draw this out here, this, this problem isn't really that difficult. But as you can see here, let's say we have one, two, three, four, right. And we want to find the largest possible value of num after swapping two digits of the same parity. So, so what is, so if we start from here, right, we could swap two digits have the same parity, and we want the largest possible value of num after any number swaps. So if I want to find the largest possible value of any swaps, what should I do? So like, let's say I start from this digit one, and I'm allowed to swap values that are have the same parity. So what I have to do is what I should find the largest digit, that is the same parity. So since number one is odd, I just have to find the largest odd number. That is, yeah, just have find the largest odd number. That's what I have to do, right, and then swap it with one. So in this case, what is the largest odd number that I see here, that's after it? It's three, right? So then if I just swap one with three, so I swap these two, I would get three, two, one, four. Okay. And then another thing is like, then after I'm done swapping this, I could swap more numbers. So it's already swapped one, that's fine. Now I'm on number two. Right. And we can swap any two numbers that have the same parity. So since we want the largest possible value, and it's the number, the digit two, here, two is even, right? We should find the largest even digit. That's larger than two, right? The largest even digits that's larger than two. So if we loop to the right, we could find that four is the largest even digit that's greater than two. So it could swap with two and four, right? So then this would give us three, four, one and two. And then when we reach one, there's no odd values afterwards that are larger than it. So we could just leave out there. And when we reach two, there's no odd, no even values after that are larger than it. So then our answer would just be three, four, one, two. Okay, so that's their output here. And yeah, that's essentially the gist of the algorithm. So I'll just show you guys a code now. So here's what I did. I first converted it into a string because I didn't want to deal with like modding numbers and stuff like that. It's just a pain in the ass. So here, then I had the number swapped because they're asking, what are they asking? Oh, wait, I never even use swaps. That's kind of crazy. I could just take that out. But anyway, um, yeah, so I loop through from zero to the end of the string. So that's just essentially looping through here to the end, right? And then I'm what I'm going to do is I'm going to get the digit. So currently, I just get this digit. I have a variable called largest number and I just get the current digit. And minus, minus quotation zeros is a way to convert the current string into its equivalent digit, right? Okay, and then I set my largest index to be I. Okay, then what I'm going to do is I'm going to loop from J is going to equal to I plus one. So what this is doing is that so if I go back all the way back down here, what it's doing is here I'm looping through here to here, right? I is going to loop from here to here. And then J is going to loop from here to here. And I just want to find the largest number, not largest digit, have the same parity as one, and then I could swap it. So that's what I'm doing here. And then here, I'm just getting the digit at J. So it would go through like 234, try to find whichever digit it is. Okay, if the current digit is greater than or equal to the largest number, right? And they have the same parity. So digit mod by two is equal to largest number mod by two. So this statement, digit mod by two means that we're just making sure that both of the two digits are the same parity. So they're either both even or they're both odd. And a way to do that is just by modding by two. And that gives tells us if it's even or odd. Okay, so if digit mod by two is equal to largest number mod by two, so that means both the digits are the same. I just set the largest digit, the largest number equal to that corresponding digit. And then I just get the also set my large index equal to J. So now after this loop is done, I just swap the two digits. So I swapped x at largest index index with x of i. So that's going to swap the two, the two numbers here one and three, they're just going to get swapped here, right? Because once I get the index of where the largest number is, largest digit to have the same parity, just swap them. And then we're good to go. And we go on to the next one and next one. And so we're done. Then at the end, at the end, I convert this number into an actual number, because they want us to put out the actual number, right, they want us to give us an actual number. So the string s tall s toy converts the equivalent C string into a to a corresponding number. And then that's the largest integer that you could return. So yeah, that this problem got a seed. And yeah, that's pretty much just as a code. I hope you guys enjoyed this video. Rate, comment, subscribe. I'll check you guys later.