@pakalupapito Hey Pal!!!

The code is actually quite simple but there are next to no comments that’s why you must have not understood but that’s the reason we are here

The answer is a little long but if you read completely, you will understand as I wrote the answer for an absolute beginner to understand and you are 3 star so you will surely get it.

instead of using inbuilt functions, the guy sorted himself but really the code works longer than required. We need the Kth element so we need the list till Kth element only. No real need to get the whole list!!!

we know the two given arrays are sorted so we don’ really to sort them again and we are sure that the element arr1[x]<arr1[y] where x<y.

now we can take d till k instead of m+n because we don’t really need to find full list(because we need to find only one index. If question asked more than 1 index then finding full list will be helpful). Instead of making a list, we will make a integer variable(answer) which stores the last no. of list at that moment.

now in the first while loop we take 3 conditions instead of 2 that i<m, j<n and d<k. now what this loop does is compare elements arr1[i] and arr2[j]. if arr1[j] is smaller than then the answer to arr1[i] and increase i by 1 else arr2[j] and increase j by 1. off course we increase d also by 1.

now if the loop break then it can mean 3 things:

first: value of i reached m. This means first array is completed now and we have to take only arr2 till d=k-1 so we run a while loop which has condition while j<n and d<k and make the answer=arr2[j], increase j and d by 1. if this loop breaks then it means we got answer. We will have this loop no matter what because it won’t run if the first loop broke because of d reaching k. Same will apply to the next loop.

second: value of j reached n. This means second array is completed now and we have to take only arr1 till d=k-1 so we run a while loop which has condition while i<m and d<k and make the answer=arr1[i], increase i and d by 1. if this loop breaks then it means we got answer.

third: value of d reached k. This applies even if loop 2 or 3 ran and broke. This is the end. Hooray!! we got the Kth element of merged the sorted list. print the answer.

Hope you understood now.