 What's up guys, my name is Michael and welcome to my YouTube channel. Today we are going to go over a problem called Juca So basically you're given So there's this story where you have Claudia and Natalie has 10 apples together And then Claudia has two more apples and Natalie and you want to know how many apples each girl has right and then Yeah, and then the student tried to check if Juca's was an accidental like what Juca said about Claudia and Natalie So then she gives them with bigger numbers So now now that we have now what they're asking is that There's 10 test cases given one after the other you have to process them all each test case consists of two lines The first line is how many apples that both girls the girls have together and the second line So it shows how many more apples Claudia has So yeah, both of them are positive integers and now we have to output The number of apples belong to Claudia and the one belonging to Natalie. Okay so that's basically the problem statement and Now let's actually Let's actually look at the how to do this problem and I'll explain So let's just look back at the problem statement what we're given. So each line contains two Two lines first one is how many apples both of them have together the second line shows how many more apples Claudia has how many more apples Claudia has okay, so if we have Natalie and Claudia so let's say Natalie has n and then Claudia has Okay, so our first line we're given is n plus k because we're given how many they both have Right the first line how many they both have together the second line shows how many more apples Claudia has so that's How many more apples Claudia has so that's k Minus n. Okay, so this is the first line And then now we want to know how many each of them have both of them positive If you want to find how much they each of them have you just have to add these equations up So these would cancel so let's say let's say our sum here was the first one was like s and this one's d Right, so then if you add up both of these equations You get 2k is going to equal s plus d and Then if you divide by two both sides, you're going to say k is equal to s plus d over 2 Okay, and then if you plug it back in you're gonna get n is equal to s minus Okay. Yeah So I just plug this k back in okay, so that's basically the gist of the problem You just have to print out both of these numbers The hard part about this problem is actually because the it's using big energy integers, right? So we're gonna actually have to create a gigantic array of 100 digits And then carry the carry every single time something Something works or something doesn't work. You know what I mean, so yeah Let's just let's just go dive right into that Okay, so the first thing we need is to know is that you we have to try to solve The sum of both of these numbers s plus d and then divide by two because that's that's the main gist of the problem And then we have to subtract so there's actually three operations we have to do One is to add them together. So we have to add addition of big numbers Subtraction of big numbers and also division of big numbers. So if we're able to do all of those and what we're good So let's go over. How do you actually do division of big numbers? So let's say I have like a gigantic number like seven eight Seven eight nine ten plus The other than not ten Yeah, seven, I don't know this number plus five six seven eight nine So do you basically have to do if you assume each of these digits are like a gigantic array so like each of these are an array What you're gonna have to do is you're gonna have to go through every single value in this array zero one two three four You're gonna have to add them up. So If you're gonna have to start from zero Um, it's actually better if you flip Yeah, okay, so you're gonna have to start from You either have to start from the beginning or the end and then carry it. So My actually better if we like the reverse the digits so then now the zero can actually be at the zero place here So then if we actually reverse the digits the zero place would be Do one zero this this this place, right? So it started the zero place one's place So you would first add up the first two digits at each corresponding place, right? And then you have to carry the the one carry the one. So How you do this is you would add this up. So this would seven plus nine is 16 Right, and then what you're gonna have to do is you're gonna have to take this number I'm mod it by 10 to get the last digit of 16, right? So the six and that'll be your one. I'll be where you place this number at then you have to Take 16. So we have 16, right? You have to mod it by 10. So 16 mod by 10 gives you six So that's gonna be the one where you place here and then we have to do 16 divided by 10 So then we get one Because it's a lowest bound and that'll give you your carry So then you would have to put the carry up on top and then you have to continue adding this so nine plus One ten once you have ten you have to divide it by mod by ten you get zero Right, and then you would have zero here and then you would divide it by ten and you would continue doing this over and over And so that's how you do integer big integer All right guys, so now we're gonna go over subtraction which is kind of the same thing you just have to borrow the Bar the one from the From the left hand side if it doesn't work out. So like let's say I have let's say I have like seven eight minus I don't know Nineteen right so 78 minus 19. So what you would do is that if you're going to loop through these digits Let's actually make it one zero. Yeah, so you would start here And if if it's less than the other digit you would actually add by ten So you would take this one add by ten so be 18 and then you subtract 18 from nine and you get nine If it's too much right if it's like greater than ten then you have to mod it by ten Right, and they'll be your digit. So then you would do that. So they'll be nine Right, so in 18 minus nine is it equal to nine and that'll be that leftover right now here you have seven you have to Since we borrowed the one we have to make seven to a six So now seven becomes a six and then you just subtract six minus one which is five Okay, so that's pretty much it how you would do subtraction. You just have to keep borrowing from the left side of its Too small right you would add ten to your original digit and then subtract and then if it's greater then you would You would have like a remainder, right? So then you would just you know mod by ten to get your digit But yeah, so if it's greater you would borrow one from the left side. Also if let's say there was like Let's say this was not 78 but like nine, right? So then this would be 18 minus nine would be nine So then you have six. So if there's nothing on the left side anymore, right? You just you just carry the rest over so it would be 69. Yeah, so that's basically how you do subtraction Okay, so now let's go over Division so next one is the division by two and that's a little bit more complicated, but I'll see if I could go over that. Okay, so like if you're dividing by two, let's have 38 here and I divide by two so you would take the left digit here and divide it by two and You would need to get the remainder so you'd have to take a left digit mod by two also to get the remainder So then there would be one So now to continue to the division you'd actually have to take your Reminder here and multiply by ten so you'd one times ten So then I'll give you ten and then you have to take ten you add eighteen add eight to get eighteen right, so that's that's basically carrying the eight down here and then When that happens then you take the leftover and then you divide by two and then that'll be nine So then you subtract two times nine eighteen you get zero So, yeah, that's pretty much it for division Just just make sure that whenever you get a remainder you you have to multiply by ten and then add it add the What's left over the next digit that's going that you're iterating from right so the next digit you have to Add by ten. Okay, so there's another case where it's like, okay So the other case is if there's absolutely no remainder so like let's say do 18 divided by two So if there's no remainder right you would you do Let's say you divided by it right you'd two times nine would become 18 and you get zero So there's no remainder here. So then what you do is you actually just do you just push back the This value divided by two right this original value divided by two and you put that there Um, if the current digits less than two you have to add a zero. So like let's say I have like ten divided by two And you have five you need to push back a zero before it So like so like here you would have to push back a zero before the five and you have ten That just to make sure that you're actually getting all the digits down. But yeah, that's pretty much it That's how you do division by two. But yeah, you just push back a zero in front I'm gonna explain the code now and then yeah, we'll be on our way our guys I'm gonna explain the code real quick and then I'm gonna head to bed So they said they wanted ten test cases. So I looped from zero to ten, right and I have this string k and n So k represents what quality has n represents what nally has and I read both of them Then I have to create vectors of integers to represent each digit to store them So I'll have digits of k and digits of n Then I loop through the string of k and then I'm gonna add every single character in the string push back to the back Into our array and then I'm gonna convert every character into its Equivalent digit form. So that's what this does. So then this stores it into the array Then I'm gonna do the same thing for n I go through every string in the character in n Convert it into its equivalent digit form and then I store it into the array dig n Then I'm gonna call sum to add k and n right because we remember we have to add k plus n and divide by 2 Right, that's the first value and then we have the second value is k minus that number So here we add I create a function called add and I'm gonna show that later But basically I add k and n the digits of k plus the digits of n to get my sum Return a summary then I divide this summary by 2 to get my div array And then basically I just print out every single digit in my div array Then for afterwards you have to find the values how much it's You have to take the digits of k and subtract by What the what the value you had previously, right? That's what we had to do like I showed you in the video So you subtract that and that's gonna be your diff So I store that into diff and then I loop through diff the diff array And then I just print out the diff array every single value in diff So now let's go over the each function So first of all we need to have the functions of add Add basically just add to all the digits of a and b and there returns a new array of New array for the sums added the digits added So because I use push back eventually in the beginning instead of reading in by 0 to the end I have to reverse the array because I added it Appending it to the end and that's not the right order. So I reverse it so that the 0th value represents the ones digit so do that for both of them and Then what I do is that I need to find the Minimum length of each of them because that's the how much I actually have to loop to add Like I don't need to continue adding Afterwards because of that and I have an array called answer and that's just gonna store all the answers of the digits And I need a carry because this carry His variable call carry is gonna represent every time I add something I'm gonna change if it's the values like greater than 10. I'm gonna change Change the carry and put that on top. So that's what that is for Okay, so I loop through from 0 to the length the minimum length, right? I'm gonna add every single digit of a and b Each digit a and b and then I'm gonna add by carry. So initially carry is 0 But we're gonna replace that after each iteration. Okay So this is gonna be our value and that's a value that we're gonna we have for each ones digit Then what we're gonna do is we're gonna divide by 10 to get our carry So like if I have like if I sum up like 78 plus 9 right 8 plus 9 gets you 17 and 17 would be our value But I need to get the carry of the 1 right so I do is I take 17 divide by 10 and that's going to give me the Carry of the 1 so that's what this carries for okay then I'm going to push back the The last the ones place of the digit so back to 78 8 plus 9 right so the ones there's of 8 plus 9 is 17 so 17 Has the value of 1 is the carry so that's scary right and then um the The actual digit that we're gonna place is gonna be the 7 and that's if you take 17 and get the remainder when you When you modify 10 so when you modify 10 the remainder is 7 so that's the answer that we push back Okay, so this is the start when Gets a little bit tricky. So here's what's gonna happen is if we have leftovers so after when we're done With the leftovers right so like if we have like 78 plus 9 right we're gonna have a leftovers of so Back when we have Back when we had a 78 plus 9 So this 8 plus 9 is 17 and then we have one so 87 right So the minimum length of both of these is one because there's only one digit of 9 so Yeah, so what what this second part here is that it's gonna Continue adding after we're past the length so when we're past the length in this place We're past the ones place already right. There's no more digits for the second value of 9 right So we still have to continue adding even there's no more digits at this part right at this 10th place There's no more digits here. So we still have to keep adding. So that's what this is for So this is gonna start from the length that we just finished off of and it's gonna continue adding and That's basically what it's doing. It's gonna continue adding and then it's going to reach values where we're gonna I would do the same thing we add the carry right so in this case So we just take the carry of 1 and plus 7 and yeah, that's what it does. So it's gonna add the carry It'll divide by 10 if There's still more carries left over right so to get the more more carry and it'll push back the ones place of it So like if in this case if you had like I don't know If this was like a 9 Right, let's say we had 98 plus 9 So so we had 98 plus 9 We have 17 right we carry 1 so in this case we still have to continue adding 1 plus 9 So that's what it does a plus 1 plus 9 and you get to 10 and then if it's 10 You just have to append like 0 and then the 1 so that's what that's the code is doing It's gonna add the rest of it We do the same thing for the second part if we have more left over for B For B so we do this exact same thing We just loop through it and we add the carry and continue adding the carry and adding it to our answer ray okay, so that's what the both of those cases do and Like this the same case would do it if like B is greater So that would be like 9 plus 98 so in this case the second the second raise bigger has a higher length But you still have to keep adding right so you have 9 plus 8 17 Carry the one you still have to keep adding this side 1 plus 9 is 10. So that's what that's for So that's what the B part is Here's when it gets a little bit more tricky like let's say we have even more carries so back to this part of 1 0 7 1 plus 9 gets you 10 right so we have another carry at 1 so that's gonna we still have to append the rest of that So that's why we add that here So in that case here went while the carries greater than zero We're still gonna get the remainder mod it by 10 to push it back that gets the ones place and then we divide by 10 So that's gonna keep doing it right so that'll actually add the zero and then add the one after this carries on After that, I'm gonna reverse the array again because I pushed back push back means you add it to the end so that means our When we did push back and as then we actually add in a reverse order So we have to reverse it again. So that's what this is for. Okay. Now. Let's look at subtraction Okay, so in subtraction you I'm assuming that you we already reversed x Right because we did that in the top So in this case only y is out of order. So we just reverse y Here then we still have the answer Our answer array and then we have our length. So in length is will length of the second The second array that we're subtracting from so here is subtracting y from x. Okay, that's what this is doing Okay, so what are you here? Okay? we're gonna loop through the length of The second value because we're subtracting Y from X so in this case, it would be like let's 78 divided by 78 minus 9 so let's say you had 78 minus 9 So and that's what this is case of doing well We're gonna loop through only up to the length of 9. So that's 1 that's what we're doing here Okay, because there's no need to keep looping the rest, right? Okay, so here what we do is that we're gonna compare the ones with digit with the ones that are the second place So if it's less than so in this case This one's digit of 8 is less than 9. So we need to borrow from the left side So that's what this is doing. It's going to borrow from the left side. So it's gonna subtract from the left This is I plus 1 so in our case it's gonna be Or so how we stored it we did it to the right we plus 1 to write but Yeah, it's actually We're actually looking the array index like this. So this is the 0th place This is the 1s place and this is this 2s 2nd place Right based on how we stored it. So yeah here it would actually take 8 it would take the 0th plus 1 which is the first place and it would borrow 1 so it's going to subtract 1 from this So it'd be 6 right. So that's what that's doing And then we have to push back. So this part we're gonna add 10 plus the original number. So here it's gonna take 10 plus 8 gets you 18 and then you could subtract it So that's what it does here 10 plus 8 it gets you 18 and you subtract it from from 9 so that will give you 9 and Yeah, that's what it does. It pushes that back and it continues going forward If it's not Less than it then you we don't have to borrow so then we just push back the original value of the digits minus X minus Y so in this case, let's say we had at this point We have six and this is a zero right six is greater than zero. So we just have to subtract it We don't have to borrow from the left side. So it just gets you that so it gets you 69. So that's this part, okay? Okay Then we have to append the rest of the digits of the X And that's that's what I'm doing here. Um, so like in this case, remember we had like 78 minus 9, right? We go back. We have 78 minus 9 18 6 right 18 minus 9 design So here we stuff to continue subtracting even though we're past the length of 9, right? The length of 9 was is just one digit. So we still have to subtract the rest of the digits So that this makes sure that this part just makes sure that you're subtracting the rest of the digits So it'd be 6 my 0 because it's a zero Right, so that's what this is for if the value is less than zero Then we have to borrow again from the left side. So you have to borrow another one So you do subtract I plus 1 minus minus and then you have to add by 10 Add by 10 by your current value in order to borrow, right? If in case we have like zero or something So, yeah, that's what this is for. Um, and then you continue adding it Add the rest Yeah, and then we reverse our answer because we were pushing back to the end And that's not actually how you wanted to push back. You just want We've been pushing it to the end So we have to reverse the numbers in order to get it to the right order again Okay here this part what we're doing is that we're actually just Find the first occurrence that's not zero and then loop through it and add the rest of the values So this is what what this code is doing is that? let's say we had like I don't know 10 minus 5 right here you 10 minus 5 is 5 and then We just were done here right but it would actually store a zero here. So what this code is doing is it's going to remove the starting zeros so what this code is doing is that we're going to go through the Array again the answer of array and find the first occurrence that is not Zero so that's what this while loop is doing this while loop is going to find the first occurrence It's not zero so in this case it would be 5 and Then what this is doing is it's going to push back to a new array called r and it's going to push all the ones that are not zero So they'll push back 5 to our answer ray and then we return it so this actually removes all the zeros prior to it So if we had like I don't know this many zeros it would remove all these and your answer is just gonna be 5 That's what that's what this code is doing It's like for loop find the first currency that's not zero and then it's going to go through the end And you're going to push back every single value Okay, so now we're going to do divide by two It's a bit a little tricky. Um, this is just dividing the array by two every digit by two So what is this doing? Okay, so we have answer of array All right answer rate and then we have value. Okay? So in this case, we're going to go through all the digit digits here And what we're going to do is that this value actually represents the remainder Yeah, it represents the remainder. So what is let's say we had an example of I Don't know one seven six. No one seven three. Okay, so when you divide by two Normally you would do a two times eight just be 16 and you subtract it, right? You get 13 two times six goes into the six 13 twice to a 12 and you have remainder one. So what this is doing is that we're going to loop through all the digits of one seven three If there's a remainder then we do that thing at first or the value of remainder zero So we don't do anything first so what this is doing is that If the current digits less than two We're actually just going to add the remainder of The remainder by the current digit and we add a zero so that's going to do like Here it's going to add a zero here, and then we have a remainder equal one. Okay. That's what it's doing Right. We add a zero to here and then we're going to have a remainder one because uh, that's what it's doing and Then while it goes to the second time, which is a seven Now we have a remainder one, right? So now seven is not less than two. So it skips here And what's going to do is it's going to take the remainder of Seven divided by two Right, so seven divided by two is going to give you Seven divided two is going to give you three Hold up something's not right. Yeah. Yeah, seven divided by two is going to give you a remainder one It's gonna be a remainder one right seven divided two three six one So that's going to be your Last value on the bottom. So that's going to be your value here. So that's your remainder one So you have to this is this is the remainder one Of doing seven divided by two Right, so I'll give that's that gives you this one And then what it does is it's going to push back the remainder of 17 divided uh seven divided by two So that's seven divided by two Three so that gives you seven divided two it gives you three and that's going to push down the remainder of this That's what it does Okay, and then it continues doing that over and over again Until you've gotten your answer of your quotient zero eight six right Okay, um at this point then we're also we're gonna remove all the Uh corresponding zeros in the beginning. So that's what this is doing So we're going to start from the beginning of our um while it's equal to zero We're going to uh Increment our index of i and then for the rest we're going to push back the ones that are not zero So this finds the first occurrence of zero. So then it finds uh And first non occurrence of zero finds eight and it's going to push back all the values after eight So it gets you 86 So yeah, that's what this does. I hope you guys understood what I was doing Ray com subscribe Yeah, so after when you're subtracted Do all this stuff. Yeah, I'll check you guys later. This is a very long problem, but it wasn't that it's It's not as complicated as what you think it is. It's just like big integer. So yeah, Ray com subscribe. I'll check you guys later