Introduction
When developers begin learning Data Structures and Algorithms (DSA), they often encounter problems that seem easy at first glance but teach important problem-solving techniques underneath.
One such problem is:
Find the largest sum of N consecutive elements in an array.
This question appears frequently in:
JavaScript interviews
Frontend developer assessments
DSA practice platforms
Coding bootcamps
Technical screening rounds
At first, many developers solve it using nested loops.
The solution works.
The answer is correct.
But as the array grows larger, performance becomes a concern.
This problem is also a perfect introduction to one of the most powerful DSA concepts:
The Sliding Window Technique.
Before learning the optimized solution, let's first understand the problem itself.
Understanding the Problem
Suppose we have the following array:
const arr = [1, 2, 3, 4, 3, 5, 4, 6, 7, 8];
And we are given:
n = 4;
Our goal is to find the largest sum of 4 consecutive numbers.
Notice the keyword:
Consecutive
This means the numbers must appear next to each other in the array.
We cannot randomly choose values.
For example:
Valid groups:
[1,2,3,4]
[2,3,4,3]
[3,4,3,5]
[4,3,5,4]
[3,5,4,6]
[5,4,6,7]
[4,6,7,8]
Invalid group:
[1,4,6,8]
These numbers are not consecutive.
Real-Life Example
Imagine a retail company tracking sales.
Daily sales data:
Day 1 → 1
Day 2 → 2
Day 3 → 3
Day 4 → 4
Day 5 → 3
Day 6 → 5
Day 7 → 4
Day 8 → 6
Day 9 → 7
Day 10 → 8
Management asks:
Which 4-day period generated the highest sales?
Now this problem suddenly feels practical.
Instead of numbers, we are analyzing business performance.
The same logic can be used for:
Sales analysis
Website traffic
Temperature readings
Stock market trends
Customer visits
Revenue tracking
Brute Force Solution
Let's start with the straightforward approach.
function sol(arr, n) {
if (n > arr.length) {
return "num should be less than arr.length";
}
let result = 0;
for (let i = 0; i < arr.length - n + 1; i++) {
let tmp = 0;
for (let j = 0; j < n; j++) {
tmp += arr[i + j];
}
if (tmp > result) {
result = tmp;
}
}
return result;
}
let result2 = sol(
[1, 2, 3, 4, 3, 5, 4, 6, 7, 8],
4
);
console.log(result2);
Output:
25
The largest sum comes from:
[4, 6, 7, 8]
Calculation:
4 + 6 + 7 + 8 = 25
Step 1: Validate Input
The first thing our function does is check:
if(n > arr.length)
Why?
Imagine:
arr = [1,2,3];
n = 5;
How can we find 5 consecutive numbers when only 3 numbers exist?
Impossible.
Therefore:
return "num should be less than arr.length";
This prevents invalid calculations.
Step 2: Create Result Variable
let result = 0;
This variable stores the highest sum found so far.
Think of it as the current champion.
Whenever a larger sum appears, the champion gets replaced.
Step 3: Outer Loop
for(
let i = 0;
i < arr.length - n + 1;
i++
)
This loop controls the starting position.
Let's visualize:
Array:
[1,2,3,4,3,5,4,6,7,8]
Window Size = 4
Iteration 1:
[1,2,3,4]
Iteration 2:
[2,3,4,3]
Iteration 3:
[3,4,3,5]
And so on.
The outer loop moves the window across the array.
Step 4: Temporary Sum
let tmp = 0;
Every new window needs a fresh sum.
This variable accumulates the total for the current group.
Step 5: Inner Loop
for(let j = 0; j < n; j++)
This loop calculates the current window's sum.
Example:
Window:
[1,2,3,4]
Process:
tmp = 1
tmp = 3
tmp = 6
tmp = 10
Final:
10
Step 6: Update Maximum Value
if(tmp > result){
result = tmp;
}
Suppose:
Current Maximum:
15
New Sum:
20
Since:
20 > 15
Update:
result = 20
This ensures we always keep track of the largest value discovered.
Visual Walkthrough
Let's calculate manually.
Window 1:
[1,2,3,4]
Sum:
10
Window 2:
[2,3,4,3]
Sum:
12
Window 3:
[3,4,3,5]
Sum:
15
Window 4:
[4,3,5,4]
Sum:
16
Window 5:
[3,5,4,6]
Sum:
18
Window 6:
[5,4,6,7]
Sum:
22
Window 7:
[4,6,7,8]
Sum:
25
Largest Sum:
25
The Performance Problem
Although the solution works, it performs unnecessary calculations.
Notice:
Window 1:
1 + 2 + 3 + 4
Window 2:
2 + 3 + 4 + 3
We are recalculating values we already know.
This becomes expensive for:
10,000 elements
50,000 elements
100,000 elements
This is where the Sliding Window Technique helps.
Optimized Sliding Window Solution
function maxSum(arr, n) {
if(n > arr.length){
return null;
}
let maxSum = 0;
for(let i = 0; i < n; i++){
maxSum += arr[i];
}
let tempSum = maxSum;
for(let i = n; i < arr.length; i++){
tempSum =
tempSum
- arr[i - n]
+ arr[i];
maxSum = Math.max(
maxSum,
tempSum
);
}
return maxSum;
}
Why Is It Called Sliding Window?
Imagine a window moving across the array.
Instead of recalculating everything:
Remove:
Left element
Add:
New right element
Example:
Current Window:
[1,2,3,4]
Sum:
10
Move right:
Remove:
1
Add:
3
New Sum:
10 - 1 + 3 = 12
Much faster.
Much smarter.
Time Complexity Comparison
Brute Force:
O(n²)
Sliding Window:
O(n)
For large datasets, this difference is enormous.
Common Interview Questions
Why is Sliding Window better?
Because it avoids repeated calculations.
When should Sliding Window be used?
Whenever the problem involves:
Consecutive elements
Continuous subarrays
Fixed-size windows
Is the brute force solution wrong?
No.
It is correct.
It is simply less efficient.
Common Beginner Mistakes
Mistake 1
Confusing:
Largest Sum
with
Largest Element
These are different problems.
Mistake 2
Forgetting:
arr.length - n + 1
This causes out-of-bounds access.
Mistake 3
Not handling invalid values of n.
Always validate inputs first.
Final Thoughts
This problem may look simple, but it teaches one of the most valuable DSA patterns used in real-world development and coding interviews.
The brute-force approach helps beginners understand the logic.
The sliding window approach teaches optimization.
Learning both is important because great developers don't just make code work—they learn how to make it work efficiently.
If you're preparing for JavaScript interviews, mastering this problem and the Sliding Window pattern will help you solve many similar challenges in the future.
Sol:)

0 Comments