Skip to content

Instantly share code, notes, and snippets.

@codecakes
Created December 17, 2020 08:38
Show Gist options
  • Save codecakes/abd8774f020a4f8e91a556133714722c to your computer and use it in GitHub Desktop.
Save codecakes/abd8774f020a4f8e91a556133714722c to your computer and use it in GitHub Desktop.
max sub array sum
def max_sub_array_of_size_k(k, arr):
max_sum = -1
running_sum = start_idx = 0
for tail_idx, num in enumerate(arr):
running_sum += num
if tail_idx - start_idx == k:
running_sum -= arr[start_idx]
start_idx += 1
max_sum = (running_sum and max(max_sum, running_sum)) or max_sum
return max_sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment