Sliding window

In a board game your piece visits exactly k neighboring squares. Some squares give you points and others take them away. Where should you start to earn the most points?

Add the first k scores. Each shift drops one square on the left and gains one on the right. All the other scores are already in the total.

Remember a new start only when its sum is strictly greater than the record. A tie keeps the leftmost window.

Add each number once and subtract it at most once. Time is O(n) and extra space is O(1).