This video serves as an introduction to the "Sliding Window" and "2 Pointers" algorithmic techniques, presented as part of the "Strivers A to Z DSA" course. The speaker explains that these are not traditional algorithms but rather patterns that require understanding a concept and then applying it to solve specific problems based on the problem statement. The video outlines four distinct patterns within this topic, with a focus on how to approach and solve problems using these techniques, particularly in the context of technical interviews.
Here are the notes for the video:
Introduction to Sliding Window & 2 Pointers
Purpose: Introduce Sliding Window and 2 Pointers patterns for DSA problems, especially for interviews.
Nature of Pattern: Not a rigid algorithm, but a concept to be adapted to problem statements.
Key Techniques:
Four Main Patterns Discussed:
Fixed-Size Window:
Longest/Shortest Subarray/Substring with a Condition:
arr[R] to the sum.sum > K (or condition violated), move L forward, subtracting arr[L] from the sum, until the condition is met again.(R - L + 1) is valid (meets the condition) and is longer than max_length, update max_length.Counting Subarrays with a Specific Condition:
Shortest Window/Subarray with a Condition:
sum >= K is met, try to shrink the window from the left (move L forward) while the condition still holds.min_length = min(min_length, R - L + 1).General Template for Sliding Window (Pattern 2/4):
arr[R] to sum.sum > K):
arr[L] from sum.max_length = max(max_length, R - L + 1)).Time/Space Complexity:
Here are the notes for the video, including pseudocode for the common sliding window patterns:
Introduction to Sliding Window & 2 Pointers
Purpose: Introduce Sliding Window and 2 Pointers patterns for DSA problems, especially for interviews.
Nature of Pattern: Not a rigid algorithm, but a concept to be adapted to problem statements.
Key Techniques:
Four Main Patterns Discussed:
Fixed-Size Window:
def max_sum_fixed_window(arr, k):
n = len(arr)
if n < k:
return 0 # Or handle error
current_sum = sum(arr[:k])
max_sum = current_sum
for i in range(k, n):
current_sum += arr[i] - arr[i-k] # Slide window: add new, remove old
max_sum = max(max_sum, current_sum)
return max_sum
Longest/Shortest Subarray/Substring with a Condition:
sum = 0, max_length = 0.arr[R] to the sum.sum > K (or condition violated), move L forward, subtracting arr[L] from the sum, until the condition is met again.(R - L + 1) is valid (meets the condition) and is longer than max_length, update max_length.def longest_subarray_sum_le_k(arr, k):
n = len(arr)
l = 0
current_sum = 0
max_length = 0
for r in range(n):
current_sum += arr[r] # Expand window
# Contract window if condition is violated
while current_sum > k:
current_sum -= arr[l]
l += 1
# If condition is met, update max_length
# The window arr[l...r] is now valid
max_length = max(max_length, r - l + 1)
return max_length
import sys
def shortest_subarray_sum_ge_k(arr, k):
n = len(arr)
l = 0
current_sum = 0
min_length = sys.maxsize # Initialize with a very large value
for r in range(n):
current_sum += arr[r] # Expand window
# Contract window WHILE condition is met AND trying to find shortest
while current_sum >= k:
min_length = min(min_length, r - l + 1)
current_sum -= arr[l]
l += 1
return min_length if min_length != sys.maxsize else 0 # Handle case where no such subarray exists
Counting Subarrays with a Specific Condition:
Shortest Window/Subarray with a Condition:
General Template for Sliding Window (Pattern 2/4):
# For problems looking for MAX length / count of valid windows
def sliding_window_max(arr, k_condition): # k_condition represents the constraint
n = len(arr)
l = 0
current_metric = 0 # e.g., sum, count of distinct chars
result = 0 # e.g., max_length, count of windows
for r in range(n):
# Expand window: update current_metric with arr[r]
current_metric += arr[r] # Example: sum
# Contract window if condition is violated
while not is_valid(current_metric, k_condition): # is_valid checks the condition
current_metric -= arr[l]
l += 1
# If condition is met, process the current valid window
# Example: For max length
result = max(result, r - l + 1)
# Example: For counting valid windows
# result += (r - l + 1) # If ALL sub-windows ending at r are valid
return result
# For problems looking for MIN length
def sliding_window_min(arr, k_condition):
n = len(arr)
l = 0
current_metric = 0
result = sys.maxsize # Initialize with infinity
for r in range(n):
# Expand window
current_metric += arr[r]
# Contract window WHILE condition is met and we are seeking minimum
while is_valid(current_metric, k_condition): # is_valid checks the condition
result = min(result, r - l + 1)
current_metric -= arr[l]
l += 1
return result if result != sys.maxsize else 0 # Handle no valid window case
Time/Space Complexity:
This video serves as an introduction to the "Sliding Window" and "2 Pointers" algorithmic techniques, presented as part of the "Strivers A to Z DSA" course. The speaker explains that these are not traditional algorithms but rather patterns that require understanding a concept and then applying it to solve specific problems based on the problem statement. The video outlines four distinct patterns within this topic, with a focus on how to approach and solve problems using these techniques, particularly in the context of technical interviews.
import java.util.HashMap;
import java.util.Map;
import java.lang.Math;
class Solution {
// Pattern 4: Shortest Window/Subarray with a Condition
// Example: Find the shortest subarray whose sum is >= k
public int shortestSubarraySumGeqK(int[] arr, int k) {
int n = arr.length;
int l = 0;
long currentSum = 0; // Use long to prevent overflow
int minLength = Integer.MAX_VALUE;
for (int r = 0; r < n; r++) {
currentSum += arr[r]; // Expand window
// Contract window WHILE condition is met and we are seeking minimum
// Note: The condition here is currentSum >= k
while (currentSum >= k) {
minLength = Math.min(minLength, r - l + 1);
currentSum -= arr[l];
l++;
}
}
return minLength == Integer.MAX_VALUE ? 0 : minLength; // Return 0 if no such subarray exists
}
// Example for Pattern 2: Longest Subarray Sum <= K
public int longestSubarraySumLeqK(int[] arr, int k) {
int n = arr.length;
int l = 0;
long currentSum = 0;
int maxLength = 0;
for (int r = 0; r < n; r++) {
currentSum += arr[r]; // Expand window
// Contract window if condition is violated
// The condition here is currentSum > k
while (currentSum > k) {
currentSum -= arr[l];
l++;
}
// If condition is met (currentSum <= k), update maxLength
maxLength = Math.max(maxLength, r - l + 1);
}
return maxLength;
}
}