 What's up guys, Mike, the coder here today, we're going to go over a minimum result by adding parentheses to expression. So you're given a zero indexed string expression, which is like num 1 and num 2. And num 1 and num 2 are like positive integers. So now what we're trying to do is we're trying to add parentheses around these expressions of the plus sign. And what we're trying to do is we are trying to get the smallest possible value after you add parentheses. So the left parentheses must be added to the left of the plus sign and the right parentheses must be added to the right of the plus sign. So then return the expression after adding a pair of parentheses such that the expression evaluates to the smallest possible value, if there are multiple answers that yield the result, we're turning them. Okay, so what this means is like here if I show you guys real quick. So let's say we're given the statement two, four, seven. And then we have plus 38 plus 38. Right. So what we're trying to do is we're trying to place like parentheses around this right. So we're trying to place parentheses around the expressions. And yeah, we're trying to get the smallest possible value after evaluating. So in this case, the output is here, this would be the best optimal place to place the parentheses. Because what happens is that 47 plus 38 would give you 85 and 85 times two. So this gives us 85. And then 85 times two gives us 170. And 170 is the smallest possible value, evaluating this expression, putting the parentheses there. So yeah, so there's other ways to go place parentheses like so let's say I place the parentheses here. So in this case, it would be like 24 times 10 times eight. And that's way greater than 170. So this number is like too large. So what we're trying to do is we're just trying to place parentheses so that we get the smallest possible value. And in that case, it was right next to 47 and 38. So there's some caveats, you cannot place it just next to a number has to be to the left and to the right. So what they're saying here is that we cannot place parentheses like that. Right. We have to have parentheses to the left of the plus sign into the right of the plus sign. Okay. So yeah, 170 is the smallest possible value. So how would you do this problem? So what I did, honestly, I didn't I didn't figure this problem out by myself. I had to look at what other people wrote. But ideally what you would do is you would look at the left side of this expression, we're just going to split into different pairs. So we have 247. Right. So let's just split it between 247. Right. So that's one pair you could have. And you could have 24 and seven. So that's another pair. So now these are like, these are like digits, right? These are like pairs of digits that you could have. And the last one you could have is zero and 247. Right. These are different pairs that you can have, using each of these digits, right, if I just different combinations. And the right side is 38. Right. So let's split 38 into three and eight. And I'm gonna split 38 with zero. Okay. Right. You guys understand. Right. So what you could do is if you look at the observation of we need to have a parentheses on the left side and the right side. So what that means is that essentially is that we could like build, we go through every single combination of each of these pairs with the second number and just place parentheses around them. And then in the end, we just get the smallest number. Okay. So like in this case, if we have 247, three and eight, prepare 247 like this. Right. 247, three and eight. So here we would like add them. And you could just multiply like that. So that would be one way you could put the parentheses. Right. We'd have here like this. Right. So that would be like one way you could put the parentheses. There should be like a one. What we could do is like, when there's a zero, we could actually just replace it with one. Because when you multiply, we don't want to multiply with zero. Right. What really matters is that we're trying to get the left side to like if I place parentheses like this, it would be 247 plus three times eight. So yeah, when the zero, this could be just be like a one because one times 247 plus three times eight would just be one of your answers. Okay. So yeah, there's another way is that so yeah, once we have all the pairs, we could just pair them up and try to get every combination of them and then just evaluate each of them. So let's say we have 247 and 38, we could just put these two here. So then this would be like 247, 38. So then what you would do is you would add both of these pairs inside together and multiply by two and this zero should be a one. So let's change the zero to be a one. Right. And then we do that all that snaz. And then we could keep going, you know, we could just go through every single possible combination we could do for two, seven, 38. It should be like this. We just multiply this, add this, then multiply this, make this max. And that'll be, that'll be an answer. That'll be a good answer. But yeah, okay. So yeah, so how would you do this in code? Simple. So first, so we need to find because they give us a string, we need to find the combinations, not the combination. We need to find the plus sign first because it's a string. And then after we have this plus sign, we could just convert the left side into like a number and the right side into a number. Right. Using some string. So yeah, that's what I do here. So I have integer index, which I find the plus sign. I have two pairs of vectors, which are arrays and their pairs. So for the pairs represents each of these pairs of 024724724738380. All right, those are each pairs of displaying it. Left value string left value item substring from zero to index. And the reason why is because that's going to get 247 here. And our right value is substring from index plus one. So that's substringing after the plus sign. Right. So after the plus sign, I'm going to substring values after this. So that will give me 38. Okay, so that'll be the second number for right value. And then here, what I'm doing is I'm going to loop through each of the values in the left side. So for I equals zero, a left value that size I plus plus. And then what I'm going to do is I'm just going to push back. I'm going to substring between each of the pairs, each of the values here. So if I have 247, I'm going to pick this number to and substring from there. So it would be like, I'll split it between two and then 247. So that'll be one pair. And then the next one, I'm going to split between 24 and seven. So I'll be another pair. And then finally, I'll just split it between like 247 zero. Okay. So yeah, in this case, if s is equal to zero. So what this means is that if the first value is the first value is zero. Right. What I'm going to do is set the second value. The second value is zero. I mean, I split from 247 and zero. So this would mean I would have 247 and zero on the right side. So this left side, you're on the right side. So that's splitting right here. So we zero 247. And I just add, I push back the opposite. So I had zero first and then the first value. The reason why is because I want it to be zero and 247 first, right? I want the zero to be first. So yeah, that's in that case, if the second number is zero, then I just push the opposite of s. And that's going to put the values into the vector pair, the pairs. Otherwise, I just push back first and second and that's going to give me each of these substrings zero 247 247 247. Okay. And I do the exact same thing for the right side. For the right side, I'm not going to put zero first. I'm just going to put 38 first and then zero. Okay. All right, here's the part where it gets a little tricky. I'm going to go through every pair now and just check, which has the smallest value. So to do that, so we have pairs x, x1, y1. So here, if I go here, I have x1. Here's x1. And here's y1. And here's x2. Here's y2. So what I'm doing is I'm doing y1 plus y2. So both of these add and then I multiply it times y2 times x1. Okay, so that's what this is doing. Right, that's evaluating the value is going to equal to that. Here I do max of one and zero. Honestly, I should have just pushed one instead of zero because I would have made more sense. But yeah, when you do max of zero and one, it's just going to give one. So this is in the case if the left side is a zero or the right side is zero, and that's not what I want. I want it to have to evaluate it with one. So that's the evaluation I would want. Right, because I don't want this to be zero because if I pair two, four, seven and three, so I pair these two up, this zero is going to cancel out the whole thing and make it zero. So I multiply it by the max of zero and one, so it gives me one on the left side. So yeah, I do that there. Then what I do is if my V array is my answer array. So if my answer array is empty, so that's like the first iteration, I just set it equal to x1, y1, x2, y2. So that's going to set equal to x1, y1, x2, y2. And that's going to set like the first array that we're checking V equal to that. And then later on as the loop continues, we get the current value, which is literally the same thing. If it's smaller than our current value, if our value is smaller than current value, we set the x1, y1, x2, y2 as a corresponding value. Okay, as a corresponding answer. So if it's less than the current value, so let's say I pair these two up, right? So 247 times 247 plus three times eight, and 247 plus three times max of one. So this is going to be one. Yeah, so the second line of code is just making sure if when I check the next one, 247 plus 38, multiply by two, multiply by one, max of zero and one is just going to be one. I'll compare both of these and find the smallest one, and I'll just reset that equal to that. So that's what this is, because they want us to find the smallest value, right? The smallest possible value. So for that, we have to just check loop through reset our variable to the new answer. The x1, y1, x2, y2 in case if our current value is not right. Yeah, so for this one, it's v of zero, v of one, v of two, v of three, because if we make all these values into an array, so let's say I have 247, 38, I mean 247, 38, right? If we make that into the values of an array, it would be v, right? v is equal to this, and then the result would be one, two, three, zero, one, two, and three, right? That'll be your index. So that's why it's max is v of zero, one times v of one plus v of two times max v of three, one. And here it resets it. Okay, in the end, I need to actually add the parentheses. So I call two string on v of one, which converts the integer to a string. And I add the parentheses in the front and do a plus and then two string of v of two and add parentheses in the back. So if the v of zero is not equal to zero, that means is that the v of zero is not equal to zero. So that's checking if the right side, v of zero is not equal to zero. So that's checking if the left side is zero. All right, so if it's not the last time it's not zero, then we know we need to push back v of zero, which is a two, right? We need to add, we have to put add two to our string. Okay, the v of three is not equal to zero. So v three, not equal to zero. That's v three. Excuse me. This is not equal to zero. And then we're gonna, that means we're on the right side of the middle. So we do plus two string of v three. Okay. And then we just return middle. Okay. So this is like putting boundaries on the thing in case if in case if some issue occurs, there's no zero, right? When there's no zero and when there's no zero, we could put we don't have to put zero there. Right. So this is putting parentheses and not putting zero. Okay. And then we just return middle. If you guys enjoy this video, rate, com, subscribe. I'm really tired. I'll check you guys out.