Created
December 17, 2020 08:38
-
-
Save codecakes/abd8774f020a4f8e91a556133714722c to your computer and use it in GitHub Desktop.
max sub array sum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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