Off By One Window
This is meant to return the largest sum of any k consecutive elements. It contains a single index bug.
def max_window_sum(a, k):
total = sum(a[:k])
best = total
for i in range(k, len(a)):
total += a[i] - a[i - k + 1]
best = max(best, total)
return best
What does max_window_sum([1, 3, 2, 6, 2, 8], 3) actually return?
Sign in to answer questions and track your progress
Sign In