Maximum sum of n consecutive elements of array in JavaScript


In the given problem statement our aim is to find the maximum sum of n consecutive items of array with the help of Javascript functionalities. So for solving this problem we will use basic Javascript functionalities and produce the maximum sum.

Understanding the Problem

The problem at hand is to find the maximum sum of n consecutive items in the array. This process will involve identifying a continuous subarray of length n within the given array which has the highest possible sum. For example suppose we have an array as [1, 2, 4, 7, 3, 5] so that if the value of n is 2 so 2 consecutive items are 4, 7 with a maximum sum of 11.

Logic for the given Problem

To solve the given problem we will use a function to do this task. And the function will initialize two pointers left and right in the beginning of the array. After that we will move the right pointer towards the right position and keep track of the current sum of items within the window. Now we will check the condition if the size of the window is greater than n so we will subtract the value of the item at the left pointer from the current sum. And then we will move the left pointer to the right side. So this way we will maintain the window of size n all the time and update the maximum sum as per the condition.

Algorithm

Step 1: As we have to find the maximum sum of n consecutive items in the array so for doing this task we will create a function and name it as maxSumOfNElements. This function accepts two parameters: first is the array as arr and the second is number n.

Step 2: So inside the above function we will first check that the value of n is greater than the length of the array. If the condition is true then return the value as null because it is not a valid input.

Step 3: After verifying the above condition we will initialize two variables as maxSum and currentSum to zero. The maxSum will store the maximum sum of consecutive elements and currentSum will store the running sum of n consecutive items.

Step 4: As we have declared variables which will be used in these steps. Now we will calculate the initial sum of the first n items in the array and save it in the maxSum and currentSum variables.

Step 5: This step will iterate over the array with the help of for loop and beginning from the index n.

Step 6: In the loop we will add the current item and subtract the item that is n positions behind from the currentSum.

Step 7: Now we will update the maxSum with the maximum value between the maxSum and currentSum.

Step 8: After finishing the loop we will return the maximum sum of n consecutive items in the array.

Example

//Function to find the maximum sum
function maxSumOfNElements(arr, n) {
   if (n > arr.length) {
      return null; // Invalid input, as n is larger than array size
   }

   let maxSum = 0;
   let currentSum = 0;

   for (let i = 0; i < n; i++) {
      maxSum += arr[i];
   }
   currentSum = maxSum;

   for (let i = n; i < arr.length; i++) {
      currentSum += arr[i] - arr[i - n];
      maxSum = Math.max(maxSum, currentSum);
   }

   return maxSum;
}

const arr = [1, 3, 5, 2, 4, 6, 8];
const n = 3;
const result = maxSumOfNElements(arr, n);
console.log("Maximum sum of", n, "consecutive items:", result);

Output

Maximum sum of 3 consecutive items: 18

Complexity

The time complexity for calculating the maximum sum of n consecutive items present in the array is O(n), in which n is the length of the array. Because we have iterated over the array only once. The space complexity for the above code is O(1) as we have only used a constant amount of space to store the variables.

Conclusion

We have effectively found the maximum sum of n consecutive items in the array. This approach is used to solve the problem with linear time complexity which is an efficient solution for large size arrays.

Updated on: 16-Aug-2023

539 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements