0
0
DSA Pythonprogramming~30 mins

Sliding Window on Arrays in DSA Python - Build from Scratch

Choose your learning style9 modes available
Sliding Window on Arrays
📖 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.