#K66122. Maximum and Minimum Precipitation in Consecutive Days
Maximum and Minimum Precipitation in Consecutive Days
Maximum and Minimum Precipitation in Consecutive Days
In this problem, you are given the precipitation amounts for n days. Your task is to compute the maximum and minimum total precipitation that occurred over exactly k consecutive days. Formally, given an integer ( n ) and an integer ( k ), along with a list ( P = [p_1, p_2, \dots, p_n] ) of precipitation amounts, you should find:
[ max = \max_{1 \leq i \leq n-k+1} \sum_{j=i}^{i+k-1} p_j, \quad min = \min_{1 \leq i \leq n-k+1} \sum_{j=i}^{i+k-1} p_j. ]
The input is provided via standard input and the output should be printed to standard output. This problem requires a sliding window technique to efficiently compute the sum over every contiguous block of ( k ) days.
inputFormat
The first line of input contains two integers ( n ) and ( k ) separated by a space, where ( n ) is the total number of days and ( k ) is the number of consecutive days to consider. The second line contains ( n ) space-separated integers, representing the precipitation amounts for each day.
outputFormat
Output two space-separated integers: the maximum and the minimum total precipitation over any segment of exactly ( k ) consecutive days.## sample
7 3
2 1 5 7 9 3 4
21 8