 What's up guys, Mike the coder here today. This is the first time I've started doing a face cam video I don't know I made a poll and says you guys want to do a face cam So this is the first time I'm gonna do a face cam today. We're gonna do a digit queries So essentially is that you're given an infinite string that consists of all positive integers in increasing order 1 2 3 4 5 6 7 8 9 and then 10 11 12 13 14 15 16 17 18 19 20 and 22 20 24 25 Okay, well, it's like I get my mouse in the in this you see one then there's two and there's three and there's four And there's five and there's six seven eight nine ten and it repeats. So it goes 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25, so it continues repeating. So our task is that basically we need to query digits at this form So what is the digit at the kth position in the string? So if I give you I don't know the first Physician it will return one, right? And if I give you the ninth position, it'll return nine, right? And if I give you the the tenth it should give one 11 it should give zero 12 should give one and so on and so forth Essentially is that's what we have to do and the input of the statement is that we basically read in the q which is the The number of queries and then we read in each integer k Which is starting index one of each of the string So that's basically the gist of the problem and now we have to figure out how to solve this problem So let's go right in so the most naive way you could do this is actually just to Repeatedly build your string. So we start one two three four five six seven eight nine ten And we just do 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 And we just keep going on and so forth 26 27 28 29 30 and we just keep going until we could get up to I don't know 10 to the 18th Which if you could do this But I don't think it will you'll probably overflow because There's only a certain amount of digits you could store in a string or an integer So if you just keep I mean you technically you could just keep doing this and so on and so forth and then Hopefully until you get 10 to the 18th and then just pick pick position k and then just read Read in whichever position at the k position But this takes a lot of time and it's not a good way to do this So how do you do this? What you do is you have to build a mathematical equation representing each of the corresponding digits With it's the pattern that you see so the first thing that you can notice is that if the current value of your Digit of your k is less than 10 Right if it's less than 10 You could just return the digit right because if you have like one two three four five six seven eight nine Right, any of the values here Are just going to be itself Right because like if the first position is the first position second position It'll just be the second position So on and so forth it only happens when we reach the 10th position Which case we have to start doing stuff afterwards So if your k is less than uh 10, we could just just return k That's all you have to do just print out k and return it and it only gets harder when we Go past this the first thing you need to know is that let's say that we're forgiven the our value of k We need to know what is our length of the number that we're on that we're repeating right if if I give you the k So let's say that I give you the thousandth position What is the length of the number that is actually repeating if it's if it's a thousand and then it's actually I don't know 300 that's repeating over and over three or one three or two three or three or four That actually matters the reason why is because if we look at this We look at one two three four five six seven eight nine right and then we look at 10 11 12 13 14 15 16 7 18 9 20 once we've reached 100 100 the positions that we are at if we just write a single math equation Dictating hey, what is the thousandth position right what happens is that once we reach longer length of our Current number that we're repeating on repeating two digit numbers of 10 So and so forth 11 12 13 15 16 and then three digit numbers 100 101 102 103 104 is that these because the length is changing of the number that we're incrementing and repeating That is going to affect what our current positions are that's going to affect the current positions and the math equation that That we need what we need to know is that hey if i'm giving you the current position k What is the number what is the length of the number that we're repeating on because if we know that then it's much easier to You know dictate these are the numbers that are going to repeat So on and so forth and then we could figure out our equation from there Because it's changing every time the length of the number that is repeating every time the length of the number is repeating Gages from 10 to 100 and now it's because it's three digits now You know length of three digits that we're repeating 101 102 101 102 103 104 and then once it gets like four digits like a thousand that changes again What we need to know is given our position k What is the length of the number that's repeating and then that will help us find our answer? So we actually can calculate this it's actually not that difficult So we know that if our position or our kth position is less than 10, we just return k, right? It's one two three five six again at night now now we reach two digit numbers So it's repeating 10 11 12 13 14 15 16 17 18 19 20 So what is the last value that are two digit numbers? It's simple 99, right? So The next two digit numbers go from 10 to 99 So now we just need to know what is the last position if we're repeating this many number of times Well, this is not actually difficult to do okay. We can actually calculate this So what is the last position of the number of 99? What do you do is you take 99 minus 10? We minus 10 And we plus one So that's going to give us 90 and this is going to be 90 numbers There are 90 numbers between 10 to 99 to 10 to 99 So there's 90 numbers and these are 90 numbers that are two two digits Okay, these are 90 numbers that are two digits. So these 90 numbers have two digits So we're going to actually take 90 and we're going to multiply it by two And it's going to give us 180. What is 180? This is going to represent similar to the last position that we are going to repeat for two digit numbers Okay, so if we're giving the k position Um, there, there's going to be The position 10 is going to start from one and then 99 is going to end at um position 180 Really would be 180 Now the problem is that this is assuming that this 10 That we started from actually started from index zero, but since we are starting from Nine, we actually have to take 180 and we plus the first nine values that we're starting from So it's going to be 180 plus nine So our range or for two digit numbers For the position would start from 10, right? So it would be this this position would be the 10th position, right? All right, this is going to be the 10th position. So there's a 10 Then 11 12 so so on and so forth and then it's going to go up to 180 Nine this is going to represent the the range of the positions that's going to be yet So we know it's like two digit numbers and so on and so forth. So if our k is like on a 170 So if there are k's at position 170, we know that there's two digit numbers here So let's say our k is equal to 170 now So I want you to find me the number at the position 170 So because we already calculate the ranges the two digit numbers We know that for the ranges of the two digit numbers of their positions We know because 170 lies between 10 and 189 We know this number whichever number this is this number Is going to be a two digit number because we calculated this range that for all the two digit numbers This is like the range of their positions. So we know the number is going to be between 10 So this is our number our number value of n Let's say let's call our number n. We know n is going to be between 10 and 99 Just from knowing this value of our current position k and knowing it's in this range Between 10 and 189. All right, so now let's actually look at the positions of our current number and see how they got their positions So let's say look at our number 13 this number 13 So what is the position of the starting value of 13? So where does this one start? Well, let's count. So we know that nine it ends at nine, right? So this first value is 10, right? So it's 10 11 12 13 14 15 16 Okay, so the starting value of 13 is at position 16. So this starting value 13 is at position The position of 13 is 16 So how do they get 16 at position 16? Well, if you think about it 13 minus the starting value of 10 13 minus 10 Is equal to three right Equal to three and because there's every two digits They're skipping every two digits, right? So because there's every two digits here Every two digits here, right? So 10 11 12. These are every two digits three times two Is equal to six Right, so three times two is equal to six and If we take this value six We add it to the starting value of 10 So we have this starting value of 10 10 plus six Is equal to 16. So that's how they got that's basically how they got This value of this 13 to be at 16 at position 16, right? They took the number Here that they're at subtracted from the starting position Multiply it by the number of digits in this case It's two and then add it to the starting digits starting position So they got the number 16 At the position of one at 16 So What we could do is that because we have this uh lower bound of 10 and we have this upper bound of 99 In our number and we have this value of k This position we could what we could do is we could use binary search Okay, so 10 and we know our numbers between 10 99 So what we could do is we could just guess like the middle. So we take 99 minus 10 Right, and then we get the middle right so we get our our midpoint And then what we do is what we're going to do is we're going to take this midpoint and subtract it From 10 Okay, so we take this midpoint and we subtract it by 10. So we're going to take this mid And subtract it by 10 Right, and then what we're going to do is we're going to multiply it by the number of digits So because we know this We because because we know that our number has two digits, right? Because of this range between 10 and 189 we take mid minus 10 multiply it by two And then we're going to get a another number. This is going to be our position, right? We just have to check this position with k And if it's greater, um, we move our Our left bound to the top and if it's less then we're just going to move the right the right range of 99 down Because of this we will we will be able to get Our solution of our answer by using binary search on the digits So, yeah, all right guys. So before I go over the code, let's just go back to what we had before So remember 10 to 999. These are the two digits Right the numbers of two digit numbers. Remember, we said 99 minus 10 plus one would give us 90 And remember we said for three digit numbers, we have 100 to 9 and 199 And then we said 999 minus 100 plus one would give us 900, right? As you can see here, you don't actually have to calculate 99 minus 10 plus one Um to get 90 you could just subtract powers of 10. So we do 100 minus 10 would give us 90 So this is just like a faster way to do it when we want to code this up to do that in a faster way That's what I did. I just actually put powers of 10. So I actually calculated powers of 10 So here we have an array of vector of powers of 10 and I have not 19 of them Yeah, I set all them to have one right set them all to have one And what I'm doing is I'm just going to loop through and I'm just going to create powers of 10 So I'm going to take the previous number and multiply by 10 So that's going to create like one 10 and 100, 1000 and 10,000 and 100,000 so on so And this will help us calculate these numbers for the number of the range between the two digits and the position All right, so I read in t which represents the number of test cases And then I do while t minus minus keep decrementing the test cases each time And here's what I do is I read in the number n and this is going to represent our k, right? Um, I should have named it k, but it should be like the index What we're trying to search for I have this variable called num digits, which is what we need to find So remember we have to calculate number of digits find out the number of digits for the number given the index Right. Remember that's what we said. So I'm to have this variable for that I have this variable called digits, which just represents the current digits that we're Adding so far and then I have a previous digit that represents the previous digits that were added And I use that later. So here I'm going to loop through from 1 to 19 Which is representing the reason why it's 19 is because there's 10 to the 18th This is the maximum number you could have for your k and that's also another reason why the power is 19 Starting from index zero we have one and then we go up to Up to 18, which is going to be 10 to the 18, right in our powers Just like what we said before here digits the number of digits is going to equal to what we said before here 100 minus 10, which will give us 90 because every time it's going to give be 90 909,000 so on and so forth. This is an easier way to do it So the powers we're going to subtract the previous powers for our digits And I'm going to multiply by i the reason why is because i represents the number of digits Okay, so Remember we had a one two three five six seventy nine, right? So remember this is that what we had before remember we start first digits is one and it goes to two Right, we're repeating two digits here 10 11 12 13 14 15 16 17 then it goes to three digits. It's four digits five digits Right, so this that's what this i represents right We're multiplying by the number of digits every time that we're looping through and then here Is this number digits is actually the length? I'll rename this like yeah digit length, okay? So this that's what this represents. It's just like the digit length that we're incrementing Then if the digit length is greater than or equal to our index And the number of digits that we have will equal to i Right because that's a number of digits that we're repeating because we're doing one Then two then three then four then five Remember we're trying to find the length that it's repeating and that represents i So our number digit variable is going to equal to i because that's what it represents and this Digit like yeah, if our digit length is greater than or equal to the index I'm going to rename it because it's kind of hard to understand this but yeah, if it's equal to Greater than equal to k then we our number digit is going to equal to i and then we break means that we found the digit length that's repeating then I have this previous digits Which is going to equal to I keep track of the previous Length now we now since we have our number number digits that are repeating We could just create use binary search now. So we have low is going to equal to num digits minus one Power of that. So like let's say it was let's say we're repeating 100. So right right here, okay? So this would represent what it represents is if our number digits is like three Powers of two powers of three minus one would be two power of 10 10 to the two would be equal to 100 high would equal to powers of three three powers through would be thousand minus one which would be 999 So what low and high represents here? It will be representing if it was three digits This is what we're low would be and this is what our high would be if we're repeating every three digits That would be our low and then this would be our high then while low is less than or equal to high Here I have the answer position and I also have starting answer position Because I need the starting position also where low is less than or equal to high We're going to get the midpoint. We're going to calculate the starting position So to do that what we're doing is we're going to take our midpoint Which is the one that we're trying to search for trying to check midpoint is going to subtract by Number digits minus one which is going to represent 100 in this case our range And we're going to multiply by number of digits, which would be three Right, there's three of these and then plus previous digits. So previous digits Were to represent all the previous digits beforehand So all these beforehand, this would be like a previous digit Previous number digits, right previous number digits I'm going to I'm going to name this previous number digits previous number digits Is going to equal to the current digit line previous number digits We equal to all the values beforehand because we need that later on midpoint minus the powers Minus one which will be our range of our upper range. So I take midpoint minus upper range Then you to check if it's right I multiply the number of digits because we needed that's number of times we're repeating three like three And then I plus previous number digits would which will represent Represent the position that we're repeating at and since we're repeating it we're stopping at this last position So I assume assume these guys are like in the same line But when we stop at the last position, yeah, we need to get our position So that's what previous number digit does is that we're adding by all the position of the previous Digits, I'll rename it again previous number position And then we need to add one because it goes to the next position Right that we're searching through. So yeah, this is all be our starting position If it's less than or equal to k the index that we want the position that we want Okay, if it's less than or equal to that, we need to check if mid is greater than our answer Which is going to be and then we set our answer to equal to mid And then our starting position we're going to be able to start position If it's less than or equal to k I said low we're going to increase the range. So this range of 100 is going to change to The midpoint which we buy 100 ish. So this this low of 100 We know it can't be in this range of 100 So we're going to go to 501 to 900 otherwise I take high is going to equal to mid So while this is done while our binary search is done Searching through all the values for from 100 to 999 What we're going to do now is we're just going to convert the answer to two string So we have our final answer of our number that we keep kept checking over and over again Converted to a two string and this will be our number and then if you want the current position. So let's say I want this digit this 13 at the 17th position Right this three. So I want the so let's say k is equal to 17 So since I already know the starting position is 16 All I have to do is choose would be one here This starting position, which would be 16 And I take 17 minus 16, which are going to get me to one And then I just print out the position of one of my number that I found So yeah, I just print that out and I'll be the uh, I'll be the digit number that will be at that position Okay, I hope you guys know this video rate com subscribe. I'm pretty tired. Yeah peace