Home > database >  A sum of consecutive numbers in array
A sum of consecutive numbers in array

Time:11-13

one more time i need help of community. There is this code. I underrstand pretty everything but not the ending. I am counting on you. So we have a function where we add an indicated elements to each other

  function array_max_consecutive_sum(nums, k) {
    let result = 0;
    let temp_sum = 0;
    // veriable where we collects results
    for (var i = 0; i < k - 1; i  ) {
        // first loop where we go through elements but it is limited to value of k
        // result
        temp_sum  = nums[i];
        for (var i = k - 1; i < nums.length; i  ) {
            // the second loop but this time we start from position where we had finished
            temp_sum  = nums[i];
        }
        // condiition statement which overwrites
        if (temp_sum > result) {
            result = temp_sum;
        }
        // How should i analyze this line of code. Could you simplify it for me? We have a veriable, from which we will remove, what to be specific? Another question is why we have to use "1" in this operation? 
        temp_sum -= nums[i - k   1];
    }
    return result;
  }
        
  console.log(array_max_consecutive_sum([1, 2, 3, 14, 5], 3))

CodePudding user response:

I'm not convinced there aren't bugs in that code. The inner loop needs to only be executed once. temp_sum needs to be incremented with nums[i] and decremented with nums[i-k 1] before evaluating if (temp_sum > result).

This line:

temp_sum -= nums[i - k   1];

Is apparently decrementing the running summation by excluding the last element of the previously evaluated subset. But it needs to be doing this before the if (temp_sum > result) statement.

I rewrote the implementation to something that I think is cleaner, faster, and more correct.

function array_max_consecutive_sum(nums, k) {

    if ((nums.length < k) || (k <= 0)) {
        return 0;
    }
    
    let result = 0;
    let temp_sum = 0;

    // iterations is the number of sub arrays of length k to evalaute
    let iterations = nums.length - k   1;

    // do first iteration where we sum up nums[0] up to and including nums[k-1]
    for (let i = 0; i < k; i  ) {
        temp_sum  = nums[i];
    }
    result = temp_sum;

    let start = 0;
    iterations--; // we just completed the first iteration

    // now evaluate each subset by subtracting the first item
    // from the left and adding in a new item onto the right
    for (let i = 0; i < iterations; i  ) {

        temp_sum -= nums[start];    // remove the first element of the previous set
        temp_sum  = nums[start k];  // add the last element of the new set
        start  ;

        // evaluate this subset sum
        if (temp_sum > result) {
            result = temp_sum;
        }
    }

    return result;
}

CodePudding user response:

This one-liner should also do the job. I still don't understand the role of parameter k, so my solution works without it.

const arr=[5,1,2,3,7,8,9,4,5,6,10,1],
maxListSum=Math.max(...arr.reduce((l,c,i,a)=>{
if (!i||c==a[i-1] 1)
 l[l.length-1] =c
  else if(i) l.push(c)
  return l
},[0]))

console.log(maxListSum)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related