Maximum Average Subarray I

Practice this coding question on Coding Gurukul.

Problem statement

A weather station wants to report the warmest average across a fixed number of consecutive readings. It must compare every complete window of that size, including windows near the end of the recording. Your task Find the maximum arithmetic mean of any contiguous subarray of exactly k elements. Important details Negative readings are allowed. Every candidate window has the same length, so maximizing its sum also maximizes its mean. Print exactly five digits after the decimal point, rounding to the nearest value; exact halfway cases round away from zero. Worked example For readings [2, 8, 4, 6] and k = 2, the window sums are 10, 12 and 10. The maximum average is 12 / 2 = 6.00000.

Input format

First line: n k. Next line: n readings.

Output format

Print the maximum average with five decimal places.

Constraints

1 <= k <= n <= 100000; -10000 <= readings <= 10000.

Sample input

4 2
2 8 4 6

Sample output

6.00000

Sign in to run or submit a solution in C++, Java or Python.