Sliding Window
Reuse the previous window's answer instead of recomputing it.
A sliding window keeps a running answer for a contiguous range. To move the window one step right you add the element entering and subtract the element leaving — the interior is never touched again, so each step is O(1) instead of O(k).
It turns 'best sum of any k consecutive elements' from O(n·k) into O(n), and the variable-size version (grow while valid, shrink while invalid) solves longest-substring and minimum-window problems the same way.
The single line `sum += a[i] - a[i - k]` is doing two things at once. AlgoLens shows the window boundaries on the array as it slides, so that line stops being a trick and becomes obvious.
The code
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> a = {2, 1, 5, 1, 3, 2};
int k = 3;
int sum = 0, best = 0;
for (int i = 0; i < k; i++) sum += a[i];
best = sum;
for (int i = k; i < (int)a.size(); i++) {
sum += a[i] - a[i - k];
best = max(best, sum);
}
cout << best << '\n';
return 0;
}
Now run your own Sliding Window
AlgoLens traces your code — arrays, graphs, trees and recursion — from a real execution, so you see how it actually behaves, not a canned animation.
Paste your codeKeep going
Binary Search
Halve the search space every step — find a value in a sorted array in O(log n).
Linear Search
Check every element until you find it — the baseline every other search beats.
Two Pointers
Close in from both ends of a sorted array — O(n) instead of O(n²).
Bubble Sort
The simplest sort — watch the largest value bubble to the end each pass.