Bird
0
0
DSA Cprogramming

Sliding Window on Arrays in DSA C

Choose your learning style9 modes available
Mental Model
A sliding window moves over the array to look at a small part at a time, updating results without starting over.
Analogy: Imagine looking through a small window on a long fence and sliding it step by step to see different parts without moving back.
Array: [1] [2] [3] [4] [5] [6] [7] [8]
Window size 3:
[1] [2] [3] ↑window
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
Dry Run Walkthrough
Input: array: [1, 3, 2, 6, 4, 5], window size: 3
Goal: Find the sum of every window of size 3 in the array
Step 1: Calculate sum of first window [1, 3, 2]
Window: [1] [3] [2] -> sum = 6
Why: We need a starting sum to slide the window forward
Step 2: Slide window right by 1: remove 1, add 6
Window: [3] [2] [6] -> sum = 6 - 1 + 6 = 11
Why: Update sum efficiently without re-adding all elements
Step 3: Slide window right by 1: remove 3, add 4
Window: [2] [6] [4] -> sum = 11 - 3 + 4 = 12
Why: Keep updating sum as window moves
Step 4: Slide window right by 1: remove 2, add 5
Window: [6] [4] [5] -> sum = 12 - 2 + 5 = 15
Why: Final window sum calculated
Result:
Window sums: 6, 11, 12, 15
Annotated Code
DSA C
#include <stdio.h>

void sliding_window_sum(int arr[], int n, int k) {
    if (k <= 0 || k > n) {
        // No valid windows
        return;
    }
    int window_sum = 0;
    // Calculate sum of first window
    for (int i = 0; i < k; i++) {
        window_sum += arr[i];
    }
    printf("%d", window_sum);
    // Slide the window from start to end
    for (int i = k; i < n; i++) {
        window_sum = window_sum - arr[i - k] + arr[i];
        printf(", %d", window_sum);
    }
    printf("\n");
}

int main() {
    int arr[] = {1, 3, 2, 6, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 3;
    sliding_window_sum(arr, n, k);
    return 0;
}
for (int i = 0; i < k; i++) { window_sum += arr[i]; }
Calculate initial sum of the first window
for (int i = k; i < n; i++) { window_sum = window_sum - arr[i - k] + arr[i]; }
Slide window by removing left element and adding new right element
OutputSuccess
6, 11, 12, 15
Complexity Analysis
Time: O(n) because we calculate the first window sum once and then update sums in constant time for each slide
Space: O(1) because we only store the current window sum and no extra arrays
vs Alternative: Naive approach recalculates sum for each window in O(k) time, leading to O(n*k) total, which is slower than this O(n) method
Edge Cases
window size k is 0
No windows to sum, function prints nothing or zero sums
DSA C
if (k <= 0 || k > n) { return; }
window size k is greater than array length
No valid windows, function prints nothing
DSA C
if (k <= 0 || k > n) { return; }
array with one element and window size 1
Prints the single element as the only window sum
DSA C
for (int i = 0; i < k; i++) { window_sum += arr[i]; }
When to Use This Pattern
When you need to process all subarrays or parts of fixed size efficiently, use sliding window to avoid repeated work.
Common Mistakes
Mistake: Recalculating the sum of the window from scratch on every slide
Fix: Update the sum by subtracting the element leaving the window and adding the new element entering
Summary
Calculates results over fixed-size parts of an array by moving a window step-by-step.
Use when you want to find sums, averages, or other aggregates over all subarrays of a fixed size efficiently.
The key is to update the result by removing the old element and adding the new one instead of recalculating everything.