#C7367. Maximum Transaction Sum in a Fixed-Size Window

    ID: 51230 Type: Default 1000ms 256MiB

Maximum Transaction Sum in a Fixed-Size Window

Maximum Transaction Sum in a Fixed-Size Window

You are given an integer n representing the number of transactions, an integer k representing the size of a contiguous subarray, and a list of n integers representing the transaction amounts. Your task is to find the maximum total amount of transactions within any contiguous subarray of size k.

The problem can be expressed in mathematical terms as follows:

Given a list \(a_1, a_2, \ldots, a_n\), find the maximum value of \(S_i = \sum_{j=i}^{i+k-1} a_j\) for \(1 \le i \le n-k+1\).

It is guaranteed that k is at least 1 and at most n, and that the list contains at least one number. The transaction amounts may be negative as well.

Example:

Input: n = 7, k = 3, transactions = [2, 1, 5, 1, 3, 2, -1]
Output: 9

inputFormat

The first line of input contains two integers n and k separated by a space, where:

  • n is the number of transactions.
  • k is the size of the contiguous window.

The second line contains n space-separated integers representing the transaction amounts.

outputFormat

Output a single integer representing the maximum sum of any contiguous subarray of size k.

## sample
7 3
2 1 5 1 3 2 -1
9