📖 Scenario: You are analyzing daily temperatures for a week to find the hottest 3-day period.
🎯 Goal: Build a program that uses the sliding window technique to find the maximum sum of temperatures over any 3 consecutive days.
📋 What You'll Learn
Create a list of daily temperatures for 7 days
Create a variable for the window size set to 3
Use a sliding window approach to find the maximum sum of any 3 consecutive days
Print the maximum sum found
💡 Why This Matters
🌍 Real World
Analyzing temperature trends helps in weather forecasting and planning outdoor activities.
💼 Career
Sliding window technique is widely used in data analysis, signal processing, and performance optimization tasks.
Progress0 / 4 steps
1
Create the list of daily temperatures
Create a list called temperatures with these exact values: [30, 32, 31, 29, 35, 36, 33]
DSA Python
Hint
Use square brackets and separate numbers with commas.
2
Set the window size
Create a variable called window_size and set it to 3
DSA Python
Hint
Just assign the number 3 to the variable window_size.
3
Find the maximum sum of any 3 consecutive days using sliding window
Create a variable called max_sum and set it to the sum of the first 3 elements in temperatures. Then use a for loop with variable i from 1 to len(temperatures) - window_size to slide the window. Inside the loop, update current_sum by subtracting the element leaving the window and adding the new element entering the window. Update max_sum if current_sum is greater.
DSA Python
Hint
Start with the sum of the first 3 days. Then slide the window by removing the first day and adding the next day in each step.
4
Print the maximum sum
Print the value of max_sum
DSA Python
Hint
Use print(max_sum) to show the result.
Practice
(1/5)
1. What does the sliding window technique help to optimize when working with arrays?
easy
A. It reduces the time complexity by avoiding repeated calculations over overlapping subarrays.
B. It sorts the array in ascending order.
C. It finds the maximum element in the entire array.
D. It reverses the array elements.
Solution
Step 1: Understand the sliding window purpose
The sliding window technique moves a fixed-size window over the array to process elements efficiently without recalculating everything each time.
Step 2: Identify what it optimizes
It avoids repeated work on overlapping parts of the array, thus reducing time complexity compared to naive methods.
Final Answer:
It reduces the time complexity by avoiding repeated calculations over overlapping subarrays. -> Option A
The window_sum update should subtract arr[i - window_size], not add it. -> Option B
Quick Check:
Subtract leaving element, not add [OK]
Hint: Subtract old element leaving window, add new element [OK]
Common Mistakes:
Adding both new and old elements instead of subtracting old
Wrong loop start index
Incorrect initial sum
5. Given an array arr = [1, 3, 2, 6, -1, 4, 1, 8, 2] and window size 5, which sliding window sum is the maximum? Use the sliding window technique to find the maximum sum of any 5 consecutive elements.
hard
A. 16
B. 19
C. 17
D. 18
Solution
Step 1: Calculate initial window sum for first 5 elements