This course is designed to help learners master the core concepts of Data Structures and Algorithms (DSA) using the C programming language. Starting from the basics and advancing to complex topics like graphs, dynamic programming, and memory optimization, this course is ideal for students, job seekers, and aspiring developers. You’ll learn how to structure and manipulate data efficiently, solve real-world coding problems, and prepare for technical interviews at top companies. The content is structured step-by-step, combining theory with hands-on coding examples and practice problems to reinforce understanding. Whether you're preparing for university exams, campus placements, or competitive programming, this course provides a strong foundation in logic building, code efficiency, and problem-solving using C. Key Highlights: Covers all major DSA topics from beginner to advanced level 100+ coding examples with explanations Focus on time and space complexity optimization Designed for coding interviews, competitive exams, and CS fundamentals
Imagine you're inspecting a long conveyor belt of items, and you need to calculate the sum of items in a specific section, then move that section forward without re-calculating everything from scratch. This is the essence of the Sliding Window Technique! It's a powerful and efficient algorithmic approach used to solve problems that involve finding a contiguous subarray, substring, or subsegment in a data structure (like an array or string) that satisfies certain conditions.
At its core, the sliding window technique maintains a "window" (a range of elements) that slides over the input data. Instead of re-evaluating the entire subsegment for each position, it efficiently updates the window's properties (like sum, count of distinct elements, max/min, etc.) by "removing" the element leaving the window and "adding" the new element entering the window. This reduces redundant computations.
Think of it like a fixed-size camera frame moving across a landscape, or a flexible net expanding and contracting to catch a certain number of fish.
Sliding window problems typically fall into two categories:
The window always maintains a constant size `K`. This is useful for problems like "Find the maximum sum subarray of size K".
// Example: Maximum sum subarray of size K
// Input: arr = [1, 4, 2, 10, 2, 3, 1, 0, 20], K = 3
// Output: 25 (from [10, 2, 3, 1, 0, 20] -> max sum is 10+2+3=15, 2+3+1=6, 3+1+0=4, 1+0+20=21) Oh, typo in example. Let's trace it.
// Correct example: [1, 4, 2, 10, 2, 3, 1, 0, 20], K=3
// Window 1: [1, 4, 2] -> Sum = 7
// Window 2: [4, 2, 10] -> Sum = 16 (max_so_far = 16)
// Window 3: [2, 10, 2] -> Sum = 14
// Window 4: [10, 2, 3] -> Sum = 15
// Window 5: [2, 3, 1] -> Sum = 6
// Window 6: [3, 1, 0] -> Sum = 4
// Window 7: [1, 0, 20] -> Sum = 21 (max_so_far = 21)
// Result: 21
int max_sum_fixed_window(vector<int>& arr, int K) {
int N = arr.size();
if (N < K) return 0; // Or handle error
int current_sum = 0;
for (int i = 0; i < K; ++i) { // Initialize first window
current_sum += arr[i];
}
int max_sum = current_sum;
for (int right = K; right < N; ++right) {
current_sum += arr[right]; // Add new element to window
current_sum -= arr[right - K]; // Remove element leaving window
max_sum = max(max_sum, current_sum); // Update max sum found
}
return max_sum;
}
The window size changes dynamically based on a condition. This is common for problems like "Find the smallest subarray with sum greater than or equal to S", or "Longest substring with K distinct characters".
// Example: Smallest subarray with sum >= S
// Input: arr = [2, 1, 5, 2, 3, 2], S = 7
// Output: 2 (subarray [5, 2])
int min_len_subarray_sum(vector<int>& arr, int S) {
int N = arr.size();
int min_length = INT_MAX;
int current_sum = 0;
int left = 0;
for (int right = 0; right < N; ++right) {
current_sum += arr[right]; // Expand window by adding element at 'right'
// While current_sum meets the condition, shrink window from 'left'
while (current_sum >= S) {
min_length = min(min_length, right - left + 1); // Record current window length
current_sum -= arr[left]; // Remove element at 'left'
left++; // Shrink window from left
}
}
return (min_length == INT_MAX) ? 0 : min_length; // Return 0 if no such subarray
}
The green box represents the sliding window of fixed size K=3. As the window slides to the right, the element leaving the window is subtracted from the sum, and the new element entering is added, efficiently updating the window's sum. The "Max Sum" tracks the highest sum found so far.
(Note: This is a static representation; in a dynamic environment, the window would visually slide.)
One of the main reasons for using the Sliding Window technique is its efficiency:
Advantages:
Disadvantages:
The Sliding Window technique is incredibly practical in scenarios where you need to process streams of data or analyze segments efficiently:
Building on the concept of multiple pointers, our next chapter explores the **Two Pointers Technique**. While often used within a sliding window, it's also a standalone powerful method for solving array and list problems by efficiently traversing data from different directions or at different speeds.