Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
array is not getting modified with method but after making its length 0 and use push method it gets modified why

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])
  }
 
};
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

0

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);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda