Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Maximum sum of n consecutive elements of array in JavaScript
In JavaScript, finding the maximum sum of n consecutive elements in an array is a classic sliding window problem. This technique efficiently calculates the maximum sum by maintaining a window of fixed size and sliding it across the array.
Understanding the Problem
The goal is to find a continuous subarray of length n within the given array that has the highest possible sum. For example, given array [1, 2, 4, 7, 3, 5] and n = 3, the consecutive elements [4, 7, 3] have the maximum sum of 14.
The Sliding Window Approach
The sliding window technique maintains a window of size n and slides it across the array. Instead of recalculating the entire sum for each position, we add the new element and subtract the element that falls out of the window.
Algorithm Steps
Step 1: Calculate the sum of the first n elements as the initial window sum.
Step 2: Slide the window by adding the next element and removing the first element of the previous window.
Step 3: Track the maximum sum encountered during the sliding process.
Implementation
function maxSumOfNElements(arr, n) {
// Check if n is valid
if (n > arr.length || n <= 0) {
return null;
}
let maxSum = 0;
let currentSum = 0;
// Calculate sum of first window
for (let i = 0; i < n; i++) {
maxSum += arr[i];
}
currentSum = maxSum;
// Slide the window and update sums
for (let i = n; i < arr.length; i++) {
currentSum += arr[i] - arr[i - n];
maxSum = Math.max(maxSum, currentSum);
}
return maxSum;
}
// Test with different examples
const arr1 = [1, 3, 5, 2, 4, 6, 8];
const n1 = 3;
console.log("Array:", arr1);
console.log("Maximum sum of", n1, "consecutive elements:", maxSumOfNElements(arr1, n1));
const arr2 = [2, 1, 5, 1, 3, 2];
const n2 = 4;
console.log("\nArray:", arr2);
console.log("Maximum sum of", n2, "consecutive elements:", maxSumOfNElements(arr2, n2));
Array: [ 1, 3, 5, 2, 4, 6, 8 ] Maximum sum of 3 consecutive elements: 18 Array: [ 2, 1, 5, 1, 3, 2 ] Maximum sum of 4 consecutive elements: 11
Edge Cases
function testEdgeCases() {
console.log("Edge case 1 - n > array length:");
console.log(maxSumOfNElements([1, 2, 3], 5)); // null
console.log("\nEdge case 2 - n equals array length:");
console.log(maxSumOfNElements([1, 2, 3, 4], 4)); // 10
console.log("\nEdge case 3 - single element:");
console.log(maxSumOfNElements([5], 1)); // 5
}
testEdgeCases();
Edge case 1 - n > array length: null Edge case 2 - n equals array length: 10 Edge case 3 - single element: 5
Complexity Analysis
| Metric | Complexity | Explanation |
|---|---|---|
| Time | O(n) | Single pass through the array |
| Space | O(1) | Only constant extra variables used |
Conclusion
The sliding window technique provides an efficient O(n) solution for finding the maximum sum of n consecutive elements. This approach is optimal for large arrays and avoids the O(n×k) complexity of naive solutions.
