Hey great article, it’s so helpful!
But one thing I wanted to point out as I was actually following your examples step by step:
int max_sum = 0, window_sum = 0;/* calculate sum of 1st window */
for (int i = 0; i < k; i++) window_sum += arr[i];/* slide window from start to end in array. */
for (int i = k; i < n; i++){
window_sum += arr[i] - arr[i-k]; // saving re-computation
max_sum = max(max_sum, window_sum);
}
I don’t think this gives the correct max_sum for arr = [700, 800, 900, 1, 2] and k = 3, because your very first window_sum 700+800+900 is never actually compared against max_sum.
Before we have a chance to compare against max_sum and set max_sum equal to that window_sum, you modify it in the first iteration of your second for-loop, I think.
Please let me know if I’m wrong!



