 What's up guys my name is Michael and welcome to my YouTube channel today We are going to go over this problem called triple fat ladies The reason why we're going over spod now is because from the poll it said you guys want to go over spod questions again So I guess we're not using bitwise anymore. We're gonna get start doing spod questions. Okay, so Basically Let's just ignore the story. Basically, you're given a number K and you want to find the Kth number whose cube ends in 888 so basically we're just gonna keep cubing numbers, right? We're gonna keep cubing numbers and then The the ones that end in 888 we're gonna count those as as a number, right? And then now we just want to find the kth number whose cube ends in 888 so like if you had As you can see in the test case 192 right 192 If I were to cube 192, so if I take a 192 times 192 times 192, let's use a calculator Okay, so if I do like 192 times 192 Right and then times 192 again. That's 192 cubed, right? You have 7,077,888 and you could see the last three digits here are repeated, right? 888 so basically we want to find the kth number whose last cube ends in 888, okay? So how would you do this? well the To solve this problem. It's let's actually brute force it first. So how would you solve this brute forcing? So what you would what I would do if I were going to brute force this is I'm actually going to loop through from a Bunch of numbers from one to I don't know 10,000 or something right like one to a thousand or something something like right one to a thousand Maybe more actually maybe more so what I'm going to do to brute force this and that I'm going to loop through from one to a Thousand I actually have not coded this up yet. So Yeah, okay, so what I would do is I would brute force this so I would start with one I go up to I Don't know maybe a million a hundred thousand. Yeah, a hundred thousand Okay, and then I'm going to cube this number this number I so I'm going to take I times I times I right Yeah, and I'll just call this Check right Okay, check. Okay. Remember I put LL that just means long long, but uh, yeah You could let's really write long long like this in case in case to make it more readable for you guys Okay, so what I'm going to do is I'm just going to cube a bunch of numbers check from one to like a hundred thousand Right, and then I'm going to cube I so I'm going to cube this number So I'm going to go through a bunch of numbers from one to a hundred thousand. I'm going to cube it Now what am I going to do? I'm going to check if it ends in In Three eight eight. So if it ends in eight eight, right? I'm going to print it out So this is I'm just going to print out what I is okay and the reason why I'm doing this is because if I'm able to find out the The numbers that actually end that are cubed that actually end up in a eight. I could find a pattern so remember so then I Could basically find a pattern for all the numbers that actually end up in eight eight Then I could write a mathematical equation that allows me to give in whichever K K number it would spit out the cube that ends in eight eight. So I hope you guys understand what I'm doing here, right? So being able to do this is basically you trying to brute-forcing it first initially to find the pattern and then you could Try to find optimal solution given that way. So that's that's what we're doing. Okay, so yeah So here I'm going to write a function called ends in and This takes in a number. I don't know Check, I don't know check right and what we're going to do is we are going to basically take the last three digits of This number so we're going to actually loop through for three times Right and then each time we're going to divide it by 10 Right so check we're going to divide it by 10 to get rid of the last digit And we'll also need to get the get the last digit, right? So so this is where them we're going to get the last digit. So to do that we mod Check by 10 to get the last digit and now we need to check if it's equal to Eight so if last digit is equal to eight then we're going to continue doing it until we get to the end, right? So if it's not equal to eight then we know that it the last three digits are not eight eight, right? So I'm going to return false Okay, then in the end I'm going to return true So basically what this is doing is it's going to go loop through three times Which gets we're just going to keep getting rid of the last digit every time and then if the last digit is Not equal to eight then I know Last three digits are not equal to eight, right? Otherwise, it's just going to keep doing this three times and then in the end if they're all equal to eight I just returned true. Okay, so that's what the ends in does Okay, and now I'm just going to print what the these numbers are and see what numbers I get and then I could write a mathematical equation to get the Kth Kth thing. Okay. Okay. So now what's well that was pretty fast. Okay So now what's what you see this? Um, you could see one nine two four four two six nine two nine four two One one nine two two one seven nine four. Yeah, so on so forth So these are the numbers. I'm going to get the pencil to write down these numbers Okay, so now I'm going to get sketch a book. Okay, so Now now that I have one nine two so so I'm one I two four four two six nine two So on and so forth right and nine four two. Um, so let's actually try to find a pattern. Okay, so if I take Let's see if I just subtract the numbers from each other. What would I get? So let's actually try What happens if you take? Nine four two minus six nine for two. Okay, so I'm actually going to rewrite this again vertically because It's much easier Okay, so these are the numbers the first few numbers and then I'm going to try to find a mathematical equation That could get me the kth number the kth one that ends in 888 so um now let's just actually just get Subtract these okay, so let's subtract it. So let's say I have Four four two Minus one nine two Okay, so that gives me 250 so here is 250 difference here is 250 right so it adds 250 and Let's check. Let's check another difference Six nine two minus four four two. Oh That's also do 50. Okay, so that's also 250. So we're gonna do that and then if the third one's third one's also 250 then a Nine four two minus Six nine two Okay, so it's also 250. Okay. Okay. Um, so now let's actually see What happens if I take? Nine four two and add by 250 Okay, so that's also do 50. So as you can see this is actually arithmetic progression where you just add 250 every single time so the equation to get the Given the number k right to find the kth value that does this So what you would do is you would take the initial number that we have which is one nine two Right, we're gonna add it and we're just going to take a K minus one and then multiply by 250 Okay, and then that's that's basically a mathematical equation for the arithmetic sequence and you could actually test this out because if you put k equals a one you're just gonna get zero times 250 Which is gonna get you one nine two right. So this is k equals a one It gets you one nine two right if the k equals a two This is gonna get this this equation should get you four four two. So I plug in k equal to two I'm gonna get one nine two plus two fifty which is gets you four four two and if I say k equals a three Then um, you would get two fifty times two plus one and two which is going to give you that So if you want to do this problem Now that we found the pattern to do this problem We don't actually need to brute force this anymore. So sometimes it's actually better to brute force and then just find the pattern For some for these ad hoc problems. So now the since we already found the pattern all we have to do is just Yeah, read in the test cases and then just print out the The solution so here I'm going to read in the test cases So while subtract the values, okay, I'm gonna read in my each try has a k right so read in my k value and Then after that I just spit out the equation. So I'm gonna print out one nine two plus k minus one times two fifty Whoops Okay, and this should work. So I have not actually submitted this yet. So Let's try this Okay, okay, well, what's the compilation here? What's compilation here? I'm gonna compilation here. Oh, let's actually see plus was 14 Uh Did it did it did it did it did it did it did it did it did it did it did it did it did it did it did it did Did it did it did it did it do And we got got aced so yeah, that's basically how you do this problem you You basically just want to find the kth number whose cube ends in a 8 so just Loop through brute force a bunch of cubes that end in a 8 And then print those out and then find a pattern So then uh, if you you could find it Create an equation we're given the kth number you would find the cube that actually ends in a 8 So yeah, that's pretty much how you do this problem. Rate, com, subscribe. I'll check you guys later. Peace