 What's up, guys? My name is Michael and welcome to my YouTube channel today. We're going to do a code forces problem called lucky numbers. And this is a problem that involves binary search and brute force. You could use bit masks, but let's not do that for now. I don't really understand how they're, how to use bit masks for this problem. But this is how you would do lucky numbers. So what is lucky numbers? Uh, they basically you're inputting a positive ninja and and the number doesn't contain, uh, leading zeros. Your job is to find the least super lucky number that is not less than N. So the least super lucky number has not less than N. A super lucky number is a decimal representation that only contains digits four and seven, right? Four and seven. So, for example, four, four, seven, seven, seven, four, four, four, seven, seven are super lucky. And four, seven, four, four, four, six, seven are not. And the four is so basically super lucky means that it contains the same amount of digits of four and seven. And yeah, so this is this four doesn't is not super lucky because there's no, the number of fours and the number of sevens are not the same right here. We have one four and there's zero sevens. And in here, seven, four, four, seven has one, seven and two fours. Right. So that's not lucky because the number of fours and sevens are not the same. So our job is to find the least super lucky number that does not exceed N. Okay. All right. So I'm going to give you guys positive video. Try this problem out yourself and then come back to it. And then I'll pretty much go over the solution with you guys. All right, guys. So I'm going to go over the problem with you guys on how to do this. So if we look at the problem statement, the first line contains a positive integer N. And that's the number that we're inputting, right? And then we need to find the least super lucky number that does not that is a is not less than N. Right. So to do this, if we look at the constraints, N is between one and 10 to the ninth. So we know that the maximum possible lucky number has to have nine digits. Right. That's a max. Okay. And the smallest number is has probably two digits actually, because the reason why is if I input like one, let's say 10, the, the, if you look at the cases, it would be probably 47. Because, uh, so, uh, yeah, 47 would be the first lucky number, right? Because it has the same number of fours and sevens. So if the smallest number of one is going to have the size of the digits, one is going to have two, right? Two digits, right? Two digits, uh, for the number of digits that there are. So for number one, the smallest would be two digits, right? 47. Okay. So if we know that the largest is 10 to the ninth, nine digits, we could recursively generate all the values of nine digits. So then once when we get past the 10th digit, we could just break, like we're just done. Okay. So we could recursively generate all the numbers that are lucky or not, and then input that into a list of, uh, into a vector of, I don't know, whatever size it is. It just keep adding, recursively generate the numbers and keep adding because the input size is really small, right? This is not that much. This is not that big. So if you recursively generate all those numbers, add them into a big vector, then sort the vector, then use binary search, you could find the one that is the number that does not exceed the letter N. Okay. So first I'm going to start out this code program by saying, uh, I'm going to include this, this is my first current program. Uh, this header file is what most people use in competitive programming. Uh, it's pretty much one simple include statement that includes all the, the header files, the libraries and stuff like that. Um, you can find it online. There's a, there's a ways that you could find it, uh, how to include it. Normally you don't do this because it's bad habits to just include everything, but this is what I do. Okay. For competitive programming sake, cause it's like one, one, one includes statement. Okay. Uh, include STDs. So then I don't have to write STD over and over again for the namespace. I'm going to do define LL as long, long. So define LL long, long means that I am, uh, including, uh, hashtag define LL long, long means that whenever I type LL, it's just going to mean long, long. And this is just a improved, like typing speed ability. But so every time when you guys see LL, it's just means long, long. Okay. So I created a global variable called N. So then I could access it by variable N that I'm inputting it from here. It's the only input line. And so I created that global variable and just before everything. So I could access it in all my functions. Okay. So first remember, we have to read in the only input, which is the letter N. So I read in, read an N using CN. So I see in the value of N. So then now I have the value. Okay. So what am I going to do? I'm going to generate a call as this function, uh, gen generate all possible values. Okay. Let's say generate all possible values or something. I don't know. I'm going to generate all the possible values and in this function, then I'm going in the function of gen, uh, generate, um, let's just call it gen, gen, gen possible values. Okay. Just for short, gen, pos values. I don't want to type it. Over again, okay. So gen possible values, it's going to generate all the possible values that have size of fours and sevens that are lucky numbers. It's going to add it into a vector. So I'm going to create a vector called, uh, vector, vector, long, long. I'm going to call it data. Uh, maybe I shouldn't call data. Uh, let's say our lucky numbers. Okay. So this is going to be generate all the possible lucky numbers here. I didn't write the function yet for gen possible values, but it's going to add up all those values into gen possible values. And then I'm going to sort this. So I'm going to sort our lucky numbers and then I'm going to use binary search on that. So, uh, I might actually do this later. So let's just generate all the possible values first and then we could actually use binary search on that. Okay. So I'm going to call this function gen possible values. Would I call it gen possible possible values? Let's just name it that. Yeah. Gen possible values. Okay. It's going to be a function. Um, we're going to do avoid it. So then it's not, there's no return type. And then I'm going to have three, three, uh, three arguments. Okay. The first one is going to be our current, uh, answer or not answer our current value or yeah, whatever value, which is the value that we're going to generate all the possible combinations of using recursion. Right. The second one, we're going to call it the number of sevens. And then the third one is going to be the number of fours. So remember we were generating all the possible values of lucky numbers, and those are occur when we have fours and sevens. Right. So what I'm going to do is I'm going to append four and seven to, to our value, current value that we're passing in every single time, and that will generate all the possible values of super lucky number. Okay. But remember it has to be super lucky. So that means that the number of digits of four and seven have to be the same. Right. So we have to only add the values that are super lucky into our vector. Uh, yeah, the only value out of the values are super lucky into our vector. So that means that when we're recursively generating, we have to make sure that our numbers of fours and sevens aren't the same. Okay. That's what it means. Each, uh, the number of digits fours and the number of digits sevens have to be the same. So, okay. So first, I have to think about the base case. Remember the base case is if our value is greater than, uh, one E to the 11 or 10, I think 10, yeah, one E to the 10th. And I'll put like double L's for, uh, it's double. So I think that would be double L's. Um, there's no operator. Okay. So I'll just add like 10 zeros. One, two, three, four, five, six, seven, eight, nine, 10. Okay. So if it's greater than the 10, 10 digits, right, that means we're done. Okay. Cause our maximum N number is 10 to the ninth, right? So if it's greater than 10, we're done. Okay. So that's what we're going to do. Okay. Um, then we're going to return. So that means we're done generating. We're completely done generating. Okay. Otherwise we're going to generate possible values. So I'm going to add 10, the value, I'm going to multiply 10 to the value and then add a four. So this means that I'm going to append that the digit four into our number, our current number. Okay. So when it means that basically what I'm doing is I'm generating all fours and the possible values of fours and sevens over and over again. And, uh, this function call is going to generate, uh, one four. So it's going to add a pen to four to the, to the last value of value. So, so if, if I have like number one, if the value is one first, I'm going to multiply by 10. So as it becomes 10, and then I'm going to add four. So now it's 14, right? So that this appends for the letter, the, not letter, the number four to the back of value. So that's what this does. Okay. So that's going to add a four to the, uh, back of the value. Okay. The number of sevens is still going to stay the same, but our number of fours is going to increase by one. Okay. So this is going to regenerate the ad for a pen for into the possible values of answer or value. Okay. Now I have to generate seven also. So I'm going to append seven also. So possible values, value multiplied by 10, LL, LL means long, long. Okay. Plus seven. Okay. And then I'm going to increase the number of sevens by one. And then the number of four stays the same. Okay. So this is going to add seven to our value of value and seven to the back of the value. Okay. Remember that we only add the values that are super lucky. So that means that the number of sevens, uh, the number of digits fours and sevens are the same, they're super lucky, right? So what that means is that if, if our number of fours is equal to the number of sevens, that means it's super lucky. I'm going to append it or push back to our lucky, our vector of lucky numbers. So pushback value. Okay. Uh, maybe I shouldn't have called the value because it's a current value. But yeah, that's going to be, that's going to generate all the possible lucky numbers. Okay. So now, now our vector array or vector array or whatever it is, uh, what do you call it vector? It's vector. So now our vector contains all the super lucky numbers. Um, then we have to use binary search and find the value that is just greater than that. Um, so here, uh, the arguments we're going to pass in for generated is going to be zero, zero and zero. Yeah. So that's going to start at zero and then at four, then seven, then four, seven, all the positive possible combinations up until one with 10 zeros. And that's it. Okay. So once we generate all the values that are super lucky into our, our lucky numbers, we are going to sort it. So I'm going to sort our lucky numbers dot again, sort our luck, uh, our lucky numbers and that's going to sort all the lucky numbers into that. So now once it's sorted, we could run binary search. So to run binary search, we're going to do, there's like binary search, binary search, uh, there's like a, I don't want to code binary search up. So there might be actually a function binary search, binary search in C plus plus. Just to make sure that like, um, like I don't want to code it up. So, okay, binary search. Yeah. Element is found in there. Okay. So, downside. Okay. So we're going to be binary underscore search. Uh, the problem is doesn't return the actual number. I have to use like, I think you have to use upper bound or something. Binary search return number. Do, do, do, do, do, let's see. Let's see. Let's see. Oh yeah. Lower bound. Okay. So return to point it to the fit. Yeah. Okay. So I think, should we use lower bound or upper bound? Let me think lower bound what's upper bound returns appointed to the position of the next higher number than number. Yeah. Yeah. Okay. So the next position of the next higher number than number. So the next higher number than number. Yeah. So we need upper bound. So we're going to use upper bound on our value and then subtract by again, and then we use that as our index. So here I'm going to do a upper bound. Let's copy this call position of position of the number is going to equal to this minus R1 not begin. Okay. And then we're going to see a position of number. So once I have the position of the number in the array, I'm going to get print out the actual number. So our lucky numbers at position. Okay. So that should work. Now for the our actual array, it's not R1. It's going to be our lucky numbers. That begin. That same thing. Yeah. Same thing as that. So this is going to get this is the number we're searching. So number we're searching is going to be in. Yeah. All right. So I think this is going to work. I haven't actually like, fully tested it, but let's try. 10 should be 47. So let's try that. If there's an issue like, I don't know. Yeah. That is 47. Okay. So that's good. So let's let's copy it and then submit it. See if we have issues. Well, past test one, test two, probably take a few times. If it gets to you, then I have to think of another solution. I don't think I'll get to you though. Hmm. Let's take it off for a while. Wrong answer. Let's see. What is the problem? What is the problem? Let's see. Do do do do do 47, 74. Ah, damn. Okay, so if our number is super lucky already, then we okay. Okay, if 47 is already super lucky, then we can't actually use 47. So we have to do upper bound. Upper bound doesn't include the actual number greater than or equal to maybe lower bound does lower bound include. Okay, lower bound. If container contains one occurrence returns point to the first occurrence. If you're going to have a mother returns pointed to the position of next higher number than number if the do do do do do do do lower. Let's say let's see lower bound. Let's try lower bound. See, I don't know if it because upper bound lower bound. Don't it actually include the, okay. Yeah. So lower bound works. Okay. So it's lower bound because it we're we're not including the value n. Right. We're we're including the value n. It's 47 could be 47. Yeah. So lower bound is a way of binary search. Remember lower bound upper bound are like types of binary searches. So we could use that. We'll see. Let's see if this works. Okay. Well past test 842 should be, you know, accepted, accepted. Yeah. So, okay. So I got accepted. So yeah, that's how you guys do lucky numbers. I hope you guys enjoyed this video. I'm going to do more problems of code forces because code force is more fun. And like, I like doing competitive programming. But yeah, great. Comment, subscribe. I don't have, I know I haven't been uploading a lot lately, but I'm going back into uploading probably daily now. And I'll do some more videos about like heaps and like prefix song. I want to do one on segment trees. I have to learn more about that soon. But yeah, just keep doing problems. Keep doing new algorithms. Keep implementing. And yeah, sooner or later, you'll do really well on these contests and interview problems. But yeah, ready to come subscribe. I'll check you guys later. Peace.