Home > other >  array is not getting modified with method but after making its length 0 and use push method it gets
array is not getting modified with method but after making its length 0 and use push method it gets

Time:04-28

Below is the leetcode question: "problem is when use sort concat any other method on nums1, it is bot modifying nums1 unless I'm emptying it and pushing new data. Below code is working but what if I dont want to create nums11 and perform every method on nums1."

Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.

Merge nums1 and nums2 into a single array sorted in non-decreasing order.

The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.

var merge = function(nums1, m, nums2, n) {
    let nums11 = [];
        
  for(let i = 0; i<m; i  ){
      nums11.push(nums1[i])
  }


    nums1.length = 0;
     nums11 = nums11.concat(nums2);
   nums11.sort((a,b) => { return a-b});
  for(let i = 0; i<nums11.length;i  ){
      nums1.push(nums11[i])
  }
 
};

CodePudding user response:

You can maybe try something like this.

for(let i = 0; i<n ; i  ){
   nums1[i m-1] = nums2[i];  // adding nums2 elements to the elements with value 0 in nums1 
}

nums1.sort((a,b) => {return a-b});
return nums1; 

As for the reason it's not getting modified, it could be because of the constraints in the question. It won't let you add more elements to nums1 because it is already the size it needs to be for the solution to fit in it.

CodePudding user response:

If you check the for loop carefully we are pushing the elements into the nums1 array. Remove the line nums1.length=0; and instead of pushing elements into the nums1. Try to change the element on ith index. That will solve the issue. Like this

nums1[i]=(nums11[i])

This is the complete code.

function merge(nums1, m, nums2) {
    let nums11 = [];
        
  for(let i = 0; i<m; i  ){
      nums11.push(nums1[i])
  }

     nums11 = nums11.concat(nums2);
   nums11.sort((a,b) => { return a-b});
  for(let i = 0; i<nums11.length;i  ){
      nums1[i]=(nums11[i])
  }

};

The code in the question makes the length of num1 array to 12.

I hope it will solve the issue.

CodePudding user response:

The requirement of the question is to modify the array in-place and also not use a built sorting method.

Consider the following approach:

  • Have two pointers i and j that point to the last index (excluding the empty space) of the arrays nums1 and nums2 respectively.
  • Have a pointer k that points the last index of nums1, this is where we would insert values during the iteration.
  • Iterate until all elements of the nums2 array have been exhausted.
  • Put the largest of the two elements at the kth index.

function merge(nums1, m, nums2, n) {
  let i = m - 1,
    j = n - 1,
    k = m   n - 1;

  while (j >= 0) {
    if (nums1[i] >= nums2[j]) {
      nums1[k] = nums1[i];
      i -= 1;
    } else {
      nums1[k] = nums2[j];
      j -= 1;
    }
    k -= 1;
  }
}

const nums1 = [1, 4, 8, 11, 0, 0, 0, 0, 0, 0];
const nums2 = [2, 3, 4, 7, 8, 10];
merge(nums1, nums1.length - nums2.length, nums2, nums2.length);
console.log(nums1);

  • Related