A sliding window is a contiguous range that moves along the array. For a fixed-size window, update the running sum by adding the entering element and subtracting the leaving one, turning O(n x k) into O(n). For a variable-size window, grow the right end until a condition holds, then shrink the left end while it still holds, recording the best length.
Remember
window += a[i] - a[i - k]; /* fixed window slides by one */
for (r = 0; r < n; r++) { /* variable window */
sum += a[r];
while (sum >= target) { best = min(best, r - l + 1); sum -= a[l++]; }
}
Reference
Cheat Sheet
Keep these facts handy while you work with Sliding Window Technique in C in C.
Topic
Detail
Fixed window
Add entering, subtract leaving
Variable window
Grow right, shrink left
Time
O(n)
Works with
Contiguous subarrays and substrings
Code
C Examples
Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Interview Programs.
Example 1 — Maximum sum of 3 consecutive elements
Array: 2, 1, 5, 1, 3, 2.
C
#include <stdio.h>
int main(void) {
int a[] = {2, 1, 5, 1, 3, 2};
int n = 6, k = 3;
int window = 0;
for (int i = 0; i < k; i++) window += a[i];
int best = window, start = 0;
for (int i = k; i < n; i++) {
window += a[i] - a[i - k];
if (window > best) {
best = window;
start = i - k + 1;
}
}
printf("max sum of %d consecutive = %d (starting at index %d)\n", k, best, start);
return 0;
}
Output
max sum of 3 consecutive = 9 (starting at index 2)
How It Works
The first window (2, 1, 5) sums to 8.
Sliding adds the new element and removes the old one: 7, then 9, then 6.
The best window starts at index 2 (5, 1, 3).
Example 2 — Shortest subarray with sum at least 7
Array: 2, 3, 1, 2, 4, 3.
C
#include <stdio.h>
int main(void) {
int a[] = {2, 3, 1, 2, 4, 3};
int n = 6, target = 7, l = 0, sum = 0, best = n + 1;
for (int r = 0; r < n; r++) {
sum += a[r];
while (sum >= target) {
if (r - l + 1 < best) best = r - l + 1;
sum -= a[l++];
}
}
printf("shortest subarray with sum >= %d: length %d\n", target, best);
return 0;
}
Output
shortest subarray with sum >= 7: length 2
How It Works
The right end grows until the sum reaches 7 or more.
Then the left end shrinks while the sum still qualifies, recording the smallest length.
The best window is (4, 3), length 2.
Edge Cases & Pitfalls
Check these before relying on Sliding Window Technique in C in real programs.
neg
Negative numbers
Variable windows that rely on a growing sum assume non-negative values.
empty
Window larger than array
Guard k <= n before building the first window.
Remember
Key Takeaways
Update incrementally.
Fixed: add and subtract.
Variable: expand then shrink.
One line: A sliding window is a contiguous range that moves along the array.
Frequently Asked Questions
A method that maintains a moving contiguous range over data, updating its value incrementally to avoid recomputation.
Typically O(n), because each element enters and leaves the window at most once.
Continue with "C Interview Programs" next. C tutorial hub for interview programs: The C Algorithm Techniques hub lists every topic in order.