Sliding Window Technique in C

Advanced
8 min read
Updated: Nov 2026
2 examples

A Window That Moves Along the Array

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++]; }
}

Cheat Sheet

Keep these facts handy while you work with Sliding Window Technique in C in C.

TopicDetail
Fixed windowAdd entering, subtract leaving
Variable windowGrow right, shrink left
TimeO(n)
Works withContiguous subarrays and substrings

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;
}

How It Works

  1. The first window (2, 1, 5) sums to 8.
  2. Sliding adds the new element and removes the old one: 7, then 9, then 6.
  3. 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;
}

How It Works

  1. The right end grows until the sum reaches 7 or more.
  2. Then the left end shrinks while the sum still qualifies, recording the smallest length.
  3. 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.

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.

Next: Interview Programs

C tutorial hub for interview programs:

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful