Algorithmic patterns · 6 / 15 Exercise Medium

Maximum sum of k consecutive items

Objective

A fixed-size sliding window of size k moves along the list: when it advances one element enters and another leaves, so the sum is updated in O(1) instead of being recomputed.

Instructions

  • The first line has space-separated integers and the second an integer k (always between 1 and the amount of numbers).
  • Show Maximum sum: x with the largest sum of k consecutive elements.
Input
1 4 2 10 2 3 1 0 20
4
Output
Maximum sum: 24

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise