 Hello friends. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to explain how it can work through sorted arrays. So I will be telling you how you can write the algorithm for what schools that sorted array input skillet. So first of all, let me implement two arrays. So this is first array. This is second array. So first array is having four elements, one, two, three, four. Second array is having four elements, two, four, six, eight. And they will be merged to third array. So it's nice to be eight, right? So this way we need to merge these two sorted arrays to third array so that third array will be automatically sorted. So for this, I'm going to write a function. So the name of function is merged. It will be receiving A1, A2 and A3 array as input and N1 and N2. So N1 is the size of first array and N2 is the size of second array. Now, inside this, I'm going to bring in three variables i, j and k. So i, j, k variables will be used for pointing the indexes. So i will be for A1, j will be for A2 and k will be for A3, right? So here I'm applying a loop. So where i is initialized with zero, j is zero and k is zero. So all three variables are initialized with zero. Now here I'm going to apply a condition if i is less than N1 and j less than N2. If both the traditions are true, then only this loop is the key and it will be implementing value of k. Now inside this loop, I will compare A1 of five with A2 of j. So if A1 of five values is less than A2 of j, it means we can assign this number because it is smaller. So we can assign this number into A3. So here I'm writing A3 of k equals to A1 of i. And here I'm implementing i, because k is implemented here as a common increment and i is implemented here. So either we need to implement i or we need to implement j based on this condition. And if this condition is false, then A3 of k will be having A2 of j value and here we need to increase j. So this way this loop will be implemented that will be copying the smaller element into A3. So let's say indexes are 0, 1, 2, 3, 0, 1, 2, 3 and 0, 1, 2, 3, 4, 5, 6, 7. So i, j, k, they are 0. Now check this condition. So N1 and N2. So here N1 is 4 and N2 is also 4 because both are having 4 elements. So here we can see I'm using less than. So i is 0 which is less than N1 and j is less than N2 both are true. And in between they have n partition if both are true then this loop will be. So right now both are true. A1 of i is 1. A2 of j is 2. So this condition is true. So A1 of i, it means 1 will be assigned to A3 of k position. And i will be implemented. So i is now 1. This else part will not execute. Then k will be implemented. So k is also 1. Now check these two conditions. So i is 1. 1 is less than 4. It is true. And j is 0 which is also less than 4. So it is true. Now we need to compare this. So A1 of i. So A1 of i is this. And A2 of j is still on this position. So compare both. So 2 is less than 2. So we will come here. So from A2, j position value will be copied to A3 of k position. So from here 2 will be copied. j will be implemented. So j is now 1. And k will be implemented. So k is 2. So this way you saw 1 element from A1 is copied and 1 element from A2 is copied. Then again check these conditions. So both are 2. Both are 1. So both are less than 4. Now check this. A1 of 1 is 2. A2 of j is 4. So both arrays 1, 1 and j are having 2 and 4 as a random. So this time this condition is 2. So A1 value will be copied to A3. So 2 will be copied here. i will be implemented. Then this is false. So k will be implemented. So k is now 2. So this way this is copied. This is copied. It is already copied. So i is 2. So both the conditions are 2. So A1 of i. So A1 of i is this. A2 of j. So j is still on 1. So 1 and j is 4. So again it is 2. So A1 of i position will go to A3 of k. So here 3 will be copied. So it is also copied. Then i will be implemented. So i is 3. Then k will be implemented. So k is now 4. Now 3 position is having 4 and this j is on 1 position. So it is having 4. So again check this. So it is false. So this time A2 of j will be copied to A3. So it will be copied here. j is 2. And k is now 4. Again check these conditions. So these are still 2. A1 of i is having 4. A2 of j is having 2. Because i is 3 and j is 2. So 3 is x is 4. And 2 is x is 6. So again check this condition. It is 2. So from A1 array i position value will be copied here. So it will be copied here. And i will be implemented to 4. And k will be implemented to 6. So this time if we check this condition. So i is 4 which is not less than 4. Because n1 is also 4. So this loop will be terminated. So you can see A1 all elements are copied. But A2's 2 elements are limited. Because they are greater as compared to A1. And this can happen also with A1. If A1 is having greater elements as compared to A2. So some elements will remain into A1. So now we need to implement 2 loops. So that we can copy the many elements of A1. Or A2 into A3 here. So now I am applying Y i less than n1. So if i is less than n1 still. So we can write A3 of A equals to A1 of i. And i plus plus and k plus plus. So if A1 array is having some remaining elements. So they will be copied to A3 as it is. Based on this condition. And right now A2 is having remaining elements. So Y i is less than n2. So A3 of k will be having A2 of j. Then A plus plus and j plus plus. And then you can complete this. So this way your third array is having either remaining elements of A1. Or remaining elements of A2. So in this case the remaining of A2 will be copied as it is. And you can see this A3 is now in sorted form. So this way A3 is having all the elements of A1 and A2 merged in sorted form. So whenever you will be calling this merge function. So if you are calling this merge function into a main function. So you will be passing 3 arrays and 3rd arrays as an output. And this will be updated automatically. So you can print A3 in this method or you can print A3 in main function. So the main function also A3 will be printing this result. That is connected. So this way I hope you understood how we can merge two sorted arrays into third array. So if you want to see or if you want to learn how we can write this algorithm. So you can go to description of this video. You will find links. Link of the playlist where all the algorithms are available related to different topics. Also you can if you go to end of this video you will find links of previous set videos. So follow them. And I hope you understood whatever I explained in this video. Thank you for watching this.