 What's up guys, my name is Michael Lynn. Welcome to my YouTube channel. Today we're going to do squares of a sorted array challenge, another encode grind challenge, okay? So given an array of integers a sorted in a non-decreasing order return an array of squares of each number also in sorted non-decreasing order. So you're given an array of numbers and we just want to return that is already sorted. We want to square each number in this array and then we want to return that array in sorted order, all right? I basically explained how to do this. So please try this yourself, come back to me. There's a way that I just said there's an easiest way to do it or there's an even more better way to do it but it's way more complicated and revolves around going through it once but try to try to get the basic way down and then try to optimize it but yeah, I'll give you some time to do this and then come back to me when you got it. All right guys I'm going to explain how to do this the most basic way to do this. First we're going to create our array to return, I'll call it to return. Next we're going to go through our array of values of a. We're going to go through every value in a and we're going to basically square each value in square every value in a and then add it to our to return array. Now we just have to sort this. So how do we do that? We do, I think it's just sort to return dot begin and to return dot end and that's going to sort the array in the end and then we're going to return to return. Let's submit this and see if we get accepted and it gets accepted. So yeah that's the basic way to do it. Now I'm going to explain the much the more efficient way of doing this because we had to go through the array twice basically we first had to go through at every single square every single value added to our new array and then we had to sort it which this required o of n when I say o of n it means linear, linear time it took a linear time we had to go through every single value and then sorting this is using quick sort we required n log n so that takes time also. So there's actually a way to do this in o of n time and I'll explain that now. Alright guys so I'm going to explain the better way to do this. So we know that the array a originally was sorted right so now the key issue is that when you square it you want to maintain the sorted order right so because we know it's sorted we know that values sorted array and values will have negative numbers and positive numbers right and negative numbers generally are like let's say in this case negative 3 negative 2 negative 1 so if you were to square all these values negative 3 square you get 9 negative 2 square you get 4 negative 1 square you get 1 if you were to square all these values their their order is going to go the square of the values are is going to go from right to left right do you get it because originally negative 3 if I square it and negative 2 I square negative 3 originally it's negative 3 negative 2 negative 1 and this is an increasing order if I square all these values the square of the values it's going to go decreasing order 9 4 and 1 and if for me to maintain this order all I have to do is go from right to left for the negative values now we need to have the pot now for the positive values in this case four five and six if you square them they're still going to go from smallest largest right like four five six square it you get sixteen twenty five thirty six they're still going to go from smallest largest so now our strategy is basically to go through the negative numbers but go from right to left and then merge that with the positive numbers from left to right so in order that we need to use two pointers to read the positive and negative values of the array one pointer is going to be the positive direction the other pointer i's going to be in the negative direction so I'll code this with you guys and explain the solution so first this is going to be the length of the array and okay j is going to go from j is going to go from the j is going to go in the positive direction so this is going to go from the positive direction from left to right four five and six this is going to go for positive direction okay now first I need to go through the array the original array and get my my index where I'm starting at for the positive values so in order that I am going to every time I see a negative value I'm going to increment my position in this array this is what it's doing I'm going to start at zero and every time I see a negative value right in this array of a I'm going to increase my position this will make sure that once this condition is finished I will be at the first starting position of a positive value okay so in this case negative three negative two negative one four five six this is going to keep going until once I hit a positive value which is four so this j is going to this j is going to start at four right because I'm going to j is my positive value I'm going to go from left to right from my positive value okay so this will get me that now I is going negative direction so in this case I is going to start from negative one negative two and it's going to start from negative one okay and it's going to go from negative one to negative two negative three so that's why I is going to equal to j minus one I is going to be the first the last negative value that we see in our sorted array okay so that's why I is equal to j minus one so in this case j will start at four I is going to start at negative one in this array that we see okay now I'm going to create my new array my new array is going to call to return and it's going to have the same size as my original array okay now this is going to be the tricky part I need to maintain an index of my new position of my the values I'm adding into my new array so that's what they're doing here technically but we don't need to do that because we could just use pushback in c++ uh honestly but let's do it anyway let's do it anyway just to just to just to make sure that you guys understand the solution so uh the t is going to be the current index that I'm going to add to my new array right so while I is greater than or equal to zero and j is less than n what does this mean this means is that while my index of my first starting position this negative one or whatever my first starting position of the the last negative number right while it's greater than or equal to zero I'm going to go this way my eyes are going to go this way we're going to iterate this way okay this is where the negative values and then we're going to square each of these values and we're going to merge it with all the values of going this way going right for j so j is j is going to the positive direction i is going to the negative direction going this way okay so while this is occurring I need a while loop that checks while my i is going this way my j is also going this way until my j reaches the end and my i reaches the zero okay this is what this while loop is for okay now what do I have to check in this while loop I have to check if my current my I'm going to first I'm going to square square whatever value I have at my i and my j what does this mean this means is that if the negative value that I'm having negative value I'm going through like like okay let's say i is negative one right if the square of this negative value is less than the square of my positive value which is in this case four right negative one square is one four square is sixteen if it's less than it what am I going to do I am going to add my add this into my array because I want to maintain it's sorted right so I'm going to add the square of the value of the smaller one into my array okay then I need to increase the index of the index that I'm currently at in my array so that this maintains the position of the value I'm currently adding so I need to increase this index this is post-incrementation post-incrementation means I'm going to add it after I'm going to increase my index index afterward so this is actually essentially the same thing as doing t and then t plus plus okay but they want to make it more fancy so they're going to do t plus plus in here so essentially this is saying to return at my current index in my array at t I'm going to set it equal to the lower value of the negative value of a of i times a of i okay and I'm going to increase my index t plus plus if it's not now if it isn't then I then I want to add the positive value squared okay do you guys understand so if the negative value squared is less than the positive value squared I want to add my negative value into my array if the positive value value squared is greater than the negative value squared I'm going to add the positive value squared into my array reason for this is because I want to maintain this array my to return to be sorted okay I want the to return to be sorted okay all right so now once we add these values I have to decrease my index that I'm currently at okay so once once I add this right once I add my negative value negative one squared I want to decrease my index of i so I'm going to go from negative I'm going to go to this one instead now because how else you want to decrease your your you want to decrease we are going this way our i value we are going the i index represents the negative values okay and it's going the negative value is going this way we are going this way iterating this way for the i values and the j values are going this way for the positive values okay so once if I'm adding the negative value negative one squared I am going to go decrease it decrease my i value so I'm going to go to negative next so I'm going to decrease my index for that one if I'm adding the j the positive value four squared right emerging the positive value because we're checking if this it's squared the value squared right let's say four squared which is 16 16 is greater than negative one uh the 16 is greater than one right because we're doing negative one squared then we need to increase the j value and go to the next number so we're increasing the j index okay now now once this is finished oops my bad once this is finished there we still have to check there are some corner cases that might occur in this situation when we're merging these two arrays let's say that one of them that one value is already one array nor emerging is already done okay one is already done let's say the j let's say that the the let's let's say in this case that I'm merging negative one and four five and six right and I once I added I checked negative one squared one is less than four squares 16 right then this is done this is going to be done the side of the array is done right but what else what are what are we going to do I have to add the rest of the array in this case okay sometimes there's some remaining values of our array that we need to concatenate into the end so that's why we have this this this while loop this while loop will add them remaining values of the right side and then we also need this while loop this uh this the this while i is greater than or equal to zero this while loop adds the remaining values on the left side okay so these are this will help maintain the corner cases in which case we're only adding like one value or we merged uh just like two values right and we need the rest of the array otherwise this this this while it would only stop in this case of when one is already added like these two are already added then then what what's going to happen now right like it's done it's done the left and right side are done we need to add the rest of the array okay so that's why we have this while loop this while loop i is greater than equal to zero will add the remaining values of to our two return array okay and then I'm going to decrease my i value this will make sure that I'm adding the remaining values and this is going to make sure that I'm adding the remaining values of my jr a and essentially this is all it takes this is basically merge sort because we're concatenating two different arrays this is essentially how merge sort works so I might put it like a hashtag merge merge sort hashtag merge sort okay hashtag merge sort okay and then we're returning it and basically let's submit it and it got accepted so yeah that's basically how you would do this question rate com subscribe let me know what let me know what type of content you want me to put out next