DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Maximum Average Subarray I

var findMaxAverage = function (nums, k) {
  let start = 0;
  let max = 0;
  let sum = 0;
  let averageBy = k;

  for (let i = 0; i < k; i++) {
    sum = sum + nums[i];
  }
  max = sum / averageBy;

  for (let i = k; k < nums.length; k++) {
    sum = sum + nums[k] - nums[start];
    start++;
    max = Math.max(max, sum / averageBy);
  }

  return max;
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

We want your help! Become a Tag Moderator.
Check out this survey and help us moderate our community by becoming a tag moderator here at DEV.