 What's up guys, my is Michael and welcome to my YouTube channel today. We're gonna do another leecho challenge called smallest range one Given an array of numbers We're gonna choose any X with between negative K and K Inclusive and add X to Whatever for each integer inside this array. That's what we're doing Then after this process, we have another array B. We're gonna return the smallest difference between the maximum value B and the minimum value B Okay, so first of all, let's think about this first We should get the Smallest possible value and the large possible value reason why is because we want to return the smallest possible difference between the maximum value B and the Minimum value B right so regardless we still need to get the maximum The largest value and the smallest value because we got to subtract that right so what we're gonna do first is we're gonna Create an integer called the smallest and have this equal to the first value and The largest is not gonna equal to the first value Then we're gonna loop through our array and Then compare each Value to The smallest and largest so what we're gonna do is not compare we're going to do smallest is gonna equal to the minimum of my current value at my array and the current smallest value and Largest is going to equal to the maximum of Whatever my current value is and largest So this is gonna get me my smallest value and my largest value Now we got to think about this Essentially is we want to return the smallest possible difference between the maximum value B and the minimum value B Okay, so right now we have the smallest and largest value of a okay, so a is the So if I draw a diagram here as you can see here Let's say I have a number line and then this is my smallest Small and This is my largest value and basically what we want to do is we want to essentially Mean return the smallest possible difference between the maximum value of B and the minimum value B So the smallest but so we want the difference between here and here Do you guys get it? Okay So this is the difference between a right now currently this is the difference between if I just take the largest minus Smallest this is the difference in a but what we did here is that to get to be we had to add choose any integer X with between negative K and K and added to each value of a so now essentially is We want to return the smallest if possible difference between the maximum value of B and the minimum value B So how do I do that? Well the smallest difference is is actually if you because we added a X value to a of I for each value and X is between negative K and K essentially as if you Take smallest and you add it with this K value and You take largest and you just subtract by this K value. Let's say take largest minus K This value is going to be the smallest difference from here to here is going to be the smallest difference You guys understand me So if I take this is going to be largest minus K, so I'll just do large Minus K And that's this value and this value is going to be small plus K and that would be the smallest difference because if you think about it if you're if you're just if you want to get the Smallest possible difference between the maximum value B and the minimum value B That would be that would essentially be it because that this would be the smallest difference We subtract this value largest minus K and smallest plus K that would be the diff smallest difference because essentially is what we did was We added whatever K value X to which is between a of K and K to every value into the array and The best possible thing is to add K here to the smallest and subtract K from the largest because that would because the X the X Is the lower bound is negative K X is the upper bound is K. So if we were to add Plus K, this would be them the maximum possible Value could add to here and this would be the minimum between here So then the distance would be as small as possible. So yeah, that's what we're going to do here so once you have the smallest and largest we're going to take smallest and Plus equal to K and largest is going to minus equal to K Then after that we are going to return the minimum of zero because we have to return minimum the reason why because Absolute absolute value issues right largest minus smallest if it's zero then yeah We want to have the absolute value to be This essentially we need minimum because otherwise You could have negative distances and that's not good. Okay, so then this would be if it's negative. We would just return zero Okay, we submit it Okay, I got a wrong answer. What was the issue? What is my issue is? Did you do smallest minus equal K largest minus largest minus small? Oh wait? Max I think actually let me see. No, I'm pretty sure it's me. Yeah. Okay. It's max the reason why it's max is because of if it's negative We don't want the negative we want to compare zero and the Difference between it because if it's negative we want it to be positive essentially So that's why we do max because we want the positive value if it's negative then we should just return zero So that's why that's why it's Max yeah, that's how you do it rate com subscribe. I'll check you guys later I hope you enjoyed this video is a short video. But yeah, peace