Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Luis Roel
A simple solution in Python

Luis Roel

Aug 24, 2023

class Solution: def findMaxSumSubArray(self,k, arr): # Check if arra is empty if not arr: return -1 # Pointer to leftmost index of our window l = 0 # Set max_so_far to the sum max_so_far = sum(arr[:k]) curr_sum = max_so_far # Start iterating from size k onwards for r in range(k, len(arr)): # Add element from right into current sum curr_sum += arr[r] # Now check if our window exceeds size k if (r - l) + 1 > k: # Subtrack leftmost element of window curr_sum -= arr[l] # Shift left l += 1 # In either case, update max max_so_far = max(max_so_far, curr_sum) return max_so_far

0

0

Comments
Comments
Luis Roel
Luis Roel10 months ago
  if (r - l) + 1 > k:

You can forego adding 1 by just doing >=

On this page