Max Sales Window

Sliding Window Array Subarray Maximum Sum

Problem: Max Sales Window

On February 24, 2025, communities are preparing for a festive celebration. Local vendors want to determine the best sequence of consecutive days to showcase their best sales leading into the festival. Given an array of integers where each element represents the sales for a day and an integer k representing a fixed number of consecutive days, find the maximum total sales that can be achieved in any contiguous subarray of size k.

Input

  • An array of integers sales representing daily sales.
  • An integer k specifying the window size.

Output

  • An integer representing the maximum sum of sales over a contiguous subarray of size k.

Example

Input: sales = [1, 3, 2, 5, 7, 2, 4], k = 3
Output: 14

Explanation: The subarray [5, 7, 2] has a sum of 14 which is the maximum possible sum for any subarray of length 3.

Constraints

  • You can assume that the array length is at least k.
  • Aim for a solution with O(n) time complexity using the sliding window technique.

Happy coding and enjoy the festive spirit!