 What's up guys, my name is Michael and welcome to my YouTube channel today we are going to go over a problem called the next palindrome and it's an ad hoc problem. So it's a problem that we're just requires you to you don't need to know anything about algorithms you just need to think and solve the problem. So it's a problem about thinking. Okay, so recall what I said about a palindrome a palindrome is let's say we have a number of two, no, not two, nine, two, nine, okay. And a palindrome is when the left most number is equal to the right most number that's basically what a palindrome is. So like if for all the numbers, the left one is equal to the right one, okay. So if in this case, nine is equal to the right most number, not nine. So it is still considered a palindrome and two is equal to the itself to is equal to itself. So it's also a palindrome. So this is a palindrome. Okay. Now, another number two, one, three, two, this is not a palindrome, although two is equal to the right most value of two, one is not equal to the right most value of three, right? One is not equal to three, right? So the a palindrome is basically if you were to reverse the numbers up to the middle and they're equal, then that means that it's a palindrome. So this is not a palindrome where one is not equal to three, like this one is not equal to three. Okay. So it's not a palindrome. Okay. So that's what a palindrome is. Now, back to the problem, we're given a number K. So we're given the number of K and we want to find the smallest number palindrome, right? The smallest palindrome that is just larger than it, right? Just larger than K. So in this case K, we want to find the smallest palindrome just larger than K. So first input statement is 808. What is the smallest palindrome just larger than 808, right? So if you were to list out all the palindrome, the numbers from 808 and continue, so remember K is equal to 808 and just list out all the numbers 808, 809, 810, continue, continue. 811, 812, 813, 814, 815, 816, 817, 818, right? And you were to continually check if it's a palindrome or not. So 809 is not a palindrome, 810 is not a palindrome, 811 is not a palindrome, 812 is not a palindrome, right? So on so forth, we will figure out that 818 is a palindrome. And this is the smallest number, smallest palindrome just larger than 808, right? Because we only have took a few steps of adding one to get to 818. So now one way you could do this is literally do what we just said before is just keep adding one to this K value and then just checking, is it a palindrome? If it's a palindrome, then we just return true or false. Okay, so now the problem is that this takes too much time. So what is, how would you solve this problem? Okay, let's look at some really simple test cases to solve it. So let's say the number was 999, right? So what is the largest palindrome here? The smallest palindrome just larger than 99. Well if you keep adding one and one, you would get the smallest palindrome just larger than this is 101, okay? And if you were to check 999, the smallest palindrome just larger than this is going to be, if you keep adding one, you get 1000 at one, you get 1001. And if you check 9999, continue to add 1001, okay? So if all the numbers are nines, if all the numbers are nines, then that means that all you have to do is just literally just print out one, print out the number one. And then n minus one zeros and then print out another one, okay? So if it's all the values are nines, you just print out one and then the number of nines, the length of the value of k, the length of this k value. So then this k value initially, the length is equal to four, right? So if all the values are nines, right, you just have to print out one, then you print out three zeros, and then print out another one, okay? So this three is just, three is just the length of the number of nines minus one, okay? And in this case, it's the same thing. So the number of nines here is three, three, right? This length is three. So then in this case, we print out one, one, one, and then we print out two, two zeros, and then we print out one, one, right? So this is just going to be the length of the initial string of k minus one, which is two. So you print out one, one, and then two zeros and then a one, and then you do the same thing for this, nine, nine. So you print out one and then a zero and then a one, one, zero, okay? So that's the case of having all nines. And that's a simple case to figure out. But now let's go back to our values of eight, eight-oh-eight, okay? So k is equal to eight-oh-eight. How do I convert this number into the smallest palindrome of eight-eighteen? Well, if you just look at the number of eight-oh-eight and eight-eighteen, you realize that the only difference is the middle number of one, of zero and one. So if this is just the number of zero and one, all they did was they just added one to the middle number. This may work, this may work. But if you have a even number of numbers, let's say you have two, one, three, three. How are you going to, you can't just add one to two, one, three, three, right? What is the middle number? Right? The middle number, you don't know what the middle number is. So let's actually go to the test case of two, one, three, three and see how to do this problem. So remember k is equal to two, one, three, three and they said that the smallest palindrome of two, one, three, three, based on the test cases, right, is two, two, two, two. So if we were to go back to this, this value is two, two, two, two and this is the smallest palindrome. The smallest palindrome is larger than k. So how do they get two, two, two, okay? So thinking back to our initial idea of just adding one to the middle value and replacing it, let's actually look at two, one, three, three. But if we were to split this two, one, three, three into two parts, so we were to split this right in the middle, left side will be two, one and right side would be three, three. Now let's look at the right most digit of one and left most digit of three, three. So that in this case would just be with this one and this three. Now how did they get two, two, two, two, right? The smallest palindrome two, two, two, well, what did, what did they do? They took this one and they added it by one, right? Two, one, two, two is two, two, two, two is just two, one and added by one. So they took this number, they added it by one and they got two, two. And what did they do? They concatenated this two, two with its itself to get two, two, two, two, right? They took this last number, added by one, which gets you two, two and then they just concatenate it with itself to get two, two, two. So if the left most number is smaller than the right most number, the first digit of the right number, all you have to do is add one to the left most digit, which is two, two, and then just concatenate it with itself to get two, two, two, two, okay? So that's all you have to do. Okay, so that's one scenario. But what if you have a number that's larger than it? So let's say we have two, one, four, three. How would you do this problem, right? Well, oh no, not two, one, four, three, my fault. That's just the same thing as what we had before, two, four, three, seven, okay? So if we look at, if we split this number, okay, it's two, four, three, seven. And split this in half, two, four, and three, seven. So you have two, four, and then three, seven. Let's compare this first, these digits, four and three. Two, four, three, four is larger than three. So what do we do? Well, if you were to think about what is the smallest palindrome just larger than K? Two, four, three, seven, right? Can't we just print out the left most side and then reverse the digits, right? So from this point, we could just print out two, four, three, seven, right? Two, four, three, seven is our K value. The smallest palindrome just larger than K is actually two, four, four, two, okay? So can't we just take if this, if this left digit is greater than this right digit? Can't we just take our left digit and just print out itself, two, four, and concatenate it with its own digits reversed, four, two, right? Two, four, four, two. Because that would get you the smallest palindrome of two, four, four, two. So yeah, that's what you would do in order to do this problem. If it's greater than, if the four is greater than three, if the smallest five is greater than three, then we would actually do that. Now what if we get to a point where we have two there with equal? So let's say we have two, one. Let's go back to the case of, let's say we have eight, nine, seven, six. Okay, no, my fault, my fault. So let's say I go to a case where they're exactly equal. So let's say I have two, two, let's say I have like a one and then a seven and then like a six and a five or something like this, right? In this case, this two is equal to this two, okay? So what do I, what should I do at this point? Well at this point, you actually just continue going, right? You could just continue going, continue checking it. Because we don't know if we should concatenate it with itself or it with by itself. So we just actually keep going. So we're actually gonna loop down from this one to the left and loop this to the right. So at this point, we'll check this one and the seven, okay? So we continue going, check one and seven. Now, remember we split this in half, okay? Check one and seven. Now, is one less than seven? It is, one is less than seven. So what should we do? We take the leftmost digit of the left side of this, right? And add it by one. So we have six, one, two, right? Now we have six, one, three, right? Now, all we have to do is just concatenate it with itself and reverse the digits. So we would just have to do a three, one, six, okay? And that will be the smallest panel of the drone just larger than the initial value of here. So now let's actually explain the code and how we did this and then I'll be on our way. So let's go to the code right now and explain how to do this, okay? So initially we have our string of S. Now, let's actually check if they're all nines. Remember if they're all nines, all you have to do is print out one and then n minus one zeros and then print out one one. So if they're all nines, how do I check all nines? I just looped through the whole, I had a function called all nines. And I just looped through everything in the string. If it's not equal to a nine, I just return false. Otherwise, at the end, I'll return true. So then at this point, I check if all nines, if it's all nines, I print out one and I print out n minus one, which is the number of strings, the size of the string minus one, zeros, then I print out one one, right? And then I do a continue so that I could just go on to the next case. So I don't have to put an L statement, okay? All right, this is another case if they're all zeros. Now, I didn't go over that when you have all zeros because I'm not sure if the test case even has that. But if there's all zeros, what you would do is you would actually print out a one. N minus two zeros. So in this case, we have three minus two, which is one zero, and then we have a one. Wait, that would be your next palindrome. So that's what I did here. I check if they're all zeros, then I just print out one one. I print out n minus two zeros and I just print out a one, okay? So those are two specific test cases that are difficult to handle. Okay, now now it gets a little tricky. Okay, so I have the value of this. I create n is equal to the size of the string and I have a left and I have a right pointer. Initially, I set them both to be the middle value to be the middle number. So in this case, if we add like, I don't know, let's go back to the case of 808. Left and right pointer are both going to start in the middle. So this left and the right are both going to be in the middle. Okay, that's what I do here. All right. So remember the cases. So if, okay, yeah, in the middle. Now, if they are both, if the size mod two is even, now, if the number of letters are even number of the digits are even, you have to actually move left down one. And I could give you an example. Why? So back to 2143, right? The size of this number of digits, this is four. Right. So n is equal to four, the size of the digits here. Now, if I were to label the index is 0123, right? And if I set the left and right pointed to be n over two, so n is four. So four over two is two. So I set both of these at two. That's actually not right because I actually have to move the left down by one because if they're both at 0123, that's not the actual middle number. So we have two males, so we have to actually move left down by one. So I subtract one from left if it's, if the number of digits is even. So that's what I do here. So if number of digits is even, I subtract left by one. Okay. So now this is the part where it gets a little tricky. Remember back to our case when they're all the same in the middle. So if that's the case, remember we have two, five, two, two, seven, right? So in this case, left is equal to this part and the right corner is equal to this part. Now, if this, if they're equal, then I'm just going to move the left and right down by one. Okay. And I keep going that until I get to the end. So that's the reason why we'd have this. And we left down one and move the right down one. And that guarantees that at the end, I'm going to get, at least I get to a point where the left and the right are not the same. Okay. Now I get down to this part. Left pointer is less than zero or right, left pointer is less than right pointer. Okay. So in this case, if a left pointer is less than zero, then that means that I went past the string, right? So they're, they're definitely not equal at that point. They're definitely not equal. Now, if the left pointer is less than the right pointer, right? So this is just checking the last digit. Right. If the last digit is less than the very most last digit, right? So this is checking if I don't know, at this point, you're going to get five two to seven, you're going to get left pointer is five and the right pointer is going to equal to seven. And it's going to check five and seven. Okay. Now, if the left is less than the right, remember what we do is we have to add one to the left. So if the left is less than the right. Left is less than the right. So in this case, five is less than seven. We need to add one to left. So remember if I check if the left pointer, this part is going to be, if left point is greater than equal to zero. So how do I add one? Well, to add one, you need to for strings in this case, you actually have to continue going downward, right? And then you to add one to left, you have to continue going downward and then add a carry. So that's what this is doing, right? Cause in case if we have like a nine plus one, you get a zero, right? Nine plus one is 10, but you have to add the carry, right? So that's what this is for adding one. So in this code, what it's doing is that it's converting the left, that digit to it's the string of the left digit. So this five, it converts the character of five into its number format. So it would be five, right? That's what it's doing. Um, it's also adding a carry in case if you need to carry, right? So initially carry is one, then you add to carry. So the carry becomes six. Then what we do is we divide it by 10. So then we actually, uh, get the carry of the number. So in this case, it would take, uh, so in this case, remember, we have to add one to our left value, right? So remember if left is less than right, we add one to the left, add one to the left, right? So what we did was here, we added one to left digit, so make it six, right? Five plus one is six. Then what do we do? We divide it by 10, right? To get in case if we have a carry. So if it was a nine plus one, it was a 10, you have to carry the one, right? Or if it was like, uh, I don't know, 11 plus one, something like that. But, uh, yeah, you have to carry the one. So then we carry it, I take the number mod by 10, right? And then, um, here, this number mod by 10 is going to be the leftover value of the one's place, right? Left over value, the one's place. And then if you add by this, this, uh, this zero, what this does is it converts it into a character. So this is going to set the string to its corresponding value. So in this case, it would set the string to become six. Okay. Now you have to actually set the left pointer. The right pointer is also equal to left pointer. So this right, we're going to actually set it to six also. Okay. That's what it does. Then we move the left pointer and the right pointed down by one. So left pointer is going to move down to the next corresponding character. In this case, it would be empty. And the right is going to move down to the next, uh, corresponding character. So then now these are gone. Now we go to the next corresponding character. So that's what this is doing. Is just adding one to the left side and resetting the values to the right. Okay. Now, once we're done with that, let's say the right value was, uh, the left value was not less than the right value. So let's say it was greater. So the other case where Mario was like eight, uh, let's see, it was a eight, five, one, six, let's say, let's say we have this, right? We split in the middle. This five is greater than, than, uh, than the left side, right? This five is greater than the right side. Of one. So what do we do? Um, all we just have to do is just reset, flip the values of the right side to be the exact opposite of the values on the left. So in this case, the left pointer, our left point is pointing to here, right? And the right pointer is pointing to here. What we're going to do is we're going to set the right pointer to equal to left. So whatever character at the right pointer is going to equal to left. Our left is five. So at this right pointer of one, we're going to change this into five. Okay. And then we just continue going down, uh, then we moved on to the left, down one and the right, down one. So now the left, we moved down to here and then the right move down to here. Now is the right greater than the left? Uh, is the left greater than the right? Yeah, it is. So then we change the right to be left. Our left is eight, right. So right is now eight. And then that's it. At the end, we just print out eight, five, five, eight, and that'll be the end of your palindrome. So yeah, that's basically how you do this, how you do this problem. Rate, comment, subscribe. Hope you guys enjoyed this video. I'll check you guys later. Peace.