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

220
Visualizações
Merge Sorted Array leetcode
var merge = function(nums1, m, nums2, n) {

    //contcating two array
     let array = nums2.concat(nums1)
    // sort the array
     array.sort((a,b) => a-b)
     // remove element > m+n length
 return array.slice(m+n-n)
};

This ^ function is returning -> [1,2,3,0,0,0] if i'm applying console then answer is same as expected -> [1,2,2,3,5,6]

Why is this happening ?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Remove slice function from the end of the function. slice(m+n-n) slices your sorted array and returns array from index m+1 to the last index.

var merge = function(nums1, m, nums2, n) {

    //contcating two array
     let array = nums2.concat(nums1)
    // sort the array
     array.sort((a,b) => a-b)
     // remove element > m+n length
 return array.slice(m+n-n);
};

console.log(merge([2,4,8,9],4,[0,4,6,9],4));

You can use the following function to merge and then sort the two arrays. Time complexity of this approach is O(nlogn)

function merge(arr1,arr2){
  return [...arr1,...arr2].sort();
}

console.log(merge([4,8,6],[1,3,9,10]));

The second approach runs in O(n) time.

 function merge(arr1,m,arr2,n){
          let result = [];
          let i=0 , j = 0 ;
          while(i<m && j<n){
            if(arr1[i]<arr2[j]){
              result.push(arr1[i]);
              i++;
            }else{
              result.push(arr2[j]);
              j++;
            }
          }
          while(i<m){
            result.push(arr1[i]);
            i++;
          }
          while(j<n){
            result.push(arr2[j]);
            j++;
          }
          return result;
     }

        console.log(merge([4,5,6],3,[1,3,8,10],4));

about 4 years ago · Juan Pablo Isaza Relatório

0

I did not see any problem with your code and it works, I just added another code. I think the problem with your console.log.

var merge = function(nums1, m, nums2, n) {

    //contcating two array
     let array = nums2.concat(nums1)
    // sort the array
     array.sort((a,b) => a-b)
     // remove element > m+n length
 return array.slice(m+n-n)
};

console.log(merge([1,2,3,0,0,0],3,[2,5,6],3));

var merge = function(nums1, m, nums2, n) {
    return    nums1.slice(0, m)
    .concat(nums2.slice(0, n))
    .sort((i, j) => i - j);
       
       
    };

  console.log(  merge([1,2,3,0,0,0],3,[2,5,6],3))

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