Skip to content

Commit dca484a

Browse files
Create max_sliding_window_sum.py
1 parent a753d14 commit dca484a

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

max_sliding_window_sum.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# max_sliding_window_sum.py
2+
3+
# Input list
4+
n = [5, 9, 1, 8, 7]
5+
6+
# Running sum of current window
7+
temp = 0
8+
9+
# Left pointer of sliding window
10+
l = 0
11+
12+
# Variable to store the maximum sum of any window
13+
ans = 0
14+
15+
# Iterate through the list
16+
for i in range(0, len(n)):
17+
# Add the current element to running sum
18+
temp += n[i]
19+
20+
# If window size exceeds 3, remove the leftmost element
21+
if (i - l == 3):
22+
temp -= n[l] # remove element going out of window
23+
l += 1 # move left pointer
24+
print(l, i) # debug: shows window boundaries
25+
26+
# If window size is exactly 3, check window sum
27+
if (i - l + 1 == 3):
28+
print(temp) # print the current window sum
29+
ans = max(ans,temp) # update maximum window sum
30+
31+
# Print the maximum sum found
32+
print(ans)

0 commit comments

Comments
 (0)