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

179
Visualizações
Optimizing Sums of Parts function for Code Wars

I am looking at the Sums of Parts problem on CodeWars:

Let us consider this example (array written in general format):

ls = [0, 1, 3, 6, 10]

Its following parts:

ls = [0, 1, 3, 6, 10]
ls = [1, 3, 6, 10]
ls = [3, 6, 10]
ls = [6, 10]
ls = [10]
ls = []

The corresponding sums are (put together in a list): [20, 20, 19, 16, 10, 0]

The function parts_sums (or its variants in other languages) will take as parameter a list ls and return a list of the sums of its parts as defined above.

The goal of the function is to sum elements of array then shift the first element of array each time until the length of array becomes 0.

I have this solution for it:

function partsSums(ls) {
    let len = ls.length;
    let arr = [];
    for (let i = 0; i < len +1; i++) {
        arr.push(summation(ls));
        ls.shift();
    }

    function summation(a) {
        let sum = 0;
        for (let i = 0; i < a.length; i++) {
            sum += a[i];
        }
        return sum;
    }
    return arr;
}

It works when I run it in my editor: all test cases on CodeWars that successfully complete pass, but when I try to submit, I get this error:

Process was terminated. It took longer than 12000ms to complete

I'm new to algorithms and can't understand where the error is? Any suggestions are welcome.

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

0

The goal of the function is to sum elements of array then shift the first element of array each time until the length of array become 0

The code challenge actually doesn't speak of shifting. You can do this without shifting, by storing the values immediately at the right index in the result array. Also, your function summation is summing some of the same values repeatedly. This can be avoided.

Take this example:

ls = [0, 1, 3, 6, 10]

The output can be constructed as follows:

  • Create an array that is one element longer, and has the value 0 at the end:

    ls:     0  1  3  6 10
    result: .  .  .  .  .  0
    

    (The values at the points are not relevant at this point)

  • Then start from the right side, and create a (backwards) running sum:

    ls:     0  1  3  6 10
    result: ↓  ↓  ↓  ↓  ↓← 0
            ↓  ↓  ↓  ↓←10
            ↓  ↓  ↓←16
            ↓  ↓←19
            ↓←20
           20
    

    So the previous result at index i+1 is added to the input at index i, and so it works backwards to the start of the array.

Here is an implementation:

function partsSums(ls) {
    let result = Array(ls.length + 1).fill(0);
    for (let i = ls.length - 1; i >= 0; i--) {
        result[i] = ls[i] + result[i + 1];
    }
    return result;    
}
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