#Leetcode88 Merge two sorted Arrays in place

2 messages · Page 1 of 1 (latest)

rapid wasp
#

https://leetcode.com/problems/merge-sorted-array/ this is a link to the original problem. The issue I am having is when dealing with m=0 I was expecting to return the other array since the first array has no useful numbers I thought the answer would just be the second array

var merge = function (nums1, m, nums2, n) {
   let outptr = nums1.length - 1;
   let mptr = m - 1;
   let nptr = Math.max(n - 1, 0);
   if (m == 0) return nums2;
   if (n == 0) return nums1;
   while (!(mptr < 0 && nptr < 0)) {
      //console.log(nums1)
      if (mptr < 0) nums1[outptr] = nums2[nptr--];
      else if (nptr < 0) nums1[outptr] = nums1[mptr--];
      else if (nums1[mptr] > nums2[nptr])
         nums1[outptr] = nums1[mptr--];
      else
         nums1[outptr] = nums2[nptr--];
      outptr--;
   }
   return nums1;
}
``` on case 3 which is [[0], 0, [1], 1]; I am not passing the test but I thought that ```js
  if (m == 0) return nums2;
```  would have taken care of that
LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

#

I don't know if the rest of the algorithm I am trying is relevant but the goal was to have 2 pointers starting from right and going to left and comparing them then choosing the highest one to fill in the bigger array that has extra zeroes at the end from right to left until both pointers hit zero meaning the input arrays have no remaining numbers