 What's up guys, my name is Michael and welcome to my YouTube channel. Today we are going to go over at code beginner 174 contest beginner contest 174 a b c d and e. We're not going to go over f because I still don't understand the problem, the last problem f, but we're going to go over a b c d and e. Okay, so the first problem is that you will turn on an air condition if and only if the temperature of the room is 30 degrees or above. So the problem that would trick people up here is or above. Remember that that means 30 degrees greater than or equal to 30. So all you have to do is in this case, just print yes, if it's greater than or equal to 30 and print no otherwise. So here I'll just show you my code right now. What took me up beginning was the greater than or equal to so when you read problems, make sure that you actually understand the problem. So what I did here is just I read just read in the value that I'm checking it T I put T it could be x you could name it whatever, but whatever value but yeah, all you have to do is check if it's greater than or equal to 30 and then print yes, otherwise print no. So what also could trip people up is this the yes you might you sometimes people print like complete uppercase, sometimes people print it all lowercase, you have to make sure that your input and output matches. So that's that's the only issue they there. Okay, now let's do problem B. So you have end points on a given plane and the coordinates of the ith point are Xi and Yi. And now you need to look for points from the origin. Look for the points such that the distance from the origin is at most D and how many points are there. So all you have to do is just for this in this case is that you have to loop through all the points, calculate the distance, and then check for everyone that is at most D just add one to a counter, right. So to do this, they want the distance at most D from the origin. So what trips people up here is the at most at most means is that the maximum possibly could have is D. So what does that mean that means our distance is we're checking if the distance from the origin is less than or equal to D. Okay, we're not checking if it's greater than or equal to D, right, we're checking less than or equal to D, because the distance is at most D. Okay, and how do you calculate the distance to calculate the distance all you have to do is just use distance formula. So the distance formula is actually this. So we have Xi Yi and the distance of the origin is zero zero. So distance formula is just x two minus x one square plus y two minus y one square, and then square root it. And that's, that's all that you have to do for a distance formula. And that's, and we're here checking less than or equal to D. Now what trips people up is that sometimes when you do square root in your code, that might actually complicate things. Because you're comparing doubles and floats. So what you could do to make it easier is actually you could square both sides, which is what I did. So I squared both sides. And I also simplified this equation. So I did Xi square plus Yi square is less than or equal to D square. All right, this is a D, by the way, D. D is the one that we're checking. So basically, you just have to go through all the points, calculate the the distance from to the origin and then check if it's less than or equal to D square. Yeah, that's all that's all we have to do. So here, I'll show you the code what I did. And let me just show here. Over here, distance D. Oh, whoops, I did. See, I just got a new pen right now. So that allows me to write stuff now. So it's pretty cool. So yeah. So all you have to do is just read in the number values and is a number of points. No, actually, yeah, n is number points. D is the distance that you're trying to check at most D. So what I did was I had a counter answer and I set it to zero. Then I loop through all the number of points, which is what this while loop while the number points is greater than zero, I'm going to subtract it. I'm going to read in every x and y value for each point. Then I'm just going to check if x times x from our equation here x times x plus y times y is less than or equal to D square, which if you put out in this case, it would be x times x plus y times y less than or equal to D times D. And then if it is, then we just add one to the counter answer. And then after the all the points are done, we just print out our counter. So that's how you do problem B. Alright, see, this is a little tricky. So I'm going to try to go over actually, it wasn't that it wasn't that tricky, actually. So here, this Takashi loves the value, the number seven and multiples of K. So K is just a value that you're inputting. Where where is the first occurrence of K in the sequence of seven, seven, 77, 77, seven. See output below. If there are no multiples of K print negative one instead. Okay, so the basically all you have to do for in this case is look at this sequence seven 77777. So how do we get from 77 to 77. And then how do we get from 77 to 77 seven? That's actually pretty easy. If you just look at this from seven to 77, and then 7777, all you have to do to get 77 from seven is just take seven, then multiply it by 10, then add seven, right? That'll get you 77. So this is actually what 77 is. And then 7777 is just the same as 77, multiply by 10. And then adding seven. Okay. So once you know this, you couldn't find the next value in each next value for the next, the next value for the sequence, right? You continue going on the sequence. So what you could do to solve this problem is just keep going from 77777 and just keep looping in this sequence for a variable for the sequence to keep going. And then you're going to check the first occurrence, the multiples of K. So if it's, if it is divisible by K, because it's a multiple of K in the sequence, then you just break out of the loop. And then, yeah, you just print it. Otherwise you print negative one. So how are you going to know when they'll stop the loop? So what you could do is some, there's pretty basic, you would actually just go brute force all the constraints. So you just loop from one to 10 to the six. And then you keep track of variable of an answer or something of the next value in the sequence. And you just check if it's divisible by K. So to check if it's divisible by K, oh, my bad. You just take just check whatever variable it is, let's say var, let's say I name my variables of this variable var. So I've been you just do mod by K, which this takes the mod by K means I get the remainder. And I just check if it's equal to zero. So if this case is true, then yeah, I just break and then I print out the number. So I'll just show you the code right now. So yeah, this was this trip me up a bit also. But I understand it. So yeah, what what I did was I read in the number K, right, which is the one that we have to check if it's a visible by. So I have a counter starting from zero, which is my current number. I'm going to brute force from one to 10 to the six, because that's the maximum constraint we have. So this is a hacky way to do it. So yeah, and then what I'm going to do is I'm going to take the current value that I have zero, and beginning at zero, but then later on, I'm going to multiply by 10, and then add seven. So I'm going to do that every single time. And then I'm going to check if it's current mod by K, each current value, mod by K is actually going to equal to zero or not, right? So that checks if it's divisible by my current very variable that I'm multiplied by seven, both by 10, adding seven is zero or not. And if it is, I'm just going to print out I, I is the counter that we're starting from one to 10 to the six. And the reason why we go to 10 to six is that we need to find 10 to the six is the maximum possible one. So we could brute force it because we could go through all the constraints. And then yeah, and it would print out the first occurrence of the value K that is in the sequence. Okay. Because of first occurrence would actually just be that. And if you go through all values of K from one to 10 to the six, and it's not possible, then yeah, you just print out negative one. So like this in this case, like two. So yeah, let's go back to the code again. I'll just do this. Yeah, right here. So yeah, multiply by 10, add seven, mod it by K, right? Get the remainder mod of K. If it's equal to zero, I just print out the current number that I'm looping from one to 10 to the six. So that'll be that number. And then yeah, then I return zero. Otherwise, if I go through all the numbers from one 10 to six, like brute force those and it doesn't work, then I just print out negative one. Yeah, that's the code. Alright, I think it gets pretty hard. It gets harder and harder here. Okay, D. So you have a shrine of n stones arranged from left to right, the color of the ice stone is given to you are which is stands for R. And it stands for which stands for red. And then you have W which stands for white. So you could do the two two operations. You could choose two stones, not necessarily adjacent. So this is a this is a clue. It's not necessarily adjacent. You swap them or you could choose one stone in the changes color. So you could change from right, red to white, white to not. Okay, so in this case, let's say you have I want to give an example. So yeah, what the fortune teller wants is that a white stone cannot be placed to the left of a red stone, because I'll bring a disaster. And you want to see how many operations it would take to reach this scenario without a white stone. So to do this problem, it's actually not that difficult. So just just don't overthink it. So let's say I have like a white stone and then a red stone and a white stone and then a redstone, like in this input statement here, white, red, white, white, white, red, white, white, here, white, red, white. Oh, yeah, damn, I got a racist. Sorry, guys. Edit undo. Yeah, white, red, white, white, red, white, red, white, red, red, white, red, red. So the the ones that I cannot have red is you can't have a red, red, redstone next to what you can't have a white stone on the left of a redstone. So the ones that you can't have are this, you can't have this because this white is an extra red, you can't have this because this white is next to the red, and you can't have this because white is next to a red. Okay, so one way to solve this actually just to swap it a white stone with a redstone, it could be any of them from the back. So if you think about it, if I'm going to swap, keep track of a current like counter and swap it, I just need to do a certain number of swaps of the red stones for the swapping the whites for the reds. And technically, I don't have to swap anything. Right? So here's what you could do. What you could do is actually just count the number of red stones that I'm going to swap with the white. Okay, because the problem just states us asks us to count, right? So I don't I don't technically have to change the original string. Right? They want us to count how many operations are needed to reach the situation without a white stone, without such a white stone. So they want us to count the number of operations it takes. So all you have to do is actually just count. So what you could do is actually just count the number of red stones. So in this case, I have one, two, three, four, four red stones, right? This is four reds. And then what you could do is because we are swapping the number of white stones, in this case, next to a with a swapping the number of white stones that have that are the left of a redstone, right? That's what we're doing. We're swapping a white stone that next to redstone with another redstone, right? All we have to do is actually just to since we count, we counted all of the number of reds already that are in total in this sequence, which is four, we just have to loop from the beginning up to four. So like from here, up to 1234, three, 1234. Yeah, up to here. And then check count how many whites there are, because that's the number of ones that we have to swap, right? So with with the number of reds. So if we count from the beginning of how many ones that we have to swap that are white, from the beginning of the beginning, count up to up to four, right? So four places up here, from from zero up to four, and count how many whites that we have to swap. That will actually be our answer, right? If we count from because the total number of reds we have is four, and then we're swapping whites. In the beginning, that in the beginning where they have whites and reds, the whites are next to a red, we're swapping those, right? So then all we have to do is just check how many whites there are from the beginning to the number of reds in the beginning, right? Because we're going to swap all the values of white with a red value from the end. So like, basically, we're trying to get something where it's like white, white, white, and red, red, red, you know, all the whites in the beginning, all the reds in the end, right? That's what we're trying to do. Or we could we're trying to do is like, get all the reds in the beginning, and all the whites in the end. So the number of swaps it would take is actually just the number of reds and looping it from the beginning, because we're going to try to count how many whites that we are that we're trying to swap. So all you have to do is count the number of reds here, which is we did we count as four, and then loop from the beginning up to four, number of reds and then check how many whites there are, because that will be the number of swaps you have to do. Okay, so in this case, it would be like 123. So the code here is down here, I'll show you the code. Right here. Okay, so this is the code. So here, what we're going to do is we read an end and read in the string. So we're going to count the number of reds, and then we add one for how many total reds in the sequence, then we're going to loop from the beginning up to the number of reds, because that is good, the from the beginning, the number of red styles, that's the number of reds that we're going to do, assuming that all the reds are in the beginning and all the whites are in the end, right? So assuming this is the, this is the what we're trying to do. And then we just count how many whites there are, because that's the number of times we have to swap from the beginning. So with the end, so that would be string check from string. After looping, after we're adding all the reds, we're going to loop from i zero up to reds, i plus plus, and then we're going to check if string at i is equal to w and then add one for ops, and then after that, we just print out ops. Okay, another one. All right, this is a probably very hard. This was this did pretty difficult to understand. But first, I'm going to show you something. Oh, wait, yeah, the altar altar. Okay, so he is pretty difficult to understand. Okay, so you have n logs of length a i a two up to a n. You're trying to cut the logs at most k times in total, who's when a log L's cut at a point whose distance from the end is it becomes two two lengths, t and then l minus t, right? So if I cut a log from the beginning, it's t, if I it becomes two lengths, t and l minus t. So now what we're trying to do is we aren't want to find the shortest possible length of the longest log after cake at most cake cuts. Okay, so this is a bit difficult. Basically, this problem is trying to say is that we are we're going to cut the we have a bunch of logs, right? We're going to cut the logs each a number of times, right? Whenever I cut the log, I want to find the shortest length after I cut the longest log. Is you right? I'm going to I want the shortest possible length after after I cut the longest log. And I only could do it at most k cuts. So if I cut like once, but cut it, let's say cut a log, that'll be like one cut and then like, okay, my bad. Let me explain it again. So if I cut a log, basically, I want to cut, go through the each logs, try to cut it and then make find the shortest possible like that could cut of the longest log. So based on all the logs, right, in this length of a my bad, in the lengths of a one a two up to a n, as a bunch of logs, right, we're going to cut the longest log, right? And we want a shortest possible length after cutting the longest log. And then we're going to do that for at most k cuts. So at most k cuts, we're going to cut that number of times and print out the shortest possible length. Okay, so what's difficult about this problem is that first of all, we don't know. We don't know the length that we're going to cut. That's the hard part, right? Trying to find the shortest length possible to cut the log. So what we're going to do is we are actually going to guess. So we're what we're going to do is we're going to guess by using binary search. So we're going to start from one going up to the the maximum length of the length of this log, which is a it could be anything like a max. So out of all these logs, a one a two to a n, we're going to find the maximum log, right? Then we're going to do binary search from one to that. And we're basically we are guessing a point, the length that we're going to cut. So we're going to binary search, and then guess that length. And then what we're doing is we're going to use that length and cut all the logs for all the values using that length. And if it doesn't work, right, we're going to move our range, either up or down. So like, let's say I have, I need to re redo this clear this. Okay, so let's say I have like a log. So I'm going to explain how to cutting logs. So bear with me. So let's say I have a one, a two, up to a n. So these are the lengths of log, right? These are the lengths of logs. And we want to find the shortest length to cut, right? So what I'm going to do is I'm going to find, like, I don't know what, what length to cut, right? So what I'm going to do is I'm going to find the maximum log, maximum length, right out of all these values. So this is going to be a max length of the log. I'm going to do it by looping through and getting the max length. So let's say let's call it a max, right? So the maximum value we could cut for each log is going to be between. Whoops, I didn't mean that. No, yeah, it's going to be between one and a max, right? Because a max is the longest length for all the logs. So whatever length that we're cutting is going to be between between one and a max. So what we're going to do is that we're going to do binary search to guess whatever length we're going to cut and do that for all the logs and see if it works. Okay, so yeah, so binary search basically what we're doing is using this range from one to a max. So like let's say I have one here, and a max is my last one here. I'm gonna get the basically what binary searches I'm going to get the middle value between this one and a max. So let's say let's call this a mid and check does this work. If this works, then since we want the smallest, smallest log, I think, we are going to move our range either up or down. So I actually don't remember what the range or what we did, but I'm going to look at the code again just to make sure that I'm not completely wrong on this, right? Yeah, so we move the if it works. So if it works, we move the right range to the middle. And if it doesn't work, move the left to the middle, right? So and then after that, we just print out right. So because we want the shortest length that we're cutting. So let's say this. So let's say in the beginning, my range is from here to here, a max them trying to guess, right? And now I just guess the midpoint, right? I'm going to use binary search get the midpoint. So I do this one. Let's say this does work, right? If this does work, and we want the smallest length, right? What I'm going to do is I'm going to take my range that I'm guessing from one to the end, because we want the smallest length, right? I'll move this, I'll move the, the maximum range, which is like a max, I move the right down to the middle. So now my a max the right range is going to be between here and here. Right? It'll be like that, because that's the range that I'm going to do. I want the smallest I want to get the smallest one, right? So if this middle bone works, I'm going to move my right range originally, it was the a max I'm like that I'm guessing. That's right. Move that down here to a mid. And I'm going to do it again. I'm going to do it again. And let's say, let's say the middle I'll get the middle point from here again, from a and a mid guess it doesn't work. Let's say let's say it's a mid to a mid to Sorry about the handwriting, but does this work? If it works, I'm going to move the right range down again. And if it if it doesn't work, I'm going to move my left range up. So I'll move my left range up. Yeah, so if we keep guessing the ranges and moving it the right, if it does work, I move the right down and move. Well, if it doesn't work, I move my left range up. Basically, that in the end, that's going to give us a, a length that is the smallest length that we're cutting. Okay, I hope you guys understand what I'm doing. So I'm basically doing binary search, cutting, guessing each length and see if it works or not. Okay, so now, now once you guys understand the range that we're guessing, here's the difficult, difficult part. Difficult, difficult part is that we need to cut at least K, at most K, at most K cuts. So at most K cuts. So let's say we're cutting a value. So the difficult part about this is that we have to check, is this length that we're cutting that we guess, right? possible? Is it right or not? So to do that, we need to think, let's break this problem up with one log first, right? So let's assume that I'm going to have one log. What did I just do? What did I do? Okay. So let's assume I'm only cutting one log with a one value. So if I'm cutting, let's say I have a log here, I'm going to cut this log, right? So I guess to the distance that I'm going to cut. So this is assuming one log, right? This is just one log. We're not checking all the logs yet. But let's say I'm going to cut a distance. This is the distance I'm cutting D, right? I guess this isn't I'm going to cut this log. So how many cuts can I do with a log one, the length of this length log of D of D? How many how many cuts can I do? It's simple. I just take the length of the log and divide by D. And the reason why is that that's going to give me the number of times I could cut the log. So I cut the log once here and I'm going to cut it log another time. Let's say I cut another log here. There will be another D log that I cut and then another one, the D log that I'm going to cut. Right? So then if I let's say I'm take L the length of my log and divide by D, they're going to give me the number of times I could cut using the length of D that I just guessed, right? And in this case, let's assume it was like three, right? So I took a log L one, I cut it D times and the number of cuts I could do was one, two, and three. Okay. So the tricky part about this is that let's say you have like leftovers. So if I have not like a leftover here, I want I don't want to include this leftover, right? So the actual maximum value could do here is actually going to be length of the log divided by D. And that'll be the number of cuts. And it will be the ceiling of it, and then subtract by one, because that that's going to get rid of this offset of the, the not include this piece that I don't care about. Okay. So yeah, that's what this was going to be the number of cuts, number of cuts for one log. So now, now we need to check for all the logs. Okay. So we have to check logs from L one L two L three up to L N. So to check for all the logs, like L one L two L three L four up to L of N, right? Those are the number of the lengths of all the logs. So now, remember, we could only have cuts number of cuts that are at most k. So I think that's less than or equal to k. That's the number of cuts we could do. So what we're going to do is we're going to loop through from all here to loop through all the number of the lengths of the log and then cut, cut all of them by the length of D. So I'm going to go through all the logs, get the number of cuts for each of these, each of these logs. So I'm going to do like a loop from li over D for all of them, get the ceiling of it and minus one. So I'll be the number of cuts. And I'm going to add them. Right. Number of cuts. I'm going to go through all the lengths of the logs and I'm going to add the number of cuts for each of them. And then once I get the total number of cuts, I just check, is it the, is the total number of cuts less than or equal to k? And if it is, then I return true. That means that my check was right. If it's not, then I would return false. So that's, that's basically how you do this problem. We guess a length to cut for each of the logs. Using binary search. If the, if, if it does work, then we're going to move our range down, right? Because we want the shortest length to cut that works. So we're going to move the length, the right range down. If it doesn't work, we're going to move the left range up, right? So then we could find the right range, the right number to cut. So yeah, so we use binary search, guess a D from the smallest value to large value, guess a D, which is like the length we're going to cut, we'll find the number of cuts, check if it's less than or equal to k. If it does, we're going to move our right range down, and then do the do the same thing again, guess another D, if it doesn't remove the left range up, and then keep guessing until finally we get the shortest number value shortest distance we go cut that has a length less than or equal to k. So yeah, this is how you do the code. So here, this is the code. First of all, I created a vector for the number of numbers and k. And that's it. Here, I read in all the lengths of each log. That's what a of i is. So this for the reason the all lines of each log, and I get the maximum number of the log. So I could get the right range, the maximum range I could, I'm going to use our right here is I'm going to use binary search. So this is basically just guessing, guessing the right range or not, the right number of not so binary search, we take the middle value of what we're doing. So here I start a left starting from zero, the like, let's say zero is the lowest number we could cut right, can cut right is the maximum number we could cut. While left is less than right, I do a left plus one. But that's just this is just like moving the left up or down. Right. That's what this while loop is doing. We're going to get the middle value between our left and right ranges, get the middle, we're going to check if this works. If it works, we're going to move our right range down. If it doesn't work, we'll move our left range up. Okay, so that's basically what this code is doing. Left plus one is just moving the left range up by one, making sure that works or not. Yeah, that's what this is doing. Because we want the shortest number shortest, shortest distance or cutting to be make sure that works. So that's why we do this. In the end, we just print out right, because that's the left and right is going to keep guessing left and rising closer and closer together. Excuse me. In the end, they're going to get to a single point. And then that will be the last number. Okay, so now now how do we check the if the distance we're cutting is actually less than we could okay, the number of cuts is less than we could okay. All you have to do remember what I told you is actually just to loop through all the values and then add up each number of cuts. So this is the number of cuts for each value, where we have a this is the variable now is equal to zero, right that now is just the number of cuts we're doing. And all we're doing is just taking the current length and dividing it by x, which is the value that I'm guessing, right? Remember, we'll use a value that we're guessing. In the example, I said D, but here in the code, I just wrote x. But yeah, x is the number that we're guessing that we're passing in from binary search to the checking the numbers less than equal k, right? We're guessing in a value. That's what we're doing here. Yeah. So what we do is we take the value, the length of the log, the current value of the length of the log minus one and then divided by x. That's basically just doing this, right? We're taking the length of the log divided by x. In here, I wrote D in the code I wrote x. It's just the number we're guessing, right? The length we're guessing for the log. So yeah, that's what you do here. I did minus one before. So this is actually equivalent to L one minus one over D, right? This is equivalent to that. So that's what I did here. Yeah, that's the same thing. So yeah, this would get each cut the number of cuts for each log. And then we add up, we sum them up. And then if it's less than or equal to k, we just return that. So yeah, that's how you do this problem. So yeah, that's how you do all the problems A, B, C, D, E. I don't know how to do F yet. Yeah, I'm still stuck on trying to figure out how to do range queries. I'm going to give a tutorial on that when I figure it out. But yeah, that's basically this whole video. I hope you guys enjoyed this video. Very calm, subscribe. I couldn't do a live stream today. I'm so sorry about it. But yeah, this is the best I could do. Yeah, very calm, subscribe. I'll check you guys later. Peace