 What's up guys, mine is Michael and welcome to my YouTube channel. Today we are going to go over another problem called adding reverse numbers. So basically your job is just to give in two numbers, you're going to reverse both of them, you're going to add them up and then you're going to reverse them again. So let's say we have 24, if you reverse 24, you're just going to get 42. If you have one, reverse one, you're still just going to get one, right? You reverse all the digits. So now when you add 42 plus one, you get 43. Now if you reverse for 43, reverse all the digits of 43, you get 34. Okay, so that's basically the problem. When we mean by reverse, we mean like swap them to like to the end, right? Let's go over another test case. So the other test case is 4358. So if you have 4358. And then the other one is 754. So if you were to reverse 4358, you get 8534. And if you were to reverse 754, you get 457. Now if you were to add them up 8534 plus 457. And then if you were to reverse this, you get 198. And then I think that's the answer. Yeah, 198. So yeah, I hope you guys get the gist of the the problem about how to reverse it. So yeah. So now let's talk about how do you solve this problem. One way to do this is actually just to convert this into a string. So if you convert to a string, you have 24 convert one into a string, you get one. And then you could like do some whatever string manipulation function. And you would get like whatever string manipulation function. And then you could just loop to this to the middle and swap it to the end. So then you would get a string of 24 if you want to reverse it. And that's what you also can have a string of one. And then you could convert this back to a number to like two integer or whatever. And whatever method it is, cover back to a number. And then you could add these together 42 plus one, because they're back to numbers now. And then you could convert this back to a string, which we have 43. And then you could reverse this again. You have 34. And then you could convert this back to an or you could just print this out. Yeah, you could at this point, you could just print it out. This this is a very, very inefficient though. If you just keep converting between string number string number. And there might be a there might be a faster optimization library that people use. But that's not really that efficient. So let's talk about how to reverse the digits of a number. So let's talk about 24 first. So let's say I want to reverse 24. So this is 24. To reverse 24. Let's think about what 24 represents. 24 represents 20 plus four. Right. And what does 20 plus four represent? Well, I could represent 20 as two times 10 to the first plus how do I represent four? I could represent four as four times 10 to the zero, because that's the first place, right? 10 to the zero is one. So then four times one is four. So now think about it. If I have this as an value of a number. So if you think about this two times into the first plus four times into zero, this would be your number of representing and biggest 10. Now let's look at how can I represent 42 by giving 42 and I want to represent 42. 42 is four times 10 to the first plus two times 10 to the zero. So now if I want to, so let's think about it, how, how would it be a good way to do this? Well, what I could do is I could take the first digit, I could loop through each digit, right? So I could go through a 24 and I keep dividing by 10. So if I keep dividing my 10 and I get the remainder of when I divide by 10, so if I take 24 divided by 10, I could get the digit of four, right? So once I have the digit of four, I just have to multiply by 10 to the first, right? Then I have to get the second digit of two. And once I get the second digit of two, I just have to multiply by 10 to the zero, right? So now what does 10 to the first represent? Well, 10 to the first represent is actually just the number of digits minus one, right? This number, this number, this number one, the number of digits of 42 is two, right? There's two different digits. There's this one and this one, there's two different digits, right? So if I want to convert to the exponent of the power of whatever is on my left side, that's just going to be the number of digits minus one, right? So like four times 10 to the one would be four times 10 to the one, and this would be two times 10 to the zero. Right? So if I were to iterate from the total number of digits going down to zero, right, get every single digit from the left side to the right side of the original number, then I just multiply them together and add, I'm just going to get my answer, right? So like, let's go over an example again. So let's say I have like 24 again, I'm going to get the left side of the digit, this digit by dividing by taking the remainder mod by 10. So that's going to give me four, right? If I mod by 10, I get four. So how many digits are there here? There's two digits. So what am I going to do? I'm going to take four, multiply by 10 to the number of digits minus one. So that's going to be one. Okay. Now I'm going to decrement this number of digits down to one now. Okay. So now I'm done with the digit four. So now I don't need this. So I'm going to divide by 10. So now 24, and I'm going to have two now. So now two, I'm going to mod by 10 to get the digit on this side. So that gives me two, right? So then from this answer that I got, I'm going to add by this digit to multiply by 10 to the number of digits here minus one, which is going to be zero. And then I'll get my answer. The ones I add, add these up, I get 4240 plus two, which gives me 42. Okay, so if you were to iterate from the number of digits minus one down to zero, multiply by 10, get every single digit from the left side, you're going to be able to reverse it. Then once you reverse digits, you could just add them up and then call the reverse function again. So yeah, that's pretty much the gist of it. I hope you guys understand what I'm doing. And I'm going to show you the code now our guys. So now let's read in the code. I'm going to explain the code now here is the the first first we have our three numbers. The first one is the number of test cases. The second one is the reading in the two numbers each for each test case, right? And then here I read in the first test case on the number of test cases. And here what I'm doing is while the number I'm decrement number test cases, and I read in each test case and each test case has the first number and the second number. So I call that m and n. And what I do is I call a function called reverse. I store that into a long, long integer. And then I call the function reverse again to reverse the second number I store in the long, long. And then I add the two numbers together. And then I recall reverse again on that. And I'm printing that out. So that's the whole gist of the first part of the algorithm. Now let's go to the top part on the reverse function. So how does the reverse function work? First, I need to get the number of digits from number because we have to count the number of digits for the number. Okay. And there I'll go over that sooner. Actually, let's go over that right now. So to get the number of digits, what do you do is just you just while then you store a copy of the number, right, you pass it in the number here digits number. And then what I do is while it's greater than zero, the number is greater than zero, I'm going to keep adding by one. And I'm going to divide by 10, because that each time you divide by 10, you get the last digit on the ones place. And then you divide by 10, you get the next one, the next one. Yeah. And then each time I divide by 10, I add one to this G, which is a counter. In the end, I just return G, and that'll be the number of digits for the number. So now here after you have the number of digits, I put that as size, which is the size of the number of digits. And I have my answer, right, the value that I'm adding each time, right, the value answer that I'm going to return, this is the value I'm going to return. So I'm going to loop through from zero to the number of digits. And I'm going to get the ones place every time of our current digit of the current x, the current number, I'm going to get the ones place by modding by 10. And then what I do is I just multiply this number by 10 to the power of number digits minus one minus I minus one, right? So I just going up to the number of digits. So if you subtract the number digits by I, you're going to get the I minus one is like decrementing the counter by one over and over again. Yeah. And then we are multiplying this by the one, the digit in the ones place. So that's what this does. And then once we do that, we add it to our answer, which is what we did in there when I showed it to you guys, that earlier there. And then each time we do that, we're dividing by 10 again, right? We're dividing the original number by 10 again over and over and again to get the remaining values, right? And then after that, we just return answer. So that's pretty much the gist of the code. If you guys have any other questions, you could comment down below. But yeah, pretty much this this counter is just a times 10 to the number of digits minus I minus one. That's this is going to make sure that you're decrementing by one each time for the digits, the the power exponent that you're going up to. But yeah, then after that, after all those are reversed, have them up printed out reversed. And that's pretty much it. Rate, comment, subscribe. I'm going to do another two problems and I'll check you guys later. Peace.