You need to write an algorithm for finding the minimum sum of a continuous sequence of n numbers in an array of size m Example: arr=[5, 7, 2, 3, 5, 12, 1, 1, 5](m=9) with n=2, the answer is[1 , 1] with n=3 answer -[1, 1, 5].
There are options?
  • with n=3 answer -[2, 3, 5]

    What is it like?
    – Muddy21 Aug 21 '18 at 21:21
  • Amazon Pinball, there is the same[1, 1, 5] – Muddy21 Aug 21 '18 at 21:29
  • Muddy21, corrected – Tense Tapir Aug 21 '18 at 21:29
  • Muddy21, but understood – Amazon Pinball Aug 21 '18 at 21:29

1 Answers 1

function getMinSum(arr, n) {
  var
    sum=0,
    minSum=0,
    index=0;

  for(var i=0;i<n;i ++) {
    sum +=arr[i];
  }
  minSum=sum;

  for(var i=n;i<arr.length;i ++) {
    sum=sum - arr[i - n] + arr[i];
    if(sum<minSum) {
      minSum=sum;
      index=i - n + 1;
    }
  }

  return {
    sum: minSum,
    seq: arr.slice(index, index + n)
  };
}
  • The66, figured out. – Clear Cod Aug 21 '18 at 22:50