#K39432. Maximum Orders in K Consecutive Days

    ID: 26419 Type: Default 1000ms 256MiB

Maximum Orders in K Consecutive Days

Maximum Orders in K Consecutive Days

You are given the number of orders received on each day over a period of N days. Your task is to compute the maximum total orders received over any contiguous subarray of exactly K consecutive days.

Formally, for an array of orders orders[0..N-1] and a window size K, you need to find the maximum value of:

\( S = \sum_{i=j}^{j+K-1} orders[i] \)

where \( 0 \leq j \leq N-K \). The problem can be efficiently solved using a sliding window technique.

inputFormat

The input is read from standard input (stdin) and has the following format:

T
N1 K1
orders_1[0] orders_1[1] ... orders_1[N1-1]
N2 K2
orders_2[0] orders_2[1] ... orders_2[N2-1]
...
NT KT
orders_T[0] orders_T[1] ... orders_T[NT-1]

Where:

  • T is the number of test cases.
  • For each test case, the first line contains two integers N and K, representing the number of days and the length of the contiguous period, respectively.
  • The next line contains N integers, where each integer denotes the number of orders received on that day.

outputFormat

For each test case, output a single integer on a new line --- the maximum sum of orders over any contiguous subarray of exactly K days.

## sample
1
5 3
1 2 3 4 5
12

</p>