#C3000. Maximum Average Subarray
Maximum Average Subarray
Maximum Average Subarray
Given an array of N integers and an integer K, your task is to find the maximum average of any contiguous subarray of length K. The average of a subarray is defined as the sum of its elements divided by K.
Mathematical Formulation:
Given an array \(a_1, a_2, \dots, a_N\) and an integer \(K\), find \[ \max_{1 \leq i \leq N-K+1} \left( \frac{a_i + a_{i+1} + \cdots + a_{i+K-1}}{K} \right). \]
Constraints: \(1 \leq K \leq N\); array elements may be positive, zero, or negative.
The optimal solution uses a sliding window technique to achieve an \(O(N)\) time complexity.
inputFormat
The input is read from standard input (stdin). The first line contains two integers N and K separated by a space. The second line contains N space-separated integers representing the array elements.
outputFormat
Output a single floating point number to standard output (stdout) which is the maximum average of any contiguous subarray of length K.## sample
6 4
1 12 -5 -6 50 3
12.75
</p>